如何在 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(); } } }
輸出
廣告