我正在为我的女朋友制作这款游戏,而且现在我一直被困在同一个问题上。基本上,我希望她能够按下“收集木材”按钮5次,然后在第五次按下按钮后,弹出“创建火灾”按钮。如何让我的按钮在我想要的时候显示?
1.问题是,无论我尝试将方法编程为第五个按钮时显示的方法,它都不会显示出来。
我会很感激任何编码技巧或任何你们都认为我可以做的事来清理我目前的代码。
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)); }
您的'面板'是否足够显示两个按钮? – VGR
谁在调用'gatherWood()'和'createFire()'以及如何? –
对不起,需要很长时间才能回复。我只是通过说gatherWood()和createFire()来主要调用它,并且我不知道面板有大小,所以我不知道它的大小。 –