问题描述
我正在使用JWindow在应用程序启动期间显示我的启动画面。但是它不会出现在所有窗口的前面,它也不会消失。
I am using JWindow to display my splash screen during the application start up. however it will not appear in front of all windows as it should, and it will not disappear as well.
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的主类中,我调用了我的启动画面
So in my main class which extends JFrame, I call my splash screen by
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
MySplash.getInstance().showSplash();
}
});
但是,JWindow出现在我计算机上所有打开的Windows实例后面。隐藏JWindow也不起作用。
However, the JWindow appears behind the all open instances of windows on my computer. Hiding the JWindow also doesn't work.
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
MySplash.getInstance().hideSplash();
}
});
推荐答案
你可能想看看。但是,如果您的解决方案是您的心脏:
You might want to take a look at java.awt.SplashScreen
. However, if your heart is set on your solution:
查看,
尝试将 JWindow
显示在前面。
我不确定为什么你的 JWindow
没有隐藏,这部分适合我。
I'm not sure why your JWindow
isn't hiding, that part works for me.
/ e1
如果您正在尝试实施,应该使您的构造函数和字段私有。您可能还想查看。
/e1
If you're trying to implement a singleton pattern, you should make your constructor and field private. You might also want to take a look at What is an efficient way to implement a singleton pattern in Java?.
/ e2
返回
s showSplash
和 hideSplash
方法是不必要的,无论如何该方法都会返回。
/e2
The return
s at the end of your showSplash
and hideSplash
methods are unnecessary, the method would have returned at that point anyways.
这篇关于Java Swing:JWindow出现在所有其他进程窗口后面,并且不会消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!