愚人节海报设计:Java如何制作启动界面?

来源:百度文库 编辑:高考问答 时间:2024/04/26 13:41:03
很多应用程序,例如Word、eclipse、JBuilder,启动的时候都有一个启动的界面,java如何实现呢?
请给出示例代码,谢谢!

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.HAND_CURSOR));//设置启动界面的光标样式
JPanel splash=new JPanel(new BorderLayout());
URL url=getClass().getResource("1.jpg");//获得指定资源文件的绝对路径。
if(url!=null)
{
splash.add(new JLabel(new ImageIcon(url)),BorderLayout.CENTER);
}
setContentPane(splash);
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();//获得屏幕的大小
pack();
setLocation((screen.width-getSize().width)/2,(screen.height-getSize().height)/2);//使启动窗口居中显示
start();
}
public void start()
{
toFront();//window类的toFront()方法可以让启动界面显示的时候暂时在最前面,用window类的setAlwayOnTop(boolean)方法可以让窗口总保持在最前面。
splashThread=new Thread(this);
splashThread.start();
}
public void run()
{
try
{
setVisible(true);
Thread.sleep(50000);
}
catch(Exception e)
{
e.printStackTrace();
}
dispose();
}
static void showFrame(String title)
{
JFrame frame=new JFrame(title);
frame.setSize(400,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screenSize=frame.getToolkit().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();
}
}
/*getToolkit()方法是java.awt.window类的方法它可以得到一个Toolkit类。Toolkit对象的getScreenSize()方法可以得到屏幕的大小。
getScreenSize()方法返回一个 Dimension对象,它的width,height属性就是屏幕的宽和高。
Object getClass()方法是java.lang.Object类的方法它可以获得当前正在运行类的对象
URL getResource(String name)方法是java.lang.Class类的方法用此方法可以获得一个指定资源文件的绝对路径。*/

    大概的思路就是使用线程来计算耗时的操作,在前段显示启动的窗口,示例如下:

java 代码/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.SplashScreen;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;

/**
 *
 * @author root
 */
public class SplashTest {

    public static void main(String[] args) {
        new Thread() {
            public void run() {
                try {
                    SplashScreen splash = SplashScreen.getSplashScreen();
                    Graphics2D g = splash.createGraphics();
                    g.setColor(Color.RED);
                    g.drawString("测试文字", 10, 30);
                    splash.update();

                    Thread.sleep(1000);

                    g.setComposite(AlphaComposite.Clear);
                    g.fillRect(0, 0, (int) splash.getSize().getWidth(), (int) splash.getSize().getHeight());
                    splash.setImageURL(SplashTest.class.getResource("/test/SplashScreen.png"));

                    int x = 100, y = 100;
                    String author = "1 2 3 4 5";
                    g.setFont(new Font("TimesRoman", Font.PLAIN, 30));
                    g.setPaintMode();

                    for (int i = 0; i < 5; i++) {
                        g.setColor(new Color(50, 50, 0));
                        g.drawString(author, x + i, y + i - 1);
                        g.setColor(new Color(10, 10, 0));
                        g.drawString(author, x + i - 1, y + i);
                    }
                    g.setColor(Color.red);
                    g.drawString(author, x + 5, y + 5);
                    splash.update();
                    Thread.sleep(1000);


                } catch (Exception ex) {
                    Logger.getLogger(SplashTest.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }.start();


        try {
            Thread.sleep(3000);
        } catch (Exception e) {
            e.printStackTrace();
        }

        JFrame jf = new JFrame("test");
        JButton jb = new JButton("click");
        jf.add(jb);
        jf.setPreferredSize(new Dimension(320, 240));
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.pack();
        jf.setVisible(true);
    }
}