我正在为我的女朋友开发这款游戏,几天来我一直被同样的问题困扰。基本上,我希望她能够按“收集木材”按钮 5 次,然后,在她第五次按下后,“创建火”按钮应该弹出。
- 问题是,无论我尝试哪种方式编程 方法在第五个按钮上显示,按下它只是不显示。
如果有任何编码技巧或你们认为我能做的任何事情,我将不胜感激 清理我当前的代码。
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)); }
我不知道面板尺寸,也不知道如何找到它。该面板被添加到
JFrame
在main
程序。gatherWood()
和createFire()
函数都在 main 中静态调用。我应该使用run()
调用方法?
最佳答案
我认为你应该创建这两个按钮(“Gathering Wood”,“Create Fire”),但使第二个按钮不可见:
fire.setVisible(false);
当你的 woodCounter 等于 5 时,你就让它可见。
如果您需要更多解释,我可以向您展示如何做到这一点。
关于java - 如何在一定时间后调用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39898507/