简体   繁体   English

如何在一段时间内禁用JButton?

[英]How to disable a JButton for certain period of time?

I would like to disable a JButton for about 10 seconds. 我想禁用JButton约10秒钟。 Is there way to do this? 有办法吗?

Thank you 谢谢

Use a Swing Timer , when triggered, it notifies the registered listener within the context of the Event Dispatching Thread, making it safe to update the UI from. 使用Swing Timer ,当触发时,它将在事件调度线程的上下文中通知已注册的侦听器,从而可以安全地从中更新UI。

See How to use Swing Timers and Concurrency in Swing for more details 有关更多详细信息,请参见如何 在Swing中 使用Swing计时器并发。

First read the answer from @MadProgrammer and go through the links provided there. 首先阅读@MadProgrammer的答案,并通过那里提供的链接。 If you still need a working example based on those suggestions, following is one. 如果您仍然需要基于这些建议的可行示例,请参考以下示例。

why the solution is better than few solutions presented 为什么解决方案比提出的几种解决方案更好

It's because it uses a javax.swing.Timer to enable the button that enables GUI related tasks to be automatically executed on the event-dispatch thread (EDT). 这是因为它使用javax.swing.Timer来启用按钮,该按钮使GUI相关任务可以在事件调度线程(EDT)上自动执行。 This saves the swing application from being intermixed with non EDT operations. 这样可以避免将swing应用程序与非EDT操作混合使用。

Please try the following example: 请尝试以下示例:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class SwingDemo extends JPanel {
    private final JButton button;
    private final Timer stopwatch;
    private final int SEC = 10;

    public SwingDemo() {
        button = new JButton("Click me to disable for " + SEC + " secs");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JButton toDisable = (JButton) e.getSource();
                toDisable.setEnabled(false);
                stopwatch.start();
            }
        });
        add(button);
        stopwatch = new Timer(SEC * 1000, new MyTimerListener(button));
        stopwatch.setRepeats(false);
    }

    static class MyTimerListener implements ActionListener {
        JComponent target;

        public MyTimerListener(JComponent target) {
            this.target = target;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            target.setEnabled(true);
        }

    }

    public static void main(String[] args) {
        final JFrame myApp = new JFrame();
        myApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myApp.setContentPane(new SwingDemo());
        myApp.pack();
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                myApp.setVisible(true);
            }
        });
    }
}

您可以使用ThreadTask或更简单的Timer类。

you can use Thread.sleep(time in mil seconds) 您可以使用Thread.sleep(时间以毫秒为单位)

ex: Thread.sleep(10000); 例如:Thread.sleep(10000); // sleep for 10 seconds //睡眠10秒

JButton button = new JButton("Test");

    try {
        button.setEnabled(false);
        Thread.sleep(10000);
        button.setEnabled(true);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

but it must be in a separate thread or it will make all the GUI hang for 10 seconds. 但它必须在单独的线程中,否则会使所有GUI挂起10秒钟。

you can post more details about the code and i can help 您可以发布有关代码的更多详细信息,我可以提供帮助

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

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