如何在 Java 中使用 JWindow 实现一个闪屏?\n
JWindow 是一个可显示在用户桌面的任意位置的容器。它没有像 JFrame 那样的标题栏、窗口管理按钮等。
JWindow 包含一个JRootPane 作为其唯一的子类。contentPane 可以作为JWindow的任何子元素的父元素。与JFrame类似,JWindow 是另一个顶级容器,并且它是一个没有装饰的 JFrame。它不具备标题栏、窗口菜单等功能。JWindow 可作为一个闪屏窗口,在应用程序启动时显示一次,然后在几秒钟后自动消失。
示例
import javax.swing.*; import java.awt.*; public class CreateSplashScreen extends JWindow { Image splashScreen; ImageIcon imageIcon; public CreateSplashScreen() { splashScreen = Toolkit.getDefaultToolkit().getImage("C:/Users/User/Desktop/Java Answers/logo.jpg"); // Create ImageIcon from Image imageIcon = new ImageIcon(splashScreen); // Set JWindow size from image size setSize(imageIcon.getIconWidth(),imageIcon.getIconHeight()); // Get current screen size Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); // Get x coordinate on screen for make JWindow locate at center int x = (screenSize.width-getSize().width)/2; // Get y coordinate on screen for make JWindow locate at center int y = (screenSize.height-getSize().height)/2; // Set new location for JWindow setLocation(x,y); // Make JWindow visible setVisible(true); } // Paint image onto JWindow public void paint(Graphics g) { super.paint(g); g.drawImage(splashScreen, 0, 0, this); } public static void main(String[]args) { CreateSplashScreen splash = new CreateSplashScreen(); try { // Make JWindow appear for 10 seconds before disappear Thread.sleep(10000); splash.dispose(); } catch(Exception e) { e.printStackTrace(); } } }
输出
广告