用JAVA做一个登陆窗口要输入账号和密码密码输错3次就提示被锁定要有退出按钮

2024-12-26 02:52:13
推荐回答(2个)
回答1:

这个是用java se 做的  代码不复杂,就是 MyEclipse自动生成了一些页面布局的代码,对程序没什么影响,如果你不喜欢或者嫌复杂稍微改一下就可以了,因为大半夜了,布局我也不可能给你手写去,挺麻烦的。见谅。怎么还要用云盘呀 干脆再给你贴上来吧。就是布局那块 有点乱。

import javax.swing.JOptionPane;
public class Login extends javax.swing.JFrame {
 int flag = 3;
 private java.awt.Button button1;
 private java.awt.Button button2;
 private javax.swing.JPasswordField jPasswordField1;
 private java.awt.Label label1;
 private java.awt.Label label2;
 private java.awt.TextField textField1;

 /** Creates new form Login */
 public Login() {
  flag=3;
  initComponents();
  this.setVisible(true);
  button1.setEnabled(true);
 }

 //这个方法里的代码是 工具自动生成的窗体布局代码。直接用就可以,如果你不喜欢你就自己稍微修改一下就可以。直接运行一点问题没有。
 private void initComponents() {

  label1 = new java.awt.Label();
  textField1 = new java.awt.TextField();
  label2 = new java.awt.Label();
  jPasswordField1 = new javax.swing.JPasswordField();
  button1 = new java.awt.Button();
  button2 = new java.awt.Button();

  setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

  label1.setName("");
  label1.setText("用户名:");

  textField1.setName("user");

  label2.setText("密码:");

  jPasswordField1.setName("password");

  button1.setLabel("登录");
  button1.addActionListener(new java.awt.event.ActionListener() {
   public void actionPerformed(java.awt.event.ActionEvent evt) {
    button1ActionPerformed(evt);
   }
  });

  button2.setLabel("退出");
  button2.addActionListener(new java.awt.event.ActionListener() {
   public void actionPerformed(java.awt.event.ActionEvent evt) {
    button2ActionPerformed(evt);
   }
  });

  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
    getContentPane());
  getContentPane().setLayout(layout);
  layout
    .setHorizontalGroup(layout
      .createParallelGroup(
        javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(
        layout
          .createSequentialGroup()
          .addGroup(
            layout
              .createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING,
                false)
              .addGroup(
                layout
                  .createSequentialGroup()
                  .addGap(
                    63,
                    63,
                    63)
                  .addGroup(
                    layout
                      .createParallelGroup(
                        javax.swing.GroupLayout.Alignment.LEADING)
                      .addComponent(
                        label1,
                        javax.swing.GroupLayout.PREFERRED_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.PREFERRED_SIZE)
                      .addComponent(
                        label2,
                        javax.swing.GroupLayout.PREFERRED_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.PREFERRED_SIZE))
                  .addGap(
                    40,
                    40,
                    40)
                  .addGroup(
                    layout
                      .createParallelGroup(
                        javax.swing.GroupLayout.Alignment.LEADING,
                        false)
                      .addComponent(
                        jPasswordField1)
                      .addComponent(
                        textField1,
                        javax.swing.GroupLayout.DEFAULT_SIZE,
                        90,
                        Short.MAX_VALUE)))
              .addGroup(
                layout
                  .createSequentialGroup()
                  .addGap(
                    114,
                    114,
                    114)
                  .addComponent(
                    button1,
                    javax.swing.GroupLayout.PREFERRED_SIZE,
                    53,
                    javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addGap(
                    29,
                    29,
                    29)
                  .addComponent(
                    button2,
                    javax.swing.GroupLayout.DEFAULT_SIZE,
                    javax.swing.GroupLayout.DEFAULT_SIZE,
                    Short.MAX_VALUE)))
          .addContainerGap(155, Short.MAX_VALUE)));
  layout
    .setVerticalGroup(layout
      .createParallelGroup(
        javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(
        layout
          .createSequentialGroup()
          .addGap(82, 82, 82)
          .addGroup(
            layout
              .createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(
                textField1,
                javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE,
                javax.swing.GroupLayout.PREFERRED_SIZE)
              .addComponent(
                label1,
                javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE,
                javax.swing.GroupLayout.PREFERRED_SIZE))
          .addGap(26, 26, 26)
          .addGroup(
            layout
              .createParallelGroup(
                javax.swing.GroupLayout.Alignment.TRAILING)
              .addComponent(
                label2,
                javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE,
                javax.swing.GroupLayout.PREFERRED_SIZE)
              .addComponent(
                jPasswordField1,
                javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE,
                javax.swing.GroupLayout.PREFERRED_SIZE))
          .addGap(38, 38, 38)
          .addGroup(
            layout
              .createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(
                button1,
                javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE,
                javax.swing.GroupLayout.PREFERRED_SIZE)
              .addComponent(
                button2,
                javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE,
                javax.swing.GroupLayout.PREFERRED_SIZE))
          .addContainerGap(84, Short.MAX_VALUE)));

  button1.getAccessibleContext().setAccessibleName("");

  pack();
 }//
 

 private void button2ActionPerformed(java.awt.event.ActionEvent evt) {
  System.exit(0);
 }

 @SuppressWarnings("deprecation")
 private void button1ActionPerformed(java.awt.event.ActionEvent evt) {
  String user = textField1.getText();
  String pass = jPasswordField1.getText();
  System.out.println(user);
  System.out.println(pass);
  if ("admin".equals(user) && pass.equals(pass)) {
   JOptionPane.showMessageDialog(null, "登录成功");
   this.setVisible(false);
  } else {
  
   flag--;
   JOptionPane.showMessageDialog(null, "登录失败,还有"+flag+"次机会");
   
  }
  if (flag <= 0) {
   button1.setEnabled(false);
  }
 }

 public static void main(String args[]) {
  java.awt.EventQueue.invokeLater(new Runnable() {
   public void run() {
    new Login().setVisible(true);
   }
  });
 }

}

回答2:

什么样的 说清楚是java web 还是 swing 还是什么