我需要在点击时禁用JButton并在2秒后再次启用它,所以我已经尝试从事件处理程序中休眠ui线程,但这使得按钮处于选中状态,您无法读取该文本禁用按钮.
代码看起来像这样:
JButton button = new JButton("Press me");
button.addActionListener(new ActionListener{
public void actionPerformed(ActionEvent ae) {
JButton button = ((JButton)e.getSource());
button.setEnabled(false);
button.setText("Wait a second")
button.repaint();
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
}
button.setEnabled(true);
button.setText("");
}
Run Code Online (Sandbox Code Playgroud)
发生的事情是按钮保持在"被选中"状态,2秒没有文本,并立即禁用并重新启用按钮,这不是我想要的,我的目标是按钮保持禁用状态,并在其上显示文本两秒钟,然后重新启用.
我该怎么办?