Java Swing 实用程序 [英] Java Swing Utilities

查看:41
本文介绍了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

因为 loaderJFrame 扩展,用户仍然可以与父窗口交互(如果一个可见),更好的解决方案可能是使用 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 实用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆