Java中的Splashscreen

Java中的Splashscreen,java,swing,text,splash-screen,Java,Swing,Text,Splash Screen,我需要找出在这段代码中我需要调整它的位置,将“SplashScreen!!!”一行移到屏幕的中间,并可能使它变大。我不确定这在代码中的位置,这让我发疯 import java.awt.*; import javax.swing.*; public class SplashScreen extends JWindow { private int duration; public SplashScreen(int d) { duration = d; } // A simple li

我需要找出在这段代码中我需要调整它的位置,将“SplashScreen!!!”一行移到屏幕的中间,并可能使它变大。我不确定这在代码中的位置,这让我发疯

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

public class SplashScreen extends JWindow {

private int duration;

public SplashScreen(int d) {
    duration = d;
}

// A simple little method to show a title screen in the center
// of the screen for the amount of time given in the constructor
public void showSplash() {

    JPanel content = (JPanel)getContentPane();
    content.setBackground(Color.blue);

    // Set the window's bounds, centering the window
    int width = 700;
    int height = 450;
    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (screen.width-width)/2;
    int y = (screen.height-height)/2;
    setBounds(x,y,width,height);

    // Build the splash screen
    JLabel label = new JLabel(new ImageIcon("java-tip.gif"));
    JLabel copyrt = new JLabel
        ("Splash Screen!!!", 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(200, 50, 20, 255);
    content.setBorder(BorderFactory.createLineBorder(oraRed, 10));

    // Display it
    setVisible(true);

    // Wait a little while, maybe while loading resources
    try { Thread.sleep(duration); } catch (Exception e) {}

    setVisible(false);
}
public void showSplashAndExit() {
    showSplash();
    System.exit(0);

}

public static void main(String[] args) {

    // Throw a nice little title page up on the screen first
    SplashScreen splash = new SplashScreen(10000);

    // Normally, we'd call splash.showSplash() and get on
    // with the program. But, since this is only a test...
    splash.showSplashAndExit();
}
}

我不知道为什么,但这个论坛中的“添加代码”功能总是让它看起来很奇怪,而且没有正确缩进。

有几种方法可以做到这一点,但让我们保持简单

基本上,这样做是将
标签
(背景)图像添加到内容窗格的中心位置。然后,它将
边框布局
应用于
标签
,并将
copyrt
添加到
标签
的中心位置

public void showSplash() {

    JPanel content = (JPanel) getContentPane();
    content.setBackground(Color.blue);

    // Set the window's bounds, centering the window
    int width = 700;
    int height = 450;
    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (screen.width - width) / 2;
    int y = (screen.height - height) / 2;
    setBounds(x, y, width, height);

    // Build the splash screen
    JLabel label = new JLabel(new ImageIcon("java-tip.gif"));
    JLabel copyrt = new JLabel("Splash Screen!!!", JLabel.CENTER);

    content.add(label, BorderLayout.CENTER);

    // Fun starts here...    
//        copyrt.setFont(new Font("Sans-Serif", Font.BOLD, 12));
//        content.add(copyrt, BorderLayout.SOUTH);

    label.setLayout(new BorderLayout());
    Font font = copyrt.getFont();
    copyrt.setFont(font.deriveFont(Font.BOLD, 24f));
    label.add(copyrt);

    Color oraRed = new Color(200, 50, 20, 255);
    content.setBorder(BorderFactory.createLineBorder(oraRed, 10));

    // Display it
    setVisible(true);

    // Don't do this, as it will cause the EDT to be stopped.  Instead
    // setup some kind of callback that can tell when the resources are 
    // loaded and start the rest of the application from there...
    // Wait a little while, maybe while loading resources
    //try {
    //    Thread.sleep(duration);
    //} catch (Exception e) {
    //}

    //setVisible(false);
}
在EDT的上下文中执行任何可能阻止它处理进一步事件的操作(如使用
sleep
或长时间运行/无限循环)也是一个坏主意

所有UI交互都应该在EDT上下文中执行,所有长时间运行或可能阻塞的任务都应该在单独的线程上运行

请查看以了解更多详细信息

已更新

如果我尝试运行您的示例,启动屏幕将永远不会出现,因为
Thread.sleep
阻止在屏幕上显示。您“可能”让它工作的事实实际上是侥幸,因为您可能是从“主”线程而不是EDT调用
showSplash
方法

相反,如果我将
Thread.sleep
替换为
SwingWorker
,我不仅可以显示初始屏幕,而且还可以控制进度更新和处理初始屏幕的计划

例如

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SplashDemo extends JWindow {

    public static void main(String[] args) {
        new SplashDemo();
    }

    public SplashDemo() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                showSplash();

            }
        });
    }

    public void showSplash() {

        JPanel content = (JPanel) getContentPane();
        content.setBackground(Color.blue);

        // Set the window's bounds, centering the window
        int width = 700;
        int height = 450;
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (screen.width - width) / 2;
        int y = (screen.height - height) / 2;
        setBounds(x, y, width, height);

        // Build the splash screen
        JLabel label = new JLabel(new ImageIcon(getClass().getResource("/java_animated.gif")));
        JLabel copyrt = new JLabel("Splash Screen!!!", JLabel.CENTER);

        content.add(label, BorderLayout.CENTER);

        label.setLayout(new GridBagLayout());
        Font font = copyrt.getFont();
        copyrt.setFont(font.deriveFont(Font.BOLD, 24f));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        label.add(copyrt, gbc);

        ImageIcon wait = new ImageIcon(getClass().getResource("/wait.gif"));
        label.add(new JLabel(wait), gbc);

        Color oraRed = new Color(200, 50, 20, 255);
        content.setBorder(BorderFactory.createLineBorder(oraRed, 10));

        // Display it
        setVisible(true);
        toFront();

        new ResourceLoader().execute();
    }

    public class ResourceLoader extends SwingWorker<Object, Object> {

        @Override
        protected Object doInBackground() throws Exception {

            // Wait a little while, maybe while loading resources
            try {
                Thread.sleep(5000);
            } catch (Exception e) {
            }

            return null;

        }

        @Override
        protected void done() {
            setVisible(false);
        }


    }

}
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.Font;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.Toolkit;
导入javax.swing.BorderFactory;
导入javax.swing.ImageIcon;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JWindow;
导入javax.swing.SwingWorker;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
公共类SplashDemo扩展了JWindow{
公共静态void main(字符串[]args){
新的SplashDemo();
}
公共服务{
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
例如printStackTrace();
}
showSplash();
}
});
}
公共空间{
JPanel content=(JPanel)getContentPane();
内容.背景(颜色.蓝色);
//设置窗口的边界,使窗口居中
整数宽度=700;
内部高度=450;
维度屏幕=Toolkit.getDefaultToolkit().getScreenSize();
intx=(screen.width-width)/2;
int y=(screen.height-height)/2;
立根(x、y、宽度、高度);
//构建启动屏幕
JLabel=newjlabel(新的图像图标(getClass().getResource(“/java_animated.gif”));
JLabel copyrt=新的JLabel(“启动屏幕!!!”,JLabel.CENTER);
添加(标签、边框布局、中心);
label.setLayout(新的GridBagLayout());
Font=copyrt.getFont();
copyrt.setFont(font.deriveFont(font.BOLD,24f));
GridBagConstraints gbc=新的GridBagConstraints();
gbc.gridwidth=GridBagConstraints.rements;
添加标签(复印件、gbc);
ImageIcon wait=newImageIcon(getClass().getResource(“/wait.gif”);
添加标签(新的JLabel(等待),gbc);
颜色或红色=新颜色(200、50、20、255);
content.setboorder(BorderFactory.createLineBorder(oraRed,10));
//展示它
setVisible(真);
toFront();
新建ResourceLoader().execute();
}
公共类ResourceLoader扩展SwingWorker{
@凌驾
受保护对象doInBackground()引发异常{
//请稍等,可能是在加载资源时
试一试{
睡眠(5000);
}捕获(例外e){
}
返回null;
}
@凌驾
受保护的void done(){
setVisible(假);
}
}
}
如果你喜欢,我用了下面的图片


您使用的是BorderLayout,这意味着您的集装箱分为东、西、北、南和中。您正在将“闪屏”添加到南部。这就是为什么它没有居中:

content.add(copyrt, BorderLayout.SOUTH);

您需要将布局管理器更改为其中一个网格布局。或者,创建多个面板并将内容添加到其中,然后添加到父容器。

Pee!伙计,你们必须让我的帖子以快速的方式显示出来,我不完全确定如何添加一个摆动计时器。启动屏幕的编译和运行非常好。我想做的完全是化妆。@Opjeezzeey它“看起来”运行良好,但相信我们,它不会……非常感谢你们!我真的只需要启动屏幕。因此,从应用程序的末尾开始就不必担心。但是,我只是删除了注释栏,以使您添加的内容发挥作用?如果您使用
Thread.sleep
,您的应用程序将“挂起”以阻止EDT。更好的方法是启动应用程序并使用类似于
SwingWorker
的东西来加载资源。调用
done
后(在
SwingWorker
上),您可以关闭启动屏幕并打开主应用程序窗口。