Java Swing : JWindow appears behind all other process windows, 并且不会消失

标签 java swing splash-screen jwindow

我正在使用 JWindow 在应用程序启动期间显示启动画面。但是它不会出现在所有窗口前,也不会消失。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;

public class MySplash {
    public static MySplash INSTANCE;
    private static JWindow jw;

    public MySplash(){
        createSplash();
    }

    private void createSplash() {
        jw = new JWindow();
        JPanel content = (JPanel) jw.getContentPane();
        content.setBackground(Color.white);

        // Set the window's bounds, centering the window
        int width = 328;
        int height = 131;
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (screen.width - width) / 2;
        int y = (screen.height - height) / 2;
        jw.setBounds(x, y, width, height);

        // Build the splash screen
        JLabel label = new JLabel(new ImageIcon("splash.jpg"));
        JLabel copyrt = new JLabel("SplashScreen Test",
            JLabel.CENTER);
        copyrt.setFont(new Font("Sans-Serif", Font.BOLD, 12));
        content.add(label, BorderLayout.CENTER);
        content.add(copyrt, BorderLayout.SOUTH);
        Color oraRed = new Color(156, 20, 20, 255);
        content.setBorder(BorderFactory.createLineBorder(oraRed, 0));

    }

    public synchronized static MySplash getInstance(){
        if(INSTANCE==null){
            INSTANCE = new MySplash();
        }

        return INSTANCE;

    }

    public void showSplash(){
        jw.setAlwaysOnTop(true);
        jw.toFront();
        jw.setVisible(true);
        return;
    }

    public void hideSplash(){
        jw.setAlwaysOnTop(false);
        jw.toBack();
        jw.setVisible(false);
        return;
    }
}

所以在我扩展 JFrame 的主类中,我通过以下方式调用启动画面

    SwingUtilities.invokeLater(new Runnable(){

        @Override
        public void run() {
            MySplash.getInstance().showSplash();
        }

    });

但是,JWindow 出现在我计算机上所有打开的窗口实例的后面。隐藏 JWindow 也不起作用。

    SwingUtilities.invokeLater(new Runnable(){

        @Override
        public void run() {
            MySplash.getInstance().hideSplash();
        }

    });

最佳答案

您可能想看看 java.awt.SplashScreen .但是,如果您对解决方案有信心:

查看Window#toFront ,

If this Window is visible, brings this Window to the front and may make it the focused Window.

尝试让您的 JWindow 可见,然后再将它放在前面。

我不确定为什么你的 JWindow 没有隐藏,那部分对我有用。

/e1
如果您正在尝试实现 singleton pattern ,您应该将构造函数和字段设为私有(private)。您可能还想看看 What is an efficient way to implement a singleton pattern in Java? .

/e2
showSplashhideSplash 方法末尾的 return 是不必要的,该方法无论如何都会在该点返回。

关于Java Swing : JWindow appears behind all other process windows, 并且不会消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8320179/

相关文章:

android - PhoneGap Build - 为 Android 设备设置闪屏

java - 如何在 Java 中表达数组的类文字?

java - 如何通过在 Try-Catch While 循环中输入特定字母来退出程序?

Mac OS X 上带有键盘输入的 Java Swing 全屏

java - 如何聚焦空组合框

java - JPanel透明度问题

iOS:当应用程序进入后台调用电话时,如何防止启动画面显示?

ios - 启动屏幕后 UISCrollView 的奇怪行为

java - TableView删除数据后如何刷新?

java - 截断为整数...以及 if 语句确定它是否不是整数