我想在单击按钮时更改按钮文本,但它没有出现在 GUI 上。在intellij IDEA中我可以看到它已更改但为什么没有出现在GUI中? 这是代码片段:
final WebLabel loading = new WebLabel("Disconnected...", IconLib.ICON_19X17_THICK_ARROW_RIGHT_LIGHTBLUE.getIcon(), SwingConstants.CENTER);
final WebLabel ipLabel = new WebLabel(host);
final JPanel horizontalMiddlePanel = new JPanel();
final WebButton disconnect = new WebButton("Connect", IconLib.ICON_16X16_QUESTIONMARK_ON_BLUE_CIRCLE.getIcon());
disconnect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (loading.getText().equals("Connected...")) {
loading.setText("Disconnected...");
loading.setIcon(IconLib.ICON_19X17_THICK_ARROW_RIGHT_LIGHTBLUE.getIcon());
disconnect.setText("Connect");
} else {
loading.setText("test");
loading.setIcon(IconLib.ICON_19X17_THICK_ARROW_RIGHT.getIcon());
ipLabel.setText(ipLabel.getText().replace(" Unreachable try again",""));
ipLabel.setForeground(Color.green);
disconnect.setText("Connecting");
callflexConnection(ipLabel, 3001, loading, disconnect);
}
}
});
最佳答案
如果不将代码拆分为两部分就不可能
1) 更新JButton#setText
然后
2) 执行其余代码
- 通过使用
javax.swing.Timer
进行延迟 - 从
SwingWorker
执行 - 包裹在
Runnble#Thread
内,
3) 此代码在 EDT 上执行,然后所有更改都在 EDT 上完成,在同一时刻/同一时刻结束
关于java - 如何更改 JButton 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8357033/