如何在我想要的时候显示按钮? [英] How do I make my button display at the time I want it to?

查看:67
本文介绍了如何在我想要的时候显示按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的女朋友制作这个游戏,而且我几天都遇到了同样的问题。基本上,我希望她能够按下Gather Wood按钮5次,然后在第五次按下它之后,会弹出Create Fire按钮。

I'm working on this game for my girlfriend and I've been stuck on the same problem for a few days now. 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.

1.问题在于,无论我尝试将方法编程为按第五个按钮显示,按下它都不会显示。

1.The problem is that no matter which way I attempt to program the method to show up on the fifth button press it just doesn't show up.


  1. 我很感激任何编码提示或任何你认为我可以做的事情来清理我的当前代码。

  1. I would appreciate any coding tips or anything y'all think I can do to clean up my current code.

private static JPanel panel;
private static int woodCounter;
private static int leafCounter;
private static JFrame frame;


  • 这是收集木材按钮

  • This is the gather wood button

    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;
    }
    


  • 这是创建开火按钮

  • This is the create fire button

    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));
    } 
    



  • 推荐答案


    基本上,我希望她能够按下Gather Wood按钮5次,然后在她第五次按下它之后,应该弹出Create Fire按钮up。

    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告诉代码做任何事情。

    I don't see any "if logic" anywhere that tells the code to do anything.

    一旦你修复了(并验证调用了createFire()`方法)我怀疑下一个问题是当你向一个可见的Swing GUI添加一个组件时,基本代码应该是:

    Once you fix that (and verify that the "createFire()` method is invoked) I suspect the next problem is that when you add a component to a visible Swing GUI the basic code should be:

    panel.add(...);
    panel.revalidate();
    panel.repaint();
    

    你需要 revalidate()调用布局管理器,否则添加的组件大小为(0,0),没有任何东西可以绘制。

    You need the revalidate() to invoke the layout manager otherwise the added component has a size of (0, 0) and there is nothing to paint.

    panel.add(fire, new FlowLayout(FlowLayout.CENTER));
    

    不要继续尝试茶布局管理器。这不是第二个参数的用途。创建面板时,面板的布局管理器只应设置一次。

    Don't keep trying to change the layout manager. That is not what the second parameter is used for. The layout manager of the panel should be set only once when the panel is created.

    这篇关于如何在我想要的时候显示按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆