Java Swing: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

如果此窗口可见,则将此窗口置于前面,并使其成为焦点窗口。

在将JWindow带到前面之前尝试使其可见。

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

/ E1
如果您正在尝试实现单例模式 ,则应将构造函数和字段设为私有。 您可能还想了解在Java中实现单例模式的有效方法是什么? 。

/ E2
showSplashhideSplash方法结束时的showSplash hideSplash是不必要的,无论如何该方法都会返回。