我正在爲我的女朋友製作這款遊戲,而且現在我一直被困在同一個問題上。基本上,我希望她能夠按下「收集木材」按鈕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()來主要調用它,並且我不知道面板有大小,所以我不知道它的大小。 –