如何在java中禁用javax.swing.JButton?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1625855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:58:21  来源:igfitidea点击:

How to disable javax.swing.JButton in java?

javauser-interfacejbutton

提问by Yatendra Goel

I have created a swings application and there is a "Start" button on the GUI. I want that whenever I clicked on that "Start" button, the start button should be disabled and the "Stop" button be enabled.

我创建了一个 Swings 应用程序,GUI 上有一个“开始”按钮。我希望每当我点击“开始”按钮时,应该禁用开始按钮并启用“停止”按钮。

For that I have written the following code in the "ActionPeformed(...)" method of the "Start" button

为此,我在“开始”按钮的“ActionPeformed(...)”方法中编写了以下代码

startButton.setEnabled(false);
stopButton.setEnabled(true);

But the above code is not creating the desired affect on the GUI.

但是上面的代码并没有对 GUI 产生预期的影响。

Is the above code correct for what I want to do?

上面的代码是否适合我想要做的事情?

It's not working with "repaint()" too.

它也不适用于“repaint()”。

Edit:

编辑:

The code is very long so I can't paste all the code. I can tell, though, more about the code.

代码很长,所以我无法粘贴所有代码。不过,我可以说更多关于代码的信息。

In the "ActionPeformed" method of "start" button, after calling the above two statements, I am executing a "SwingWorker" thread.

在“开始”按钮的“ActionPeformed”方法中,调用上述两条语句后,我正在执行一个“SwingWorker”线程。

Is this thread creating any problem?

这个线程有什么问题吗?

采纳答案by akf

For that I have written the following code in the "ActionPeformed(...)" method of the "Start" button

为此,我在“开始”按钮的“ActionPeformed(...)”方法中编写了以下代码

You need that code to be in the actionPerformed(...)of the ActionListenerregistered with the Start button, not for the Start button itself.

你需要的代码是在actionPerformed(...)ActionListener使用开始按钮注册,而不是开始按钮本身。

You can add a simple ActionListenerlike this:

你可以添加一个ActionListener这样的简单:

JButton startButton = new JButton("Start");
startButton.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent ae) {
        startButton.setEnabled(false);
        stopButton.setEnabled(true);
     }
   }
 );

note that your startButton above will need to be finalin the above example if you want to create the anonymous listener in local scope.

请注意,final如果您想在本地范围内创建匿名侦听器,则上面的startButton 需要在上面的示例中。

回答by broschb

This works.

这有效。

public class TestButton {

public TestButton() {
    JFrame f = new JFrame();
    f.setSize(new Dimension(200,200));
    JPanel p = new JPanel();
    p.setLayout(new FlowLayout());

    final JButton stop = new JButton("Stop");
    final JButton start = new JButton("Start");
    p.add(start);
    p.add(stop);
    f.getContentPane().add(p);
    stop.setEnabled(false);
    stop.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            start.setEnabled(true);
            stop.setEnabled(false);

        }
    });

    start.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            start.setEnabled(false);
            stop.setEnabled(true);

        }
    });
    f.setVisible(true);
}

/**
 * @param args
 */
public static void main(String[] args) {
    new TestButton();

}

}

}

回答by camickr

The code is very long so I can't paste all the code.

代码很长,所以我无法粘贴所有代码。

There could be any number of reasons why your code doesn't work. Maybe you declared the button variables twice so you aren't actually changing enabling/disabling the button like you think you are. Maybe you are blocking the EDT.

您的代码不起作用的原因可能有很多。也许你声明了两次按钮变量,所以你实际上并没有像你想象的那样改变启用/禁用按钮。也许您正在阻止 EDT。

You need to create a SSCCEto post on the forum.

您需要创建一个SSCCE才能在论坛上发帖。

So its up to you to isolate the problem. Start with a simple frame thas two buttons and see if your code works. Once you get that working, then try starting a Thread that simply sleeps for 10 seconds to see if it still works.

因此,由您来隔离问题。从一个包含两个按钮的简单框架开始,看看您的代码是否有效。一旦你开始工作,然后尝试启动一个简单地休眠 10 秒的线程,看看它是否仍然有效。

Learn how the basice work first before writing a 200 line program.

在编写 200 行程序之前,先了解基础是如何工作的。

Learn how to do some basic debugging, we are not mind readers. We can't guess what silly mistake you are doing based on your verbal description of the problem.

学习如何做一些基本的调试,我们不介意读者。根据您对问题的口头描述,我们无法猜测您犯了什么愚蠢的错误。