如何让我的按钮在我想要的时候显示?
How do I make my button display at the time I want it to?
我正在为我的女朋友开发这款游戏,几天来我一直被同样的问题困扰。基本上,我希望她能够按 "Gather Wood" 按钮 5 次,然后在她按第五次后,"Create Fire" 按钮应该会弹出。
1.The 问题是,无论我尝试以何种方式编写在第五次按下按钮时显示的方法,它都不会显示。
我将不胜感激任何编码技巧或你们认为我可以做的任何事情来清理我当前的代码。
private static JPanel panel;
private static int woodCounter;
private static int leafCounter;
private static JFrame frame;
这是收集木材按钮
public static int gatherWood() {
woodCounter = 0;
JButton wood = new JButton("Gather Wood");
wood.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
System.out.println("Gathering Wood");
woodCounter++;
woodCounter++;
System.out.println(woodCounter);
}
});
wood.setVisible(true);
panel.add(wood, new FlowLayout(FlowLayout.CENTER));
return woodCounter;
}
这是创建点火按钮
public static void createFire() {
JButton fire = new JButton("Create Fire");
fire.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
System.out.println("Creating a fire.");
woodCounter = woodCounter - 10;
}
});
fire.setVisible(true);
panel.add(fire, new FlowLayout(FlowLayout.CENTER));
}
Basically, I want her to be able to press the "Gather Wood" button 5 times then, right after she presses it the fifth time, the "Create Fire" button should pop up.
我在任何地方都没有看到任何 "if logic" 告诉代码执行任何操作。
一旦你解决了这个问题(并验证“createFire()”方法被调用)我怀疑下一个问题是当你将一个组件添加到一个可见的 Swing GUI 时,基本代码应该是:
panel.add(...);
panel.revalidate();
panel.repaint();
您需要 revalidate()
来调用布局管理器,否则添加的组件的大小为 (0, 0) 并且没有可绘制的内容。
panel.add(fire, new FlowLayout(FlowLayout.CENTER));
不要一直尝试更改布局管理器。这不是第二个参数的用途。面板的布局管理器应该只在创建面板时设置一次。
我正在为我的女朋友开发这款游戏,几天来我一直被同样的问题困扰。基本上,我希望她能够按 "Gather Wood" 按钮 5 次,然后在她按第五次后,"Create Fire" 按钮应该会弹出。
1.The 问题是,无论我尝试以何种方式编写在第五次按下按钮时显示的方法,它都不会显示。
我将不胜感激任何编码技巧或你们认为我可以做的任何事情来清理我当前的代码。
private static JPanel panel; private static int woodCounter; private static int leafCounter; private static JFrame frame;
这是收集木材按钮
public static int gatherWood() { woodCounter = 0; JButton wood = new JButton("Gather Wood"); wood.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { System.out.println("Gathering Wood"); woodCounter++; woodCounter++; System.out.println(woodCounter); } }); wood.setVisible(true); panel.add(wood, new FlowLayout(FlowLayout.CENTER)); return woodCounter; }
这是创建点火按钮
public static void createFire() { JButton fire = new JButton("Create Fire"); fire.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { System.out.println("Creating a fire."); woodCounter = woodCounter - 10; } }); fire.setVisible(true); panel.add(fire, new FlowLayout(FlowLayout.CENTER)); }
Basically, I want her to be able to press the "Gather Wood" button 5 times then, right after she presses it the fifth time, the "Create Fire" button should pop up.
我在任何地方都没有看到任何 "if logic" 告诉代码执行任何操作。
一旦你解决了这个问题(并验证“createFire()”方法被调用)我怀疑下一个问题是当你将一个组件添加到一个可见的 Swing GUI 时,基本代码应该是:
panel.add(...);
panel.revalidate();
panel.repaint();
您需要 revalidate()
来调用布局管理器,否则添加的组件的大小为 (0, 0) 并且没有可绘制的内容。
panel.add(fire, new FlowLayout(FlowLayout.CENTER));
不要一直尝试更改布局管理器。这不是第二个参数的用途。面板的布局管理器应该只在创建面板时设置一次。