【发布时间】:2017-04-07 23:29:24
【问题描述】:
我有一个 JButton,我们称它为“按钮”并向其添加一个 ActionListener:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
call();
}
});
它已正确添加到我的框架等中。在那个 JFrame 中,我还有一个 JLabel,我想在 JButton 方法工作时更改它的文本(因为它需要大约 30 秒才能完成)。我怎么做?我必须使用一些多线程的东西吗? 这是基本原理(JLabel称为输出):
public void call(){
output.setText("test1");
try { Thread.sleep(1000);
} catch (InterruptedException e) {}
output.setText("test2");
}
这将导致“输出”标签在一秒钟后更改为“test2”。如何让它立即显示“test1”?
【问题讨论】:
-
删除
output.setText("test1");之后的所有内容...
标签: java multithreading swing jbutton jlabel