jsplashwindow.java

来自「java2参考大全上的例子的源码和自己的理解.」· Java 代码 · 共 97 行

JAVA
97
字号
package JSplashWindow;

//JSplashWindow.java
//Demos how to show a splash window

import javax.swing.*;
import java.awt.*;
import java.net.*;

public class JSplashWindow
    extends JWindow
    implements Runnable {
  Thread splashThread = null;
  public JSplashWindow() {
    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    JPanel splash = new JPanel(new BorderLayout());
    //D:\JAVA Programe\JSplashWindow\classes
    /*
         Class URL represents a Uniform Resource Locator, a pointer to a "resource" on the
         World Wide Web. A resource can be something as simple as a file or a directory,
         or it can be a reference to a more complicated object, such as a query to a database
         or to a search engine.
     */
    URL url = getClass().getResource("/images/winter.jpg");
    if (url != null) {
      /*
           An implementation of the Icon interface that paints Icons from Images.
           Images that are created from a URL or filename are preloaded using MediaTracker to
           monitor the loaded state of the image.
       */
      /*
           ImageIcon(URL\u00A0location) \u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0Creates an ImageIcon from the specified URL.
       */
      splash.add(new JLabel(new ImageIcon(url)),BorderLayout.CENTER);
    }
    /*
         public void setContentPane(Container\u00A0contentPane)Sets the contentPane property
         for this window. This method is called by the constructor.
         The content pane displays all opened files in a project as a set of tabs.
        To open a file in the content pane, double-click it in the project pane,
        or select it and press Enter. The name of each opened file is displayed on a tab in the
        content pane. When you click a tab, that file becomes the current file.
    */
    setContentPane(splash);

    Dimension screen = getToolkit().getScreenSize();
    pack();
    setLocation( (screen.width - getSize().width) / 2,
                (screen.height - getSize().height) / 2);
  }

  public void start() {
    this.toFront();
    splashThread = new Thread(this);
    splashThread.start();

  }

  public void run() {
    try {
      show();
      Thread.sleep(5000);
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    this.dispose();
  }

  static void showFrame(String title) {
    JFrame frame = new JFrame(title);
    frame.setSize(400, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Center the window
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = frame.getSize();
    if (frameSize.height > screenSize.height) {
      frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
      frameSize.width = screenSize.width;
    }
    frame.setLocation( (screenSize.width - frameSize.width) / 2,
                      (screenSize.height - frameSize.height) / 2);

    frame.setVisible(true);

  }

  public static void main(String[] args) {
    showFrame("Demo splash window");
    JSplashWindow splash = new JSplashWindow();
    splash.start();

  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?