创建登入界面(有验证码)
//信1805-1王正帅20183544 package textpass; import java.util.Random; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; public class textpass extends JFrame implements ActionListener { //static private JPanel jp=new JPanel(); private JLabel[] jlArray={new JLabel("用户名"), new JLabel("密 码"), new JLabel("")}; private JLabel jlArray1=new JLabel("验证码"); private String code=new String(""); private JButton[] jbArray={new JButton("登陆"), new JButton("注册"), new JButton("找密码")}; private JTextField jtxtName =new JTextField(); private JTextField jtxt =new JTextField();//++ //private JButton jbArray1 =new JButton(code); private JPasswordField jtxtPassword= new JPasswordField(); public textpass(){ JPanel jp=new JPanel();//界面 jp.setPreferredSize(new Dimension(3000, 1500));//关键代码,设置JPanel的大小 jp.setLayout(null); //满换行 for(int i=1;i<=6;i++){ Random random=new Random(); int character=random.nextInt(26)+97;//随机产生97-123之间的数 code=code+(char)character;//将产生的数转化为char类型为小写字母 } JButton jbArray1 =new JButton(code); jlArray1.setBounds(30, 110, 80, 26);//设置文字30据左,120上下 jbArray1.setBounds(180, 110, 80, 26);//设置文字30据左,120上下 jp.add(jlArray1);jp.add(jbArray1);//显示 for(int i=0;i<2;i++){ jlArray[i].setBounds(30, 10+i*50, 80, 26); jbArray[i].setBounds(50+i*100, 180, 70,26); jp.add(jlArray[i]); jp.add(jbArray[i]); jbArray[i].addActionListener(this); } System.out.println(code); jtxtName.setBounds(80,10,180,30);//设置文本框80左右10上下 jp.add(jtxtName); jtxtName.addActionListener(this); jtxt.setBounds(80,110,80,30); jp.add(jtxt); jtxt.addActionListener(this); jtxtPassword.setBounds(80,60,180,30); jp.add(jtxtPassword); jtxtPassword.setEchoChar('*'); jtxtPassword.addActionListener(this); jlArray[2].setBounds(10, 180, 300, 30); jp.add(jlArray[2]); this.add(jp); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("请登陆"); this.setResizable(false); this.setBounds(1100, 100, 300, 250);//设置界面出现在屏幕上的位置 this.setVisible(true); } public void actionPerformed(ActionEvent e){ if(e.getSource()==jtxtName){ jtxtPassword.requestFocus(); } else if(e.getSource()==jbArray[1]){ jlArray[2].setText(""); jtxtName.setText(""); jtxtPassword.setText(""); jtxtName.requestFocus(); } else{ if(jtxtName.getText().length()>=3&&jtxtName.getText().length()<=8//登入名大于3位小于8位。 &&String.valueOf(jtxtPassword.getPassword()).length()>=3 &&String.valueOf(jtxtPassword.getPassword()).length()<=12 &&String.valueOf(jtxt.getText()).equals(code)){//登密码大于3位小于12位。 jlArray[2].setText("成功"); } else{ jlArray[2].setText("失败"); } } } public static void main(String[] args) { new textpass(); } }
运行测试: