zoukankan      html  css  js  c++  java
  • java在线聊天项目 swt可视化窗口Design 登录框注册按钮点击改变窗口大小——出现注册面板 实现打开登录框时屏幕居中

    登录框注册按钮点击改变窗口大小——出现注册面板 


    首先
    用swt可视化设计登录窗口如下图:

    此时窗口高度为578

    没点击注册时高度为301(可自己定)

    注意:注册用户的Jpanel 的border选择Title Border,title属性是“注册用户”

          布局Layout选择Absolute Layout

    接着,对话框窗口设计好后,双击注册按钮,进行代码编辑,在注册按钮的监听代码中增加一个if判断,当等于301,就给改为窗口高度578,否则改为301

    因为使用的是匿名内部类,不能直接使用this.关键字调用getHeight()方法和setHeight()方法,所以用LoginDialog.this.的方法进行调用

    完整代码如下:

    package com.swift;
    
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import javax.swing.border.TitledBorder;
    
    public class LoginDialog extends JDialog {
    
        private JPasswordField passwordField_2;
        private JPasswordField passwordField_1;
        private JTextField textField_2;
        private JTextField textField;
        public static void main(String args[]) {
            JFrame.setDefaultLookAndFeelDecorated(true);
            JDialog.setDefaultLookAndFeelDecorated(true);
            
            try {
                UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
            } catch (ClassNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (InstantiationException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (UnsupportedLookAndFeelException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            
            
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        LoginDialog dialog = new LoginDialog();
                        dialog.addWindowListener(new WindowAdapter() {
                            public void windowClosing(WindowEvent e) {
                                System.exit(0);
                            }
                        });
                        dialog.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        
        public LoginDialog() {
            super();
            setResizable(false);
            setTitle("在线聊天登录框");
            getContentPane().setLayout(null);
            setBounds(100, 100, 427, 301);//注册时高度为578,不注册是301
    
            final JButton button_1 = new JButton();
            button_1.setText("登录");
            button_1.setBounds(230, 222, 106, 36);
            getContentPane().add(button_1);
    
            final JTextField textField_1 = new JTextField();
            textField_1.setBounds(148, 90, 192, 42);
            getContentPane().add(textField_1);
    
            final JLabel label = new JLabel();
            label.setText("帐    号");
            label.setBounds(76, 102, 66, 18);
            getContentPane().add(label);
    
            final JLabel label_1 = new JLabel();
            label_1.setText("密    码");
            label_1.setBounds(76, 167, 66, 18);
            getContentPane().add(label_1);
    
            final JPasswordField passwordField = new JPasswordField();
            passwordField.setBounds(148, 155, 192, 42);
            getContentPane().add(passwordField);
    
            final JButton button = new JButton();
            button.addActionListener(new ActionListener() {
                public void actionPerformed(final ActionEvent e) {
                    if(LoginDialog.this.getHeight()==301) {//使用LoginDialog.this.的方法调用,判断高度,前边有setResizable(false);
                        LoginDialog.this.setSize(427, 578);
                    }else {
                        LoginDialog.this.setSize(427, 301);
                    }
                }
            });
            button.setText("注册");
            button.setBounds(76, 222, 106, 36);
            getContentPane().add(button);
    
            final JPanel panel = new JPanel();
            panel.setLayout(null);
            panel.setBorder(new TitledBorder(null, "注册用户", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
            panel.setBounds(10, 278, 401, 226);
            getContentPane().add(panel);
    
            final JLabel label_2 = new JLabel();
            label_2.setBounds(44, 41, 65, 18);
            label_2.setText("手机号:");
            panel.add(label_2);
    
            textField = new JTextField();
            textField.setBounds(115, 35, 225, 30);
            panel.add(textField);
    
            final JButton button_2 = new JButton();
            button_2.setText("发送验证");
            button_2.setBounds(243, 75, 97, 30);
            panel.add(button_2);
    
            textField_2 = new JTextField();
            textField_2.setBounds(115, 104, 95, 30);
            panel.add(textField_2);
    
            final JLabel label_3 = new JLabel();
            label_3.setText("验证码:");
            label_3.setBounds(44, 110, 65, 18);
            panel.add(label_3);
    
            passwordField_1 = new JPasswordField();
            passwordField_1.setBounds(115, 143, 231, 30);
            panel.add(passwordField_1);
    
            passwordField_2 = new JPasswordField();
            passwordField_2.setBounds(115, 175, 231, 30);
            panel.add(passwordField_2);
    
            final JLabel label_4 = new JLabel();
            label_4.setText("密        码:");
            label_4.setBounds(44, 149, 65, 18);
            panel.add(label_4);
    
            final JLabel label_5 = new JLabel();
            label_5.setText("验证密码:");
            label_5.setBounds(44, 181, 65, 18);
            panel.add(label_5);
    
            final JButton button_3 = new JButton();
            button_3.setBounds(47, 510, 97, 30);
            getContentPane().add(button_3);
            button_3.setText("放弃");
    
            final JButton button_4 = new JButton();
            button_4.setBounds(262, 510, 97, 30);
            getContentPane().add(button_4);
            button_4.setText("注册用户");
        }
        
    }

    实现打开登录框时屏幕居中


    因屏幕居中的功能,其他窗口也需要,所以见一个工具类

    原理是获得屏幕的宽度和高度

    分别减去对话框的宽度和高度

    然后除以2

    就是窗口居中的那个点——Point

    首先,制作一个类com.swift.util.Center

    里边有一个方法getPoint(int width,int height)

    和一个包装的重载方法,方便调用

    getPoint(Dimension d)

    全部代码如下:

    package com.swift.util;
    
    import java.awt.Dimension;
    import java.awt.Point;
    import java.awt.Toolkit;
    
    public class Center {
        public static Point getPoint(int width,int height) {
            Toolkit toolkit=Toolkit.getDefaultToolkit();//应该是单例
            int screenW=toolkit.getScreenSize().width;
            int screenH=toolkit.getScreenSize().height;
            int x=(screenW-width)/2;
            int y=(screenH-height)/2;
            Point p=new Point(x,y);
            return p;
        }
        //函数的重载,参数是包装类尺寸——Dimension
        public static Point getPoint(Dimension d) {
            Point p=getPoint(d.width,d.height);
            return p;
        }
    }

    使用居中的工具类

    this.setLocation(Center.getPoint(this.getSize()));

    在设置对话框宽度高度后,重新设置窗口位置

    LoginDialog.this.setLocation(Center.getPoint(LoginDialog.this.getSize()));

    匿名内部类中,当点击注册按钮,窗口被重新改变大小之后,也要不断改变窗口位置,内部类使用LoginDialog.this.

    全部代码修改如下:

    package com.swift;
    
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import javax.swing.border.TitledBorder;
    
    import com.swift.util.Center;
    
    public class LoginDialog extends JDialog {
    
        private JPasswordField passwordField_2;
        private JPasswordField passwordField_1;
        private JTextField textField_2;
        private JTextField textField;
        public static void main(String args[]) {
            JFrame.setDefaultLookAndFeelDecorated(true);
            JDialog.setDefaultLookAndFeelDecorated(true);
            
            try {
                UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
            } catch (ClassNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (InstantiationException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (UnsupportedLookAndFeelException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            
            
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        LoginDialog dialog = new LoginDialog();
                        dialog.addWindowListener(new WindowAdapter() {
                            public void windowClosing(WindowEvent e) {
                                System.exit(0);
                            }
                        });
                        dialog.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        
        public LoginDialog() {
            super();
            setResizable(false);
            setTitle("在线聊天登录框");
            getContentPane().setLayout(null);
            setBounds(100, 100, 427, 301);//注册时高度为578,不注册是301
            
            //设置窗口居中
            this.setLocation(Center.getPoint(this.getSize()));
    
            final JButton button_1 = new JButton();
            button_1.setText("登录");
            button_1.setBounds(230, 222, 106, 36);
            getContentPane().add(button_1);
    
            final JTextField textField_1 = new JTextField();
            textField_1.setBounds(148, 90, 192, 42);
            getContentPane().add(textField_1);
    
            final JLabel label = new JLabel();
            label.setText("帐    号");
            label.setBounds(76, 102, 66, 18);
            getContentPane().add(label);
    
            final JLabel label_1 = new JLabel();
            label_1.setText("密    码");
            label_1.setBounds(76, 167, 66, 18);
            getContentPane().add(label_1);
    
            final JPasswordField passwordField = new JPasswordField();
            passwordField.setBounds(148, 155, 192, 42);
            getContentPane().add(passwordField);
    
            final JButton button = new JButton();
            button.addActionListener(new ActionListener() {
                public void actionPerformed(final ActionEvent e) {
                    if(LoginDialog.this.getHeight()==301) {
                        LoginDialog.this.setSize(427, 578);
                    }else {
                        LoginDialog.this.setSize(427, 301);
                    }
              //设置窗口不断居中 LoginDialog.
    this.setLocation(Center.getPoint(LoginDialog.this.getSize())); } }); button.setText("注册"); button.setBounds(76, 222, 106, 36); getContentPane().add(button); final JPanel panel = new JPanel(); panel.setLayout(null); panel.setBorder(new TitledBorder(null, "注册用户", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null)); panel.setBounds(10, 278, 401, 226); getContentPane().add(panel); final JLabel label_2 = new JLabel(); label_2.setBounds(44, 41, 65, 18); label_2.setText("手机号:"); panel.add(label_2); textField = new JTextField(); textField.setBounds(115, 35, 225, 30); panel.add(textField); final JButton button_2 = new JButton(); button_2.setText("发送验证"); button_2.setBounds(243, 75, 97, 30); panel.add(button_2); textField_2 = new JTextField(); textField_2.setBounds(115, 104, 95, 30); panel.add(textField_2); final JLabel label_3 = new JLabel(); label_3.setText("验证码:"); label_3.setBounds(44, 110, 65, 18); panel.add(label_3); passwordField_1 = new JPasswordField(); passwordField_1.setBounds(115, 143, 231, 30); panel.add(passwordField_1); passwordField_2 = new JPasswordField(); passwordField_2.setBounds(115, 175, 231, 30); panel.add(passwordField_2); final JLabel label_4 = new JLabel(); label_4.setText("密 码:"); label_4.setBounds(44, 149, 65, 18); panel.add(label_4); final JLabel label_5 = new JLabel(); label_5.setText("验证密码:"); label_5.setBounds(44, 181, 65, 18); panel.add(label_5); final JButton button_3 = new JButton(); button_3.setBounds(47, 510, 97, 30); getContentPane().add(button_3); button_3.setText("放弃"); final JButton button_4 = new JButton(); button_4.setBounds(262, 510, 97, 30); getContentPane().add(button_4); button_4.setText("注册用户"); } }
  • 相关阅读:
    Axis2、Axis1 以及其他接口的调用方式
    com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException
    百度、谷歌、雅虎网站地图提交地址
    MyEclipse 死掉,JVM terminated. Exit code=1073807364
    有关Servlet的面试题
    SpringMVC文件的上传与下载实现
    Java API 操作HBase Shell
    Java内存模型与线程(二)线程的实现和线程的调度
    Java内存模型与线程(一)
    ubuntu环境下搭建Hadoop集群中必须需要注意的问题
  • 原文地址:https://www.cnblogs.com/qingyundian/p/8011503.html
Copyright © 2011-2022 走看看