java splashwindow

本文介绍了一个用于显示启动画面的Java程序实现,通过自定义的PBSUISplashWindow类创建启动窗口,并在窗口中显示图片和加载状态文字。文章详细展示了如何设置窗口的位置、大小以及如何在图片上覆盖文字。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package util;

import java.awt.*;
import javax.swing.*;
import main.CMainApp;

/**
 * A class used to display a splashwindow on the screen while the program
 * is starting up. A message can be displayed on top of the image
 * in the lower right corner.
 */

final public class PBSUISplashWindow
    extends JWindow {

  /**
   * Constant handle to the glass pane that handles drawing text
   * on top of the splash screen.
   */
  private static final SplashGlassPane GLASS_PANE =
      new SplashGlassPane();

  /**
   * Creates a new SplashWindow, setting its location, size, etc.
   */
  public PBSUISplashWindow(String SplashImageFileName) {
    ImageIcon splashIcon = new ImageIcon(SplashImageFileName);

    Image image = splashIcon.getImage();
    Dimension size = new Dimension(image.getWidth(null) + 2,
                                   image.getHeight(null) + 2);
    this.setSize(size);

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setLocation( (screenSize.width - size.width) / 2,
                (screenSize.height - size.height) / 2);
    JLabel splashLabel = new JLabel(splashIcon);
    splashLabel.setBorder(BorderFactory.createLineBorder(Color.black, 1));

    this.setGlassPane(GLASS_PANE);
    this.getContentPane().add(splashLabel, BorderLayout.CENTER);
    this.pack();
    this.setVisible(true);
    GLASS_PANE.setVisible(true);
  }

  /**
   * Sets the loading status text to display in the splash
   * screen window.
   *
   * @param text the text to display
   */
  public static void setStatusText(String text) {
    GLASS_PANE.setText(text);
  }

  /**
   * A private glass for the glass pane that handles drawing
   * status text above the background image.
   */
  private static final class SplashGlassPane
      extends JPanel {

    /**
     * Handle for the panel that contains the text.
     */
    private static JPanel TEXT_PANEL = new JPanel();

    /**
     * The label for the text.
     */
    private static JLabel TEXT_LABEL = new JLabel();

    /**
     * Constructor lays out the panels and label, sets them to
     * be transparent, etc.
     */
    private SplashGlassPane() {
      this.setOpaque(false);
      this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
      this.add(Box.createVerticalGlue());
      TEXT_PANEL.setOpaque(false);
      TEXT_PANEL.setLayout(new BoxLayout(TEXT_PANEL, BoxLayout.X_AXIS));
      TEXT_PANEL.add(Box.createHorizontalGlue());
      // make sure label doesn't clip....
      TEXT_LABEL.setMinimumSize(new Dimension(40, 3));
      TEXT_PANEL.add(TEXT_LABEL);
      //TEXT_PANEL.add(a horizontal separator component);
      this.add(TEXT_PANEL);
      //this.add(a vertical separator component);
    }

    /**
     * Sets the text to display for the status area.
     *
     * @param text the text to display
     */
    private static void setText(String text) {
      FontMetrics fm = TEXT_LABEL.getFontMetrics(TEXT_LABEL.getFont());
      TEXT_LABEL.setPreferredSize(new Dimension(fm.stringWidth(text),
                                                fm.getHeight()));
      TEXT_LABEL.setText(text);
    }
  }

  public static void main(String[] args) {
    PBSUISplashWindow window = new PBSUISplashWindow("D://PMM2005//PMMGUI//res//splash.jpg");
    window.setStatusText("SplashWindow Test ");
    try {
      Thread.sleep(5000);
    }
    catch (InterruptedException ex) {
    }
    window.dispose();
    window = null;
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值