gpt4 book ai didi

java SwingWorker 对话框未显示

转载 作者:行者123 更新时间:2023-12-02 13:21:53 26 4
gpt4 key购买 nike

在 Netbeans 中,我创建了一个 GUI 项目,该项目使用框架前端,我可以添加组件并双击以编辑其事件。我指的是具有“源”、“设计”和“历史”选项卡的窗口。

以下是组件和关系:

1- 打开文件选择器的按钮。

2- 显示文件选择器结果的文本区域。如果用户选择一个文件,它将在文本区域中显示文件名;否则会写“已被用户取消”。

3-同时,如果用户选择一个文件,我想打开一个“请稍候”对话框 SwingWorker 并在后台执行一些工作。

问题是,当用户选择文件时,我没有看到请等待对话框! Netbeans 生成的完整代码可在 pastebin 处获取。 。部分代码如下所示:

private void OpenSongFileActionPerformed(java.awt.event.ActionEvent evt) {                                              
// TODO add your handling code here:
JFileChooser fileChooser = new JFileChooser();
fileChooser.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter filter = new FileNameExtensionFilter("MP3 files", "mp3");
fileChooser.addChoosableFileFilter(filter);
fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
int result = fileChooser.showOpenDialog(this);
if (result != JFileChooser.APPROVE_OPTION) {
//ReadInfo.setText("No song has been selected");
System.out.println("OpenSongFile canceled by user");
return;
}
final JDialog loading = new JDialog(this);
JPanel p1 = new JPanel(new BorderLayout());
p1.add(new JLabel("Please wait..."), BorderLayout.CENTER);
loading.setUndecorated(true);
loading.getContentPane().add(p1);
loading.pack();
loading.setLocationRelativeTo(this);
loading.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
loading.setModal(true);
SwingWorker<String, Void> worker = new SwingWorker<String, Void>() {
@Override
protected String doInBackground() throws InterruptedException {
for (int i = 0; i < 10000; i++)
for (int j = 0; j < 10000; j++)
;
return "hello";
}
@Override
protected void done() {
loading.dispose();
}
};
worker.execute();
loading.setVisible(true);
try {
worker.get();
} catch (Exception e1) {
e1.printStackTrace();
}

File selectedFile = fileChooser.getSelectedFile();
ReadInfo.setText("Selected file: " + selectedFile.getAbsolutePath());
}

P.S:我使用了SwingWorker代码解释here .

最佳答案

worker.get(); 将阻塞,直到 SwingWorker 返回,这意味着您正在阻塞 EDT,从而阻止显示对话框。

相反,请利用 SwingWorkerPropertyChangeListener 支持并监视 STARTDONE 事件

SwingWorker<String, Void> worker = new SwingWorker<String, Void>() {
@Override
protected String doInBackground() throws InterruptedException {
Thread.sleep(5000);
return "hello";
}

@Override
protected void done() {
loading.dispose();
}
};
worker.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println(evt.getPropertyName());
Object value = evt.getNewValue();
if (value instanceof SwingWorker.StateValue) {
SwingWorker.StateValue state = (SwingWorker.StateValue) value;
switch (state) {
case DONE: {
try {
String result = worker.get();
JOptionPane.showMessageDialog(null, result);
} catch (InterruptedException | ExecutionException ex) {
ex.printStackTrace();
}
}
break;
}
}
}
});
worker.execute();
loading.setVisible(true);

另一个问题是您的循环可能运行得太快,以至于窗口在屏幕上实现之前就已关闭。

例如,我只是使用Thread.sleep来暂停doInBackground方法5秒钟...

import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

public class Memory extends JFrame {

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
makeItSo();
}
});
}

public static void makeItSo() {
final JDialog loading = new JDialog();
JPanel p1 = new JPanel(new BorderLayout());
p1.add(new JLabel("Please wait..."), BorderLayout.CENTER);
loading.setUndecorated(true);
loading.getContentPane().add(p1);
loading.pack();
loading.setLocationRelativeTo(null);
loading.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
loading.setModal(true);
SwingWorker<String, Void> worker = new SwingWorker<String, Void>() {
@Override
protected String doInBackground() throws InterruptedException {
Thread.sleep(5000);
return "hello";
}

@Override
protected void done() {
loading.dispose();
}
};
worker.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println(evt.getPropertyName());
Object value = evt.getNewValue();
if (value instanceof SwingWorker.StateValue) {
SwingWorker.StateValue state = (SwingWorker.StateValue) value;
switch (state) {
case DONE: {
try {
String result = worker.get();
JOptionPane.showMessageDialog(null, result);
} catch (InterruptedException | ExecutionException ex) {
ex.printStackTrace();
}
}
break;
}
}
}
});
worker.execute();
loading.setVisible(true);
}

}

关于java SwingWorker 对话框未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43536964/

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