swing制作的启动界面

SplashWindow1.java

package jiang.splash;

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


/**
 *

Description:


 * SplashWindow1类从Swing的JWindow派生。
 * JWindow是一个容器,它没有其他窗口所具有的各种窗口元素,如标题条、窗口管理按钮,甚至连突出显示的边框也没有。
 * 因此,JWindow对于制作欢迎屏幕来说是非常合适的。下面的代码假定图形文件在当前目录。
 * 图形通过ImageIcon装入内存,然后它就被放到了JWindow的中心。
 * 接着,窗口被pack(),这使得Swing把窗口调整到适当的大小,最后窗口被移到了屏幕的中心。
 * 如果我们运行下面的程序,可以发现虽然欢迎画面确实出现在屏幕中央,但遗憾的是,它却不会关闭!
 * 要关闭欢迎画面,我们需要加入更多的代码(见SplashWindow2.java)。
 *

Copyright: Copyright (c) 2005


 *
 *

Company:


 *
 * @author not attributable
 * @version 1.0
 */
class SplashWindow1 extends JWindow {
    public SplashWindow1(String filename, Frame f) {
        super(f);
        JLabel l = new JLabel(new ImageIcon(filename));
        getContentPane().add(l, BorderLayout.CENTER);
        pack();
        Dimension screenSize =
                Toolkit.getDefaultToolkit().getScreenSize();
        Dimension labelSize = l.getPreferredSize();
        setLocation(screenSize.width / 2 - (labelSize.width / 2),
                    screenSize.height / 2 - (labelSize.height / 2));
        setVisible(true);
        screenSize = null;
        labelSize = null;
    }

    public static void main(String[] args) {
        String fn = "images/mysplash.jpg";
        Frame fm = new Frame();
        SplashWindow1 Login = new SplashWindow1(fn, fm);
    }

}

SplashWindow2.java

package jiang.splash;

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

/**
 *
 *

Description:


 *
 * 和原先的SplashWindow1类相比,这个SplashWindow2类唯一的区别在于多出了一个安装到
 * JWindow上的匿名MouseListener。经过这个改动之后,用户可以点击欢迎屏幕关闭它。
 *

 * 现在我们有了一个很不错的欢迎屏幕,它可以通过点击的方法关闭,但它不会自己消失。
 * 接下来我们要加入代码,使得欢迎屏幕在显示一定的时间之后自动消失。这里我们要考虑到运用线程。
 * (见SplashWindow3.java)
 *

Copyright: Copyright (c) 2005


 *
 *

Company:


 *
 * @author not attributable
 * @version 1.0
 */

class SplashWindow2 extends JWindow
{
    public SplashWindow2(String filename, Frame f)
    {
        super(f);
        JLabel l = new JLabel(new ImageIcon(filename));
        getContentPane().add(l, BorderLayout.CENTER);
        pack();
        Dimension screenSize =
          Toolkit.getDefaultToolkit().getScreenSize();
        Dimension labelSize = l.getPreferredSize();
        setLocation(screenSize.width/2 - (labelSize.width/2),
                    screenSize.height/2 - (labelSize.height/2));
        addMouseListener(new MouseAdapter()
            {
                public void mousePressed(MouseEvent e)
                {
                    setVisible(false);
                    dispose();
                }
            });
        setVisible(true);
    }

    public static void main(String[] args) {
        String fn = "images/mysplash.jpg";
        Frame fm = new Frame();
        SplashWindow2 Login = new SplashWindow2(fn, fm);
    }

}

SplashWindow3.java

package jiang.splash;

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

/**
 *
 *

Description:


 * 这里的基本思路是利用一个在一定时间内暂停等待的Thread对象。
 * 在下面的代码中,线程的暂停时间是2秒。当这个线程唤醒时,它将关闭欢迎屏幕。
 * 由于Swing是非线程安全的,除非代码在事件分派线程上执行,否则它就不应该影响任何UI组件的状态。
 * 所谓事件分派线程,就是Swing中负责绘图和事件处理的线程。
 *

 * 为了解决这个问题,Swing设计者赋予我们安全地把Runnable对象加入UI事件队列的能力。
 * 在本例中,我们用可运行对象closerRunner完成最关键的工作。
 * 我们把可运行对象传入SwingUtilities.invokeAndWait()静态方法,
 * 然后SwingUtilities.invokeAndWait()进行所有未完成的UI操作,
 * 并执行传递给该椒ǖ目稍诵卸韵骳loserRunner的run方法。
 * 通过运用一个独立的线程负责欢迎屏幕的关闭操作,应用担负起了显示和关闭欢迎屏幕之间的所有操作。
 *

 * 如果要让欢迎屏幕总是显示且用户不能关闭它,你必须删除那些隐藏欢迎屏幕的代码。
 * 如果要让欢迎屏幕只能由用户手工关闭,你可以象使用任何其他JWindow对象一样调用
 * SplashWindow3对象上的setVisible(false)和dispose()方法。
 *

 * 总而言之,借助于SwingUtilities.invokeAndWait()方法,我们可以安全地创建出多线程欢迎屏幕。
 * 具体实现时,欢迎屏幕可以由用户点击关闭,也可以在一定的时间之后自动关闭。
 * Swing所支持的线程模型使得应用在显示欢迎屏幕之后仍能够响应其他事件和处理其他任务。
 *
 *

Copyright: Copyright (c) 2005


 *
 *

Company:


 *
 * @author not attributable
 * @version 1.0
 */

class SplashWindow3 extends JWindow
{
    public SplashWindow3(String filename, Frame f, int waitTime)
    {
        super(f);
        JLabel l = new JLabel(new ImageIcon(filename));
        getContentPane().add(l, BorderLayout.CENTER);
        pack();
        Dimension screenSize =
          Toolkit.getDefaultToolkit().getScreenSize();
        Dimension labelSize = l.getPreferredSize();
        setLocation(screenSize.width/2 - (labelSize.width/2),
                    screenSize.height/2 - (labelSize.height/2));
        addMouseListener(new MouseAdapter()
            {
                public void mousePressed(MouseEvent e)
                {
                    setVisible(false);
                    dispose();
                }
            });
        final int pause = waitTime;
        final Runnable closerRunner = new Runnable()
            {
                public void run()
                {
                    setVisible(false);
                    dispose();
                }
            };
        Runnable waitRunner = new Runnable()
            {
                public void run()
                {
                    try
                        {
                            Thread.sleep(pause);
                            SwingUtilities.invokeAndWait(closerRunner);
                        }
                    catch(Exception e)
                        {
                            e.printStackTrace();
                            // 能够捕获InvocationTargetException
                            // 能够捕获InterruptedException
                        }
                }
            };
        setVisible(true);
        Thread splashThread = new Thread(waitRunner, "SplashThread");
        splashThread.start();
    }

    public static void main(String[] args) {
        String fn = "images/mysplash.jpg";
        Frame fm = new Frame();
        SplashWindow3 Login = new SplashWindow3(fn, fm, 2000);
    }

}

SplashWindow4.java

package jiang.splash;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

/**
 *
 *

Description:


 * 在下面的代码中,欢迎屏幕的暂停时间是10秒。同时加入了进度条显示。
 *
 *

Copyright: Copyright (c) 2005


 *
 *

Company:


 *
 * @author not attributable
 * @version 1.0
 */

public class SplashWindow4 extends JWindow {
    public SplashWindow4(String imgPath, Frame f, int waitTime) {

        super(f);

        JLabel lMain = new JLabel(new ImageIcon(imgPath + "logo_main3.jpg"));
        JLabel lEast = new JLabel(new ImageIcon(imgPath + "logo_east.jpg"));
        JLabel lSouth = new JLabel(new ImageIcon(imgPath + "logo_south.jpg"));
        JLabel lWest = new JLabel(new ImageIcon(imgPath + "logo_west.jpg"));
        final JProgressBar p = new JProgressBar();

        getContentPane().add(lMain, BorderLayout.NORTH);
        getContentPane().add(lSouth, BorderLayout.SOUTH);
        getContentPane().add(lEast, BorderLayout.EAST);
        getContentPane().add(lWest, BorderLayout.WEST);
        getContentPane().add(p, BorderLayout.CENTER);
        p.setBorderPainted(false);
        p.setBorder(null);
        p.setPreferredSize(new Dimension(p.getSize().width, 6));
        p.setForeground(new Color(153,153,204));

        pack();

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension splashSize = new Dimension(lMain.getPreferredSize().width,
                                             lMain.getPreferredSize().height +
                                             p.getPreferredSize().height +
                                             lSouth.getPreferredSize().height);
        setLocation(screenSize.width / 2 - (splashSize.width / 2),
                    screenSize.height / 2 - (splashSize.height / 2));

        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                setVisible(false);
                dispose();
            }
        });

        final int pause = waitTime;
        final Runnable closerRunner = new Runnable() {
            public void run() {
                setVisible(false);
                dispose();
            }
        };
        Runnable waitRunner = new Runnable() {
            public void run() {
                try {
                    Thread.sleep(pause);
                    SwingUtilities.invokeAndWait(closerRunner);
                } catch (Exception e) {
                    e.printStackTrace();
                    // 能够捕获InvocationTargetException
                    // 能够捕获InterruptedException
                }
            }
        };

        Runnable progress = new Runnable() {
            public void run() {
                for (int i = 1; i <= 50; i++) {
                    p.setValue(i * 2);
                    try {
                        Thread.sleep(pause / 50);
                    } catch (InterruptedException ex) {
                    }
                }
            }
        };

        setVisible(true);

        Thread splashThread = new Thread(waitRunner, "SplashThread");
        splashThread.start();
        Thread progressThread = new Thread(progress, "ProgressThread");
        progressThread.start();

    }

    public static void main(String[] args) {
        String imgPath  = "images/";
        Frame fm = new Frame();
        SplashWindow4 Login = new SplashWindow4(imgPath, fm, 10000);
    }

}


你可能感兴趣的:(11,J2SE,swing,import,string,thread,exception,login)