qq_58925043 2021-06-18 18:40 采纳率: 16.7%
浏览 249

JTextField JPasswordField

编写程序,利用JTextField和JPasswordField分别接收用户输入的用户名和密码,并对用户输入的密码进行检验。对于每个用户名有3次密码输入机会。 提示:ActionEvent事件的处理接口为ActionListener,需要重写的方法名为actionPerformed

  • 写回答

1条回答 默认 最新

  • 杨丙寅 2023-01-28 13:44
    关注
    
    import java.awt.EventQueue;
     
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import java.awt.BorderLayout;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JTextField;
    import java.awt.Color;
    import javax.swing.JCheckBox;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.GridBagLayout;
    import java.awt.GridBagConstraints;
    import java.awt.Insets;
    import javax.swing.JTextPane;
    import java.awt.TextArea;
    import java.awt.Label;
    import javax.swing.JPasswordField;
    import javax.swing.JOptionPane;
     
    public class test {
     
        private JFrame frame;
        private JPasswordField passwordField;
        private JTextField textField;
     
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        test window = new test();
                        window.frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
     
        /**
         * Create the application.
         */
        public test() {
            initialize();
             
        }
        /**
         * Judge the pwd and username
         */
        public void judge() {
             
        }
     
        /**
         * Initialize the contents of the frame.
         */
        private void initialize() {
            frame = new JFrame();
            frame.setBounds(100, 100, 450, 300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().setLayout(null);
             
     
            Label label_2 = new Label("login");//login
            label_2.setBounds(141, 31, 139, 23);
            frame.getContentPane().add(label_2);
             
             
            JButton btnNewButton = new JButton("\u767B\u5F55");//登录
            btnNewButton.setBounds(168, 198, 93, 23);
            btnNewButton.setBackground(Color.LIGHT_GRAY);
            btnNewButton.addActionListener(new ActionListener() {
                int i = 3;
                public void actionPerformed(ActionEvent arg0) {
                    String usr=textField.getText();
                    String pwd=passwordField.getText();
                     
                    while(i!=0) {
                        i--;
                        if(usr.equals("123456")&pwd.equals("abc123")) {
                            JOptionPane.showMessageDialog(null,  "登陆成功!欢迎徐小白同学~");
                            break;
                         }
                        else {
                            JOptionPane.showMessageDialog(null, "账户或密码错误,你还有"+(i)+"次机会");
                            break;
                        }
                    }
                    if(i==0) {
                        System.exit(0);
                    }
                }
            });
            frame.getContentPane().add(btnNewButton);
             
            Label label = new Label("\u5B66\u53F7\uFF1A");//学号
            label.setBounds(140, 73, 34, 23);
            frame.getContentPane().add(label);
             
            Label label_1 = new Label("\u5BC6\u7801\uFF1A");//密码
            label_1.setBounds(140, 121, 34, 23);
            frame.getContentPane().add(label_1);
             
            passwordField = new JPasswordField();
            passwordField.setBounds(182, 123, 79, 21);
            frame.getContentPane().add(passwordField);
     
            textField = new JTextField();
            textField.setBounds(182, 73, 79, 21);
            frame.getContentPane().add(textField);
            textField.setColumns(10);
             
        }
         
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘