当闪屏可见时,使主JFrame不可见

时间:2015-07-29 13:38:30

标签: java swing splash

我有这个createAndShowGui()类,它从另一个方法调用启动画面,并创建一个用于主应用程序的JFrame。我一直试图弄清楚如何在启动画面可见时隐藏JFrame。

我尝试使用

if (splash.isVisible()){
    frame.setVisible(false)
}
else { frame.setVisible(true)
}

这不起作用。什么是实现这一目标的最佳方式?

private static void createAndShowGui() {
    DrawPanelMain mainPanel = new DrawPanelMain();
    JFrame frame = new JFrame("RF Connection Panel " + version);
    SwingMenu mainBar = new SwingMenu();
    Splash splash = new Splash("C:\\Users\\Rob\\splashscreen.gif", frame, 8000);
    splash.setVisible(true);

    frame.setJMenuBar(mainBar);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.getContentPane().add(mainPanel);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

}

Splash.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/*
 * This class creates a splash screen that is displayed when the program is run.  It includes thread management,
 * mouse adapter, and runs with a JWindow on top of the JFrame for the overall application.
 */
public class Splash extends JWindow {
    public Splash(String filename, Frame f, int waitTime) {
        super(f);
        JLabel label = new JLabel(new ImageIcon(filename));
        getContentPane().add(label, BorderLayout.CENTER);
        pack();

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension labelSize = label.getPreferredSize();
        setLocation(screenSize.width / 2 - (labelSize.width / 2), screenSize.height / 2 - (labelSize.height / 2));

        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e){
                setVisible(false);
                dispose();
            }
        });

        final int pause = waitTime;
        final Runnable closerRunner = new Runnable() {

            public void run() {
                setVisible(false);
                dispose();
            }
        };

        Runnable waitRunner = new Runnable() {
            public void run(){

            try {
                Thread.sleep(pause);
                SwingUtilities.invokeAndWait(closerRunner);
            }
            catch (Exception e){
                JOptionPane.showMessageDialog(null, e.getStackTrace());
            }
        }
    };

        setVisible(true);
        Thread splashThread = new Thread(waitRunner, "SplashThread");
        splashThread.start();
    }
}

1 个答案:

答案 0 :(得分:2)

编辑:创意2

由于您已将帧传递给Splash对象,因此您可以使用隐藏Splash的相同方法使其可见。

首先,为框架添加一个实例变量,以便所有方法都可以访问它并将其分配给构造函数参数。

public class Splash extends JWindow {
    private JFrame frame;
    public Splash(String filename, Frame f, int waitTime) {
       super(f);
       this.frame = f;

然后,在隐藏“启动画面”屏幕的方法中,隐藏框架。

addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent e){
        setVisible(false);
        this.frame.setVisible(true); 
        dispose();
       }
   });

最后..

 final Runnable closerRunner = new Runnable() {
     public void run() {
         setVisible(false);
         this.frame.setVisible(true);
         dispose();
     }
 };