gpt4 book ai didi

java - 如何使用 SwingWorker(外部类)触发对话框

转载 作者:行者123 更新时间:2023-12-02 00:40:08 25 4
gpt4 key购买 nike

我一直在努力熟悉 Java 中 SwingWorkers 的使用。是否可以在 SwingWorker 的处理方法中从 GUI 触发显示对话框?

最佳答案

within a SwingWorker's process method

如果你的意思是:

a) 在 doInBackground() 内,

public Boolean doInBackground() {

// caught exception half way e.g. login auth fail

// 'push' into GUI layer (EDT)
String text = "Your error here";
publish(text);

Boolean result = Boolean.TRUE;

// continue with remaining process

return result;
}

protected void process(List<Object> chunks) {
String[] message = chunks.toArray(new String[chunks.size()]);

// prompt user with dialog box
JDialogBox.showMessageDialog(window, message);
}
<小时/>

b) 在done()内,

protected void done() {
// prompt user with dialog box
String message;
try {
message = get();
} catch (Exception e) {
e.printStackTrace();
}
JDialogBox.showMessageDialog(window, message);
}
<小时/>

c) 还是在 get() 之外?

SwingWorker sw = new SwingWorker() {.....;
sw.execute();

// some time later

final String message;
try {
message = sw.get();
} catch (Exception e) {
e.printStackTrace();
}

Runnable edt = new Runnable() {

public void run() {
JDialogBox.showMessageDialog(window, message);
}
}

java.awt.EventQueue.invokeLater(edt);

关于java - 如何使用 SwingWorker(外部类)触发对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6631293/

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