Java Swing 实用程序

编程入门 行业动态 更新时间:2024-10-28 21:21:59
本文介绍了Java Swing 实用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I want to create a loading message that will popup on the screen while information is being loaded. I will call the initLoadingPanel() method for it so that the JFrame will be visible. My problem is how can I close it?

我的代码如下.

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)>,这就提出了一个问题,你是如何加载你的资源的?

Generally, you should only need to call loader.dispose() or loader.setVisible(false), this then raises the question of, how are you loading your resources?

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

You will, likely, need to pass a reference of loader to the this part of your code, so when it's finished, you can dispose of the frame.

因为框架被装饰,用户可以简单地点击[x]"按钮并关闭窗口,而你可以将框架defaultCloseOperation设置为DO_NOTHING_ON_CLOSE,看起来还是很奇怪.

Because the frame is decorated, the user could simply hit the "[x]" button and close the window, while you can set the frames defaultCloseOperation to DO_NOTHING_ON_CLOSE, it still looks weird.

您可以使用 JFrame#setUndecorated

因为 loader 从 JFrame 扩展,用户仍然可以与父窗口交互(如果一个可见),更好的解决方案可能是使用 JDialog 改为模态.

Because loader extends from JFrame it's still possible for the user to interact with the parent window (if one is visible), a better solution might be to use a JDialog instead and make it modal.

您也可以考虑查看如何创建飞溅筛选一些其他想法

You might also consider having a look at How to Create a Splash Screen for some other ideas

更新

你正在隐藏你的变量......

You're shadowing your variables...

首先声明 loader 作为 DataMigration

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

然后在 Runnable....

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

这说明instance字段还是null,试试...

This means that the instance field is still null, try...

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 框架会出现,但很可能不会,因为你正在阻止事件调度线程.

Isn't going to do what you think it should...you "might" get lucky and the loader frame will appear, but it's likely that it won't because you're blocking the Event Dispatching Thread.

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

Instead, you should consider using a 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 是什么类型,所以我猜...

(I don't know what type cvs is, so I'm guessing...

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

This will ensure that your UI will remain responsive while the data is loaded...

看看Swing 中的并发

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

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 实用程序

本文发布于:2023-10-26 12:56:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530206.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实用程序   Java   Swing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!