zoukankan      html  css  js  c++  java
  • Java学习(二)

    在老师布置完作业以后我便开始着手建立我的程序,我设计的是一个登录系统,在一个界面选择用户名,以后输入密码进入到第二个界面,然后在第二个界面可以看到自己的个人信息。我用到了单选按钮和复选框和组合框,我能很熟练的运用这几个组件。他们都有着相似之处,单选按钮的话就是先new一个按钮组对象,在new两个按钮,然后把两根按钮添加到按钮组中,再添加到容器中。复选框更简单就是new四个对象,只要掌握好布局就行。前两个想要选择哪个按钮就让其可见就行,组合框的话就是new一个对象,然后一直添加可以了。

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    
    import java.awt.EventQueue;
    import java.awt.Font;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.swing.ButtonGroup;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JComboBox;
    import javax.swing.JPasswordField;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;
    import javax.swing.SwingConstants;
    
    public class FirstLogin extends JFrame {
    
        private JPanel contentPane;
        private JComboBox usernameInput;
        private JPasswordField pwdInput;
        private JLabel warning;
        
    
        public static void main(String args[]) {
            
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        
                        FirstLogin window = new FirstLogin();
                        window.setResizable(false);
                        window.setVisible(true);
                        
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
            
        class Success extends JFrame{
            
            public Success() {
    
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setBounds(630, 250, 450, 500);
                contentPane = new JPanel();
                contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
                setContentPane(contentPane);
                contentPane.setLayout(null);
                JLabel title = new JLabel("个人资料");
                title.setForeground(Color.BLACK);
                title.setFont(new Font("微软雅黑", Font.PLAIN, 15));
                title.setBounds(10, 5, 292, 41);
                contentPane.add(title);
                
                /*
                 * 姓名标签
                 */
                JLabel name = new JLabel("姓名:");
                contentPane.add(name);
                name.setBounds(20, 40, 292, 41);
                
                /*
                 * 性别标签
                 */
                JLabel sex = new JLabel("性别:");
                contentPane.add(sex);
                sex.setBounds(20, 80, 292, 41);
                
                /*
                 * 出生年月标签
                 */
                JLabel date = new JLabel("出生年月:");
                contentPane.add(date);
                date.setBounds(20, 120, 292, 41);
                
                /*
                 * 爱好标签
                 */
                JLabel hobby = new JLabel("爱好:");
                contentPane.add(hobby);
                hobby.setBounds(20, 160, 292, 41);
                
                /*
                 * 姓名文本框
                 */
                JTextField  nameinput = new JTextField("王杰");
                contentPane.add(nameinput);
                nameinput.setBounds(60, 50, 50, 20);
                
                /*
                 * 性别按钮
                 */
                ButtonGroup btnGroup = new ButtonGroup();
                JRadioButton s1 = new JRadioButton("");
                s1.setBounds(60, 90,60,25);
                s1.setSelected(true);
                JRadioButton s2 = new JRadioButton("");
                s2.setBounds(130, 90,60,25);
                btnGroup.add(s1);
                btnGroup.add(s2);
                contentPane.add(s1);
                contentPane.add(s2);
                
                
                /*
                 * 出生日期
                 */
                
                JComboBox date1 = new JComboBox();
                date1.addItem("1995年");
                date1.setBounds(92, 130, 70, 20);
                contentPane.add(date1);
                
                JComboBox date2 = new JComboBox();
                date2.addItem("1月");
                date2.setBounds(170, 130, 50, 20);
                contentPane.add(date2);
                
                
                /*
                 * 爱好
                 */
                JCheckBox check1 = new JCheckBox("游泳");
                JCheckBox check2 = new JCheckBox("跑步");
                JCheckBox check3 = new JCheckBox("钓鱼");
                JCheckBox check4 = new JCheckBox("篮球");
                check1.setBounds(60, 200,80, 25);
                check1.setSelected(true);
                check2.setBounds(60, 260, 80, 25);
                check3.setBounds(220, 200, 80, 25);
                check3.setSelected(true);
                check4.setBounds(220, 260, 80, 25);
                contentPane.add(check1);
                contentPane.add(check2);
                contentPane.add(check3);
                contentPane.add(check4);    
                this.setResizable(false); 
                this.setVisible(true);
            }
        }
                
        
        public FirstLogin() {
        
            /*
            * 设置关闭按钮功能
            */
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(630, 250, 500, 350);//界面长宽高
            contentPane = new JPanel();//创建容器
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));//容器边界
            setContentPane(contentPane);//添加容器到主界面
            contentPane.setLayout(null);//设置布局为绝对布局
            /*
            *初始化提示框
            */
            warning = new JLabel("");
            warning.setHorizontalAlignment(SwingConstants.CENTER);
            warning.setBounds(105, 229, 239, 23);
            warning.setForeground(Color.red);
            contentPane.add(warning);
            /*
            * 标题栏
            */
            JLabel title = new JLabel("用户管理系统");
            title.setForeground(Color.BLUE);
            title.setFont(new Font("微软雅黑", Font.PLAIN, 24));
            title.setBounds(160, 20, 292, 41);
            contentPane.add(title);
            /*
            * 版本栏
            */
            JLabel version = new JLabel("version : V1.0");
            version.setBounds(385, 295, 97, 15);
            contentPane.add(version);
        
            /*
            * 用户名栏
            */
            JLabel username = new JLabel("用户名:");
            username.setBounds(72, 105, 63, 26);
            contentPane.add(username);
            /*
            * 密码栏
            */
            JLabel pwd = new JLabel("密码 :");
            pwd.setBounds(72, 141, 63, 26);
            contentPane.add(pwd);
            /*
            * 用户名输入栏
            */
            usernameInput = new JComboBox();
            usernameInput.setBounds(187, 107, 177, 23);
            usernameInput.addItem("");
            usernameInput.addItem(1);
            usernameInput.addItem(2);
            usernameInput.addItem(3);
        
            contentPane.add(usernameInput);
    
            /*
            * 密码输入栏
            */
            pwdInput = new JPasswordField("");
            pwdInput.setBounds(187, 144, 177, 23);
            contentPane.add(pwdInput);
    
            /*
            * 登录按钮
            */
            
            JButton enter = new JButton("登录");
            enter.setBounds(65, 189, 93, 23);
            /**
            * 注册监听
            */
            
            enter.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    
            String accout = usernameInput.getSelectedItem().toString();
            String pwd = new String(pwdInput.getPassword());
            if(accout.equals("1")&&pwd.equals("123")){
                 setVisible(false);
                 new Success(); 
            }    
              else{
         
                 if(accout.equals("2")&&pwd.equals("123")){
                    setVisible(false);
                     new Success(); 
          }    
                else{
                  if(accout.equals("3")&&pwd.equals("123")){
                     setVisible(false);
                     new Success();     
           }    
                  else{
                     JOptionPane.showMessageDialog(warning, "您输入的用户名与密码不匹配,请重新输入");
                
            }}}}
                });
            
            contentPane.add(enter);
        
            
            /*
            * 退出按钮
            */
            JButton exit = new JButton("退出");
            exit.setBounds(265, 189, 93, 23);
            /**
            * 注册监听
            */
            exit.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent e) {
                    FirstLogin.this.dispose();
                }
            });
            contentPane.add(exit);
    
    }
    }

     

     

    用鼠标点击退出按钮,界面消失。

    我是软一王杰

  • 相关阅读:
    vue开发常用技巧总结(一)
    js时间戳转固定日期格式输出处理
    vue恢复初始数据
    Feature Police导致iframe页面无法使用粘贴功能
    页面异步请求canceled 或 network中接口请求成功但无法查看返回值
    我在阿里云做云开发平台
    Python项目中的requirements文件
    Json常用格式
    浅谈开机启动_windows
    mmdetection 报错: AttributeError: ‘ConfigDict‘ object has no attribute ‘pipeline‘
  • 原文地址:https://www.cnblogs.com/WangJie0108/p/5284797.html
Copyright © 2011-2022 走看看