在Swing中禁用GUI上的按钮 [英] Disable buttons on GUI in Swing

查看:168
本文介绍了在Swing中禁用GUI上的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在GUI上有一些按钮,当在Selenium中单击时,它们会执行测试.它们只能串行运行,并且当前已添加到EventQueue中.我希望这样,如果单击一个按钮并执行测试,那么它将禁用其他按钮,因此无法将其他测试添加到队列中.

I have buttons on a GUI that execute tests when clicked on in Selenium. They can only be run serially and are currently added to EventQueue. I would like it so that if a button is clicked on and a test is executed, then it will disable the other buttons so that other tests cannot be added to a queue.

当前按钮外观如下:

    Test1 = new JButton("Test1 ");
    Test1.setLocation(290, 30);
    Test1.setSize(120, 30);
    Test1.addActionListener(this);
    Test1.addMouseListener(new MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent e) {
            if (Test1.isEnabled()) {
                Test1.setEnabled(false);
                errorLabel.setText("");
                service.submit(()->{
                    Result result = JUnitCore.runClasses(Test1.class);
                    EventQueue.invokeLater(()->{
                        errorMessageDisplay(result);
                        Test1.setEnabled(true);
                    });
                });
            }
        }
    });
    buttonPanel.add(Test1);

我使用了EventQueue,因为它允许我在GUI上重置更新通过/失败错误消息.

I have used the EventQueue as it allows me to reset update Pass/Fail error messages on the GUI.

我怎样才能最好地做到这一点?

How can I best achieve this?

推荐答案

您应在按钮上添加ActionListener.更重要的是,您应该使用命名约定,这也意味着对象的名称应以小写字母开头.大写字母保留用于类和静态字段(全部为大写).以下代码将ActionListener添加到JButton,并在单击后将其禁用.如果不是您要的内容,我会在稍后添加另一个版本.

You should add ActionListener to your button. What's even more important, you should use naming conventions what also means that your objects' names should start with a small letter. Capital letters are reserved for Classes and static fields (all upper case). The following code adds an ActionListener to your JButton and disables it after clicked. If it's not what you're looking for, I'll add another version in a moment.

test1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            test1.setEnabled(false);
        }
    });

如果您想保留按钮的状态,但不要禁用它,则以下代码可能是一种解决方案:

In case, you want to keep the state of your button, but don't disable it, the following code might be a solution:

private final static String ENABLED = "ENABLED";
private final static String DISABLED = "DISABLED";

public static void main(String[] args) {
    Map<JButton, String> map = new HashMap<>();
    JButton test1 = new JButton();
    map.put(test1, ENABLED);
    test1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (map.get(test1).equals(ENABLED)) {
                //do something
            } else {
                //do something else. I'll enable.
                map.remove(test1);
                map.put(test1, ENABLED);
            }
        }
    });

}

这篇关于在Swing中禁用GUI上的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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