package text20190909; import javax.swing.*; import java.awt.Color; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.Console; import javax.swing.JFrame; import javax.swing.JLabel; import java.math.*; public class Demo extends JFrame{ String b; JButton bq4; Container c; public Demo() { setTitle("登录界面"); setBounds(500,250,400,350); setDefaultCloseOperation(EXIT_ON_CLOSE); c=getContentPane(); //JLabel wb[]=new JLabel[3]; JLabel bq1=new JLabel("登录名:"); setLayout(null); bq1.setBounds(10, 20,50,20); JLabel bq2=new JLabel("密码:"); bq2.setBounds(10, 100,50,20); JLabel bq3=new JLabel("验证码:"); bq3.setBounds(10, 180,50,20); JTextField wb2=new JTextField(20); wb2.setBounds(70, 20,250,36); JPasswordField code=new JPasswordField(20); code.setBounds(70, 100,250,36); JButton jb1=new JButton("登录"); jb1.setBounds(100, 250,90, 36); JTextField yz=new JTextField(20); yz.setBounds(70, 180, 180, 36); c.add(yz); // String b=new String(Rando()); // // JButton bq4=new JButton(b); // bq4.setBounds(260, 180, 70, 36); // c.add(bq4); // /* bq4.setVisible(false); // bq4=new JButton(Rando()); // bq4.addActionListener(null); // c.add(bq4); // */ // bq4.addMouseListener(new MouseAdapter() { // // // @Override // public void mouseClicked(MouseEvent e) { // // // TODO 自动生成的方法存根 // if(e.getButton()==MouseEvent.BUTTON1) // { // bq4.setVisible(false); // JButton bq4=new JButton(Rando()); // // bq4.setBounds(260, 180, 70, 36); // c.add(bq4); // // // } // // } // }); paint(); // (new ActionListener() { // public void actionPerformed(ActionEvent e) { // // if(e.getSource()==bq4) // { // bq4.setVisible(false); // JButton d=new JButton(Rando()); // //d.setBounds(260, 180, 70, 36); // c.add(d); // // // } // // } // } // // // // ); jb1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if((yz.getText().equals(b)&&wb2.getText().trim().equalsIgnoreCase("guziteng")&&new String(code.getPassword()).trim().equalsIgnoreCase("guzuziteng"))){ JOptionPane.showMessageDialog(null, "登陆成功!"); }else if (wb2.getText().trim().length() == 0 || new String(code.getPassword()).trim().length() == 0||yz.getText().trim().equalsIgnoreCase(b)) { JOptionPane.showMessageDialog(null, "不能为空!"); }else{ JOptionPane.showMessageDialog(null, "错误"); // 清零 wb2.setText(""); yz.setText(""); code.setText(""); } } } ); JButton jb2=new JButton("快速注册"); jb2.setBounds(200, 250, 90, 36); c.add(bq1); c.add(bq2); c.add(bq3); c.add(wb2); c.add(code); c.add(jb1); c.add(jb2); setVisible(true); } public void paint() { String b=new String(Rando()); JButton bq4=new JButton(b); bq4.setBounds(260, 180, 70, 36); c.add(bq4); /* bq4.setVisible(false); bq4=new JButton(Rando()); bq4.addActionListener(null); c.add(bq4); */ bq4.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { // TODO 自动生成的方法存根 if(e.getButton()==MouseEvent.BUTTON1) { bq4.setVisible(false); paint(); } } }); } public String Rando() { String str1=""; for(int i=0;i<4;i++) { int x=0; int a = (int)(Math.random()*3); if(a==0) { x = (int)(Math.random()*10+48); } if(a==1) { x = (int)(Math.random()*26+65); } if(a==2) { x = (int)(Math.random()*26+97); } str1=str1+(char)x; } return str1; } public static void main(String[] args) { Demo l=new Demo(); } }
测试案例
为空点击按钮
登录名错误
点击验证码更换