用Swing制作欢迎屏幕

几乎所有时髦的应用都有一个欢迎屏幕。欢迎屏幕既是宣传产品的方法之一,而且在长时间的应用启动过程中,欢迎屏幕还用来表示应用正在准备过程中。



下面是一个最简单的欢迎屏幕实现:



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;

    }

}









SplashWindow1类从Swing的JWindow派生。JWindow是一个容器,它没有其他窗口所具有的各种窗口元素,如标题条、窗口管理按钮,甚至连突出显示的边框也没有。因此,JWindow对于制作欢迎屏幕来说是非常合适的。上面的代码假定图形文件在当前目录。图形通过ImageIcon装入内存,然后它就被放到了JWindow的中心。接着,窗口被pack(),这使得Swing把窗口调整到适当的大小,最后窗口被移到了屏幕的中心。



如果我们运行上面的程序,可以发现虽然欢迎画面确实出现在屏幕中央,但遗憾的是,它却不会关闭!要关闭欢迎画面,我们需要加入更多的代码:



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);

    }

}









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



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



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();

    }

}









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



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



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



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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.shou.loginfjame; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.SQLException; import java.util.List; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.xml.bind.util.ValidationEventCollector; import com.shou.LoginUtil.LoginUser; import com.shou.dao.LoginDao; import com.shuo.util.ValidCode; public class LoginFjame extends JFrame implements ActionListener { private JFrame frame = new JFrame("登录"); private JPanel panel = new JPanel(); private JLabel tiel = new JLabel("龍丶逸小说登录系统"); // 创建标题 private JLabel userLabel = new JLabel("用户名:"); // 创建UserJLabel private JTextArea userText=new JTextArea("请输入内容",7, 30); // 获取登录名 private JLabel passLabel = new JLabel("密 码:"); // 创建PassJLabel private JPasswordField passText = new JPasswordField(20); // 密码框隐藏 private JLabel verCodeLa = new JLabel("验证码:"); // 验证码 private JTextField inputCode = new JTextField(); // 验证码框 private ValidCode vcode = new ValidCode(); // 验证码内容 JTextField jt_code; private JButton loginButton = new JButton("登录"); // 创建登录按钮 private JButton registerButton = new JButton("注 册"); // 创建注册按钮 private JButton newPasswordButton = new JButton("忘记密码"); // 创建注册按钮 private JButton exitButton = new JButton("退出"); JTextField field = null; public LoginFjame() { System.out.println("====================================="); System.out.println("== 龍丶逸小说系统 =="); System.out.println("== V1.1.1.0 =="); System.out.println("====================================="); WinLogin(); } public void WinLogin() { panel.setLayout(null); // 设置布局为 null // 创建标题名称 this.tiel.setFont(new Font("宋体", 1, 20)); this.tiel.setBounds(150, 30, 300, 25); this.panel.add(this.tiel); // 创建 UserJLabel this.userLabel.setFont(new Font("宋体", 1, 13)); this.userLabel.setBounds(70, 80, 80, 25); this.panel.add(userLabel); // 创建文本域用于用户输入 this.userText.setBounds(145, 80, 165, 25); this.panel.add(this.userText); // 注册 this.registerButton.setFont(new Font("宋体", 1, 15)); this.registerButton.setContentAreaFilled(false); this.registerButton.setBorderPainted(false); /* registerButton.setBackground(Color.red); */ this.registerButton.setBounds(320, 80, 100, 25); this.panel.add(this.registerButton); // 变成小手 this.registerButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 创建PassJLabel this.passLabel.setFont(new Font("宋体", 1, 13)); this.passLabel.setBounds(70, 110, 80, 25); this.panel.add(this.passLabel); // 密码输入框 隐藏 this.passText.setBounds(145, 110, 165, 25); this.panel.add(this.passText); // 忘记密码 this.newPasswordButton.setFont(new Font("宋体", 1, 15)); this.newPasswordButton.setContentAreaFilled(false); this.newPasswordButton.setBorderPainted(false); /* registerButton.setBackground(Color.red); */ this.newPasswordButton.setBounds(320, 110, 100, 25); this.panel.add(this.newPasswordButton); // 变成小手 this.newPasswordButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 验证码code this.verCodeLa.setFont(new Font("宋体", 1, 13)); this.verCodeLa.setBounds(70, 140, 80, 25); this.panel.add(this.verCodeLa); // 验证码框 this.inputCode.setBounds(145, 140, 165, 25); this.panel.add(this.inputCode); // 验证码图片 this.vcode.setBounds(320, 140, 165, 25); this.panel.add(this.vcode); System.out.println(this.vcode); // 创建登录按钮 this.loginButton.setFont(new Font("宋体", 1, 15)); this.loginButton.setBounds(95, 190, 80, 25); this.panel.add(this.loginButton); // 变成小手 this.loginButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 退出按钮 this.exitButton.setFont(new Font("宋体", 1, 15)); this.exitButton.setBounds(230, 190, 80, 25); this.panel.add(this.exitButton); // 变成小手 this.exitButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 设置窗体的位置及大小 this.frame.setSize(460, 355); frame.setLocationRelativeTo(null); // 在屏幕中居中显示 frame.add(this.panel); // 添加面板 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置X号后关闭 //设置按钮 this.registerButton.addActionListener(this); //注册按钮 this.newPasswordButton.addActionListener(this); //忘记密码 this.loginButton.addActionListener(this); //登录 this.exitButton.addActionListener(this); //退出 // 往窗体里放其他控件 frame.setVisible(true); // 设置窗体可见 } @Override public void actionPerformed(ActionEvent e) { JButton bt = (JButton) e.getSource(); // 获取按钮信息 String str = bt.getText(); // 获取用户名 String name = this.userText.getText().trim(); // 获取密码 String password = this.passText.getText().trim(); // 获取验证码 String code = this.inputCode.getText().trim(); // 获取jsp验证码 String vcode = this.vcode.getCode(); // 登录 if (str.equals("登录")) { System.out.println("登录"); // 验证码转为大写 String Dcode = code.toUpperCase(); String Dvcode = vcode.toUpperCase(); // 验证码判断 if (Dcode.equals(Dvcode)) { //获取页面的用户名 String username=this.userText.getText().trim(); // 根据用户名查看是否有该用户 try { List loginUser=new LoginDao().queryAll(username); String a=loginUser.toString(); System.out.println(a.toString()); if(!a.toString().equals("[]")){ //密码判断 String mysqlPasword=loginUser.get(0).l_password(); if(mysqlPasword.equals(password)){ //登录成功 JOptionPane pane = new JOptionPane("登录成功"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); }else{ JOptionPane pane = new JOptionPane("密码错误错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); } }else{ JOptionPane pane = new JOptionPane("用户名错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); } /*System.out.println(loginUser.toString()); String sqlUername=loginUser.get(0).getL_username();*/ /*int sqlpassword=loginUser.get(0).getL_power();*/ /*System.out.println("loginF:"+sqlUername);*/ } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else { JOptionPane pane = new JOptionPane("验证码错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); System.out.println(dialog.getFont()); dialog.show(); } } else // 退出 if (str.equals("退出")) { System.out.println("退出"); System.exit(0); } else // 注册 if (str.equals("注 册")) { System.out.println("注 册"); } // 注册 else if (str.equals("忘记密码")) { System.out.println("忘记密码"); } else { System.out.println("异常错误"); } } public boolean isValidCodeRight() { System.out.println(this.jt_code.getText()); if (this.jt_code == null) { return false; } if (this.vcode == null) { return true; } if (this.vcode.getCode().equals(this.jt_code.getText())) { return true; } return false; } public static void main(String[] args) { new LoginFjame(); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值