1、设计思想
添加窗口界面,想i面添加一系列组件。
输入账号密码和验证码后,如果验证码正确,进行注册,否则注册失败。注册成功后,验证码自动刷新,并将注册的帐号和密码保存。注册失败后,验证码刷新,从新注册。
而后进行登录,输入帐号密码和验证码,当三个都输入对的时候,登录成功,否则登录失败,刷新验证码重新登录。
当点击刷新验证码后,验证码自动刷新。
2、流程图
3、源程序代码
package java作业;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
class chuang extends JFrame implements ActionListener
{
private JTextField txt1;
private JTextField txt2;
private String base = "abcdefghijklmnopqrstuvwxyz0123456789";
private String zifu;
private JLabel haha;
private JTextField txt3;
private JButton d;
private JButton dl;
private JButton zc;
private String zhanghao;
private String mima;
private String yanzheng;
private StringBuffer sb;
chuang()
{
setBounds(800,400,300,250);
setVisible(true);
setTitle("界面");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null);
JLabel zh=new JLabel("帐号:");
zh.setBounds(20,15,50,25);
add(zh);
JLabel mm=new JLabel("密码:");
mm.setBounds(20,50,50,25);
add(mm);
JLabel en=new JLabel("点击按钮后,放大窗口刷新验证码");
en.setBounds(20,150,200,25);
add(en);
Random random = new Random();
sb = new StringBuffer();
for (int i = 0; i < 4; i++)
{
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
}
haha=new JLabel(sb.toString()+":");
haha.setBounds(20,85,50,25);
add(haha);
txt1 = new JTextField(20);
txt1.setBounds(65,15,150,25);
add(txt1);
txt1.setText("请输入帐号");
txt2 = new JTextField(20);
txt2.setBounds(65,50,150,25);
add(txt2);
txt2.setText("请输入密码");
txt3 = new JTextField(20);
txt3.setBounds(65,85,100,25);
add(txt3);
txt3.setText("请输入验证码");
d=new JButton("刷新验证码");
d.addActionListener(this);
d.setBounds(170,85,100,24);
add(d);
dl=new JButton("登录");
dl.addActionListener(this);
dl.setBounds(35,120,75,24);
add(dl);
zc=new JButton("注册");
zc.addActionListener(this);
zc.setBounds(120,120,75,24);
add(zc);
validate();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==d)
{
remove(haha);
Random random = new Random();
sb = new StringBuffer();
for (int i = 0; i < 4; i++)
{
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
}
haha=new JLabel(sb.toString()+":");
haha.setBounds(20,85,50,25);
add(haha);
validate();
}
else if(e.getSource()==dl)
{
yanzheng=txt3.getText();
if(yanzheng.equals(sb.toString())&&txt1.getText().equals(zhanghao)&&txt2.getText().equals(mima))
{
JFrame ha=new JFrame("登录界面");
ha.setBounds(880,460,150,100);
ha.setVisible(true);
JLabel hao=new JLabel("登录成功!");
ha.add(hao);
validate();
}
else
{
JFrame ha=new JFrame("登录界面");
ha.setBounds(880,460,150,100);
ha.setVisible(true);
JLabel hao=new JLabel("登录失败!");
ha.add(hao);
validate();
}
}
else if(e.getSource()==zc)
{
yanzheng=txt3.getText();
zhanghao=txt1.getText();
mima=txt2.getText();
if(yanzheng.equals(sb.toString()))
{
JFrame ha=new JFrame("注册界面");
ha.setBounds(880,460,150,100);
ha.setVisible(true);
JLabel hao=new JLabel("注册成功!");
ha.add(hao);
validate();
}
else
{
JFrame ha=new JFrame("注册界面");
ha.setBounds(880,460,150,100);
ha.setVisible(true);
JLabel hao=new JLabel("注册失败!");
ha.add(hao);
validate();
}
remove(haha);
Random random = new Random();
sb = new StringBuffer();
for (int i = 0; i < 4; i++)
{
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
}
haha=new JLabel(sb.toString()+":");
haha.setBounds(20,85,50,25);
add(haha);
validate();
}
}
public void shuzi()
{
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 4; i++)
{
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
}
System.out.println(sb.toString());
}
}
public class suiji {
public static void main(String args[])
{
new chuang();
}
}
4、结果截图