Java Swing实用程序

new*_*bie 0 java swing

我想创建一个加载消息,在加载信息时将在屏幕上弹出.我将为它调用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);
         }
     });
}
Run Code Online (Sandbox Code Playgroud)

Mad*_*mer 5

一般来说,你应该只需要打电话,loader.dispose()或者loader.setVisible(false)这就提出了一个问题,你如何加载你的资源?

您可能需要传递对loader代码的这一部分的引用,所以当它完成后,您可以处理该帧.

由于框架装饰,用户可以简单地点击"[X]"按钮,关闭窗口,同时可以设置帧defaultCloseOperationDO_NOTHING_ON_CLOSE,它仍然看起来怪怪的.

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

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

您还可以考虑查看如何为其他一些想法创建启动画面

更新

你正在掩盖你的变量......

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

public class DataMigration extends JFrame{
    //...
    private JFrame loader;
Run Code Online (Sandbox Code Playgroud)

但是然后在run你的方法中重新声明它Runnable....

SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        JFrame loader = new JFrame("Loading....");
Run Code Online (Sandbox Code Playgroud)

这意味着实例字段仍为null,请尝试...

SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        loader = new JFrame("Loading....");
Run Code Online (Sandbox Code Playgroud)

代替...

也...

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

不会做你认为应该做的事情......你"可能"变得幸运并且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();
Run Code Online (Sandbox Code Playgroud)

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

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

看看Swing中的Concurrency

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

例

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);
            }
        });

    }
}
Run Code Online (Sandbox Code Playgroud)