登陆界面代码
1、設(shè)計(jì)思想
添加窗口界面,想i面添加一系列組件。
輸入賬號(hào)密碼和驗(yàn)證碼后,如果驗(yàn)證碼正確,進(jìn)行注冊(cè),否則注冊(cè)失敗。注冊(cè)成功后,驗(yàn)證碼自動(dòng)刷新,并將注冊(cè)的帳號(hào)和密碼保存。注冊(cè)失敗后,驗(yàn)證碼刷新,從新注冊(cè)。
而后進(jìn)行登錄,輸入帳號(hào)密碼和驗(yàn)證碼,當(dāng)三個(gè)都輸入對(duì)的時(shí)候,登錄成功,否則登錄失敗,刷新驗(yàn)證碼重新登錄。
當(dāng)點(diǎn)擊刷新驗(yàn)證碼后,驗(yàn)證碼自動(dòng)刷新。
2、流程圖
3、源程序代碼
package java作業(yè);
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("帳號(hào):");
zh.setBounds(20,15,50,25);
add(zh);
JLabel mm=new JLabel("密碼:");
mm.setBounds(20,50,50,25);
add(mm);
JLabel en=new JLabel("點(diǎn)擊按鈕后,放大窗口刷新驗(yàn)證碼");
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("請(qǐng)輸入帳號(hào)");
txt2 = new JTextField(20);
txt2.setBounds(65,50,150,25);
add(txt2);
txt2.setText("請(qǐng)輸入密碼");
txt3 = new JTextField(20);
txt3.setBounds(65,85,100,25);
add(txt3);
txt3.setText("請(qǐng)輸入驗(yàn)證碼");
d=new JButton("刷新驗(yàn)證碼");
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("注冊(cè)");
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("注冊(cè)界面");
ha.setBounds(880,460,150,100);
ha.setVisible(true);
JLabel hao=new JLabel("注冊(cè)成功!");
ha.add(hao);
validate();
}
else
{
JFrame ha=new JFrame("注冊(cè)界面");
ha.setBounds(880,460,150,100);
ha.setVisible(true);
JLabel hao=new JLabel("注冊(cè)失敗!");
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、結(jié)果截圖
?
轉(zhuǎn)載于:https://www.cnblogs.com/CHAHA123/p/7636206.html
總結(jié)
- 上一篇: 《构建之法》 读书笔记(4)
- 下一篇: Windows下MYSQL的安装与配置