gpt4 book ai didi

Java Swing 实用程序

转载 作者:行者123 更新时间:2023-12-01 17:09:15 25 4
gpt4 key购买 nike

我想创建一条加载消息,该消息在加载信息时会在屏幕上弹出。我将为其调用 initLoadingPanel() 方法,以便 JFrame 可见。我的问题是如何关闭它?

我的代码如下。

public class DataMigration extends JFrame{

private JFrame frmDataMigration;

private JFrame loader;

private JButton btnProcess;

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DataMigration window = new DataMigration();
window.frmDataMigration.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

public DataMigration() {
initialize();
}

private void initialize() {

LoggerImp.startLog(CLASS_NAME, "initialize()");

frmDataMigration = new JFrame();

btnProcess = new JButton("Load");
btnProcess.setEnabled(false);
btnProcess.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

SwingWorker <CSV, CSV> worker = new SwingWorker<CSV, CSV>() {

@Override
protected CSV doInBackground() throws Exception {
return FileReaderCSVHelper.fileReader(dirName.getText(), fileName.getText());
}

@Override
protected void done() {
try {
csv = doInBackground();
generateTableList(csv.getCsvTitle(), stateController.getFieldNames());
} catch (ExecutionException ex) {
ex.printStackTrace();
} catch (Exception e){

}
loader.dispose();
}

};
worker.execute();
}
});
frmDataMigration.getContentPane().add(btnProcess);
}

public void initLoadingPanel(){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
loader = new JFrame("Loading....");
ImageIcon img = new ImageIcon("loader.gif");
loader.add(new JLabel(" Loading...", img, JLabel.CENTER));

loader.setAlwaysOnTop(true);
loader.pack();
loader.setSize( 448, 497);
loader.setVisible(true);
loader.setLocationRelativeTo(null);
}
});
}

最佳答案

通常,您只需要调用 loader.dispose()loader.setVisible(false),这就提出了一个问题,您如何加载您的资源?

您可能需要将 loader 的引用传递给代码的这一部分,以便在完成后,您可以处理该框架。

由于框架经过装饰,用户只需点击“[x]”按钮即可关闭窗口,同时您可以将框架 defaultCloseOperation 设置为 DO_NOTHING_ON_CLOSE,看起来还是很奇怪。

您可以使用 JFrame#setUndecorated 删除框架装饰

因为 loaderJFrame 扩展而来,用户仍然可以与父窗口交互(如果父窗口可见),更好的解决方案可能是使用 JDialog 改为模态。

您也可以考虑查看 How to Create a Splash Screen一些其他的想法

已更新

您正在隐藏变量...

首先,将 loader 声明为 DataMigration 的实例字段

public class DataMigration extends JFrame{
//...
private JFrame loader;

但是然后在 Runnablerun 方法中重新声明它....

SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame loader = new JFrame("Loading....");

这意味着实例字段仍然为空,尝试...

SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
loader = new JFrame("Loading....");

相反...

还有...

public void actionPerformed(ActionEvent arg0) {
initLoadingPanel();
csv = FileReaderCSVHelper.fileReader(dirName.getText(), fileName.getText());
generateTableList(csv.getCsvTitle(), stateController.getFieldNames());
loader.dispose();
}

不会做你认为应该做的事情...你“可能”很幸运,loader框架会出现,但很可能不会,因为你正在阻止事件调度线程。

相反,您应该考虑使用 SwingWorker....

initLoadingPanel();
SwingWorker worker = new SwingWorker<CVS, CVS>() {

@Override
protected CVS doInBackground() throws Exception {
return FileReaderCSVHelper.fileReader(dirName.getText(), fileName.getText());
}

@Override
protected void done() {
try {
cvs = get();
generateTableList(csv.getCsvTitle(), stateController.getFieldNames());
} catch (ExecutionException ex) {
ex.printStackTrace();
}
loader.dispose();
}

};
worker.execute();

(我不知道 cvs 是什么类型,所以我猜测......

这将确保您的 UI 在加载数据时保持响应...

看看Concurrency in Swing

更新一个工作示例......

Example

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ExecutionException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;

public class DataMigration extends JFrame {

private JFrame frmDataMigration;

private JFrame loader;

private JButton btnProcess;

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DataMigration window = new DataMigration();
window.frmDataMigration.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

public DataMigration() {
initialize();
}

private void initialize() {

frmDataMigration = new JFrame();

btnProcess = new JButton("Load");
btnProcess.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
initLoadingPanel();
SwingWorker worker = new SwingWorker() {

@Override
protected Object doInBackground() throws Exception {
Thread.sleep(5000);
return "This is a test value used to highlight the example";
}

@Override
protected void done() {
try {
get();
} catch (ExecutionException ex) {
} catch (InterruptedException ex) {
}
loader.dispose();
btnProcess.setEnabled(true);
}

};
worker.execute();
btnProcess.setEnabled(false);
}
});
frmDataMigration.getContentPane().add(btnProcess);
frmDataMigration.setDefaultCloseOperation(EXIT_ON_CLOSE);
frmDataMigration.pack();
frmDataMigration.setLocationRelativeTo(null);
}

public void initLoadingPanel() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
loader = new JFrame("Loading....");
ImageIcon img = new ImageIcon("loader.gif");
loader.add(new JLabel(" Loading...", img, JLabel.CENTER));

loader.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
loader.setAlwaysOnTop(true);
loader.pack();
loader.setSize(448, 497);
loader.setVisible(true);
loader.setLocationRelativeTo(null);
}
});

}
}

关于Java Swing 实用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24379667/

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