gpt4 book ai didi

java - 无法在线程内更新 JDialog GUI

转载 作者:行者123 更新时间:2023-11-29 03:35:53 24 4
gpt4 key购买 nike

请看下面的代码

private class EmergencyAlertNotifier implements Runnable, ActionListener
{
JDialog dialog = new JDialog();
int number=0;

JLabel message;
JButton yes,no;

String messageStr;

public EmergencyAlertNotifier()
{
dialog.setLayout(new BorderLayout());

//The JLabel which will display the number of seconds left
//before alerting emergency services
message = new JLabel();


messageStr="number";

yes = new JButton("OK");
yes.addActionListener(this);
no = new JButton("Cancel");
no.addActionListener(this);

JPanel btnPanel = new JPanel();
btnPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
btnPanel.add(yes);
btnPanel.add(no);

dialog.add(message,"Center");
dialog.add(btnPanel,"South");

dialog.setTitle("Ready To Notify Emergency Fire Services");
dialog.setVisible(true);
dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}

@Override
public void run()
{
for(int i=10;i>0;i--)
{
message.setText(messageStr+i+" Sec.");


try
{
Thread.sleep(1000);
}
catch(Exception e)
{
e.printStackTrace();
}
}


}

@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==yes)
{

}
else
{
dialog.dispose();
}
}

}

线程在上面的类之外启动

 new Thread(new EmergencyAlertNotifier()).start();

我正在尝试使用线程内不断变化的数字来更新 JLabel。但是,JLabel 并没有出现在 GUI 中。这是为什么?请帮忙!

最佳答案

  • 您的代码中存在一些问题,Concurency in Swing

  • 我不能以某种方式发表评论,

  • 直到 SwingWorker 将在官方 API 中实现,Runnable#ThreadWorkers Thread 的标准解决方法,

    /li>
  • 生产代码使用 Runnable#Thread 而不是 SwingWorker(我的观点,不同意 API 中实现的黑洞)

  • Runnable#Thread 到 Swing GUI 的输出需要使用 invokeLater,用于 thread_safe (setText, append..., 特别是Java7和Java6)方法也有变化

  • 使用 util.Timer 进行 count_down,而不是 Thread.sleep()

修改后的代码,按预期工作

import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class EmergencyAlertNotifier implements Runnable, ActionListener {

private JDialog dialog = new JDialog();
private int number = 0;
private JLabel message;
private JButton yes, no;
private String messageStr;
private boolean runProcess = true;

public EmergencyAlertNotifier() {
dialog.setLayout(new GridLayout());
//The JLabel which will display the number of seconds left
//before alerting emergency services
message = new JLabel();
messageStr = "number";
yes = new JButton("OK");
yes.addActionListener(this);
no = new JButton("Cancel");
no.addActionListener(this);
JPanel btnPanel = new JPanel();
btnPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
btnPanel.add(yes);
btnPanel.add(no);
dialog.add(message, "Center");
dialog.add(btnPanel, "South");
dialog.setTitle("Ready To Notify Emergency Fire Services");
dialog.pack();
dialog.setVisible(true);
dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
new Thread(this).start();
}

@Override
public void run() {
while (runProcess) {
for (int i = 10; i > 0; i--) {
message.setText(messageStr + " " + i + " Sec.");
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
runProcess = false;
}
}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == yes) {
} else {
dialog.dispose();
}
}

public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
EmergencyAlertNotifier ean = new EmergencyAlertNotifier();
}
});
}
}

关于java - 无法在线程内更新 JDialog GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15583900/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com