简体   繁体   English

如何在Java中单击时使按钮不可见?

[英]How to make a button invisible when clicked in java?

I am working on a project from a textbook and I'm stuck. 我正在研究教科书上的一个项目,但遇到了麻烦。 The goal is: When the GUI first appears, both buttons are visible, but when one button is clicked, that button disappears and only the other button is visible. 目标是:首次出现GUI时,两个按钮都可见,但是单击一个按钮时,该按钮消失,只有另一个按钮可见。 Thereafter only one button is visible; 此后,只有一个按钮可见。 when the button is clicked, it disappears and the other button appears 单击该按钮时,它消失并且出现另一个按钮

public class ButtonDemo extends JFrame implements ActionListener
{
    public static final int WIDTH = 400;
    public static final int HEIGHT = 300;

    public ButtonDemo()
    {
        setSize(WIDTH, HEIGHT);
        WindowDestroyer listener = new WindowDestroyer();
        addWindowListener(listener);

        Container contentPane = getContentPane();
        contentPane.setBackground(Color.WHITE);

        contentPane.setLayout(new FlowLayout());

        JButton sunnyButton = new JButton("Sunny");
        sunnyButton.addActionListener(this);
        contentPane.add(sunnyButton);
        JButton cloudyButton = new JButton("Cloudy");
        cloudyButton.addActionListener(this);
        contentPane.add(cloudyButton);
    }

    public void actionPerformed(ActionEvent e)
    {
        String actionCommand = e.getActionCommand();
        Container contentPane = getContentPane();

        if(actionCommand.equals("Sunny"))
        {
            contentPane.setBackground(Color.BLUE);
        }

        else if (actionCommand.equals("Cloudy"))
        {
            contentPane.setBackground(Color.GRAY);


        }
        else
            System.out.println("Error in button interface.");
    }
}

Using setVisible on the buttons should work. 在按钮上使用setVisible应该可以。 Try the following: 请尝试以下操作:

Move the following lines to the fields of ButtonDemo: 将以下行移动到ButtonDemo的字段中:

JButton sunnyButton = new JButton("Sunny");
JButton cloudyButton = new JButton("Cloudy");

Change the if statements in your actionPerformed to: actionPerformed的if语句更改为:

if(actionCommand.equals("Sunny"))
{
    contentPane.setBackground(Color.BLUE);
    sunnyButton.setVisible(false);
    cloudyButton.setVisible(true);
}
else if (actionCommand.equals("Cloudy"))
{
    contentPane.setBackground(Color.GRAY);
    sunnyButton.setVisible(true);
    cloudyButton.setVisible(false);
}

It's very simple code. 这是非常简单的代码。 Make sunnyButton and cloudyButton as instance member. 使sunnyButtoncloudyButton成为实例成员。

Simply check for the source of the action event and hide the source component and show the other one. 只需检查动作事件的源并隐藏源组件并显示另一个即可。

public void actionPerformed(ActionEvent e) {

    if (sunnyButton == e.getSource()) {
        sunnyButton.setVisible(false);
        cloudyButton.setVisible(true);
    } else if (cloudyButton == e.getSource()) {
        sunnyButton.setVisible(true);
        cloudyButton.setVisible(false);
    }
    ...
}

What about 关于什么

source.setVisible(false) 

to hide and 隐藏和

source.setVisible(true) 

to show the buttons? 显示按钮?

Edit: You should get the source of the event (e.getSource()) and hide it 编辑:您应该获取事件的来源(e.getSource())并将其隐藏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM