码迷,mamicode.com
首页 > 其他好文 > 详细

登陆界面

时间:2017-10-08 11:17:18      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:需要   退出   icon   class   on()   bounds   init   end   password   

1.先布局,然后创建组件,再根据需要add

2.

【程序流程图】

技术分享

3.

package Demo;

import javax.swing.*;
import java.awt.event.*;
import java.awt.CardLayout;
import java.util.List;
import java.util.ArrayList;

class Person{
private String name;
private String key;
public Person() {

}
public Person(String n,String k) {
this.name = n;
this.key = k;
}
public String getName() {
return name;
}
public String getKey() {
return key;
}
public void setName(String s) {
this.name = s;
}
public void setKey(String k) {
this.key = k;
}
}

public class Login extends JFrame{
private List<Person>list = new ArrayList<Person>(0);
private JPanel[]panel = new JPanel[3];
private JPanel Main = new JPanel();
private String tip = "";
private CardLayout card = new CardLayout();
JButton button1 = new JButton("登陆");
JButton button2 = new JButton("注册");
JButton button3 = new JButton("注册");
JLabel name_label = new JLabel("用户名:");
JLabel name_label1 = new JLabel("用户名:");
JLabel key_label = new JLabel("密码:");
JLabel key_label1 = new JLabel("密码:");
JLabel yz_label = new JLabel("验证码:");
JLabel yz_labels = new JLabel("验证码:");
JLabel yz_label1 = new JLabel("");
JLabel yz_labels1 = new JLabel("");
JLabel tip_label = new JLabel("");
JTextField name_text = new JTextField("");
JPasswordField pkey = new JPasswordField("");
JTextField yz_text = new JTextField("");
JTextField name_text1 = new JTextField("");
JTextField pkey1 = new JTextField("");
JTextField yz_text1 = new JTextField("");
JLabel user_name = new JLabel();

public static String RandomStr() {
String tmp = "";
for(int i = 1;i<=6;++i) {
int val = (int)(Math.random()*9);
tmp+=val;
}
return tmp;
}

public void init() {
for(int i = 0;i<3;++i)
panel[i] = new JPanel();

this.setSize(300,350);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
Main.setSize(300,350);


Main.setLayout(card);
Main.add(panel[0],"log");
Main.add(panel[1],"reg");
Main.add(panel[2],"user");

panel[0].setLayout(null);
panel[0].setSize(300,350);
tip = RandomStr();
yz_label1.setText(tip);
name_label.setBounds(10,10,50,50);
name_text.setBounds(70,10,150,50);
key_label.setBounds(10,70,50,50);
pkey.setBounds(70,70,150,50);
yz_label.setBounds(10,130,50,50);
yz_text.setBounds(70, 130,100,50);
yz_label1.setBounds(180,130,50,50);
tip_label.setBounds(10,190,150,40);
button1.setBounds(30,240,80,50);
button2.setBounds(150,240,80,50);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int flag = 0;
if(list.size()==0) {
tip_label.setText("当前没用户");
return;
}
Person shiyong = new Person();
String mima = new String(pkey.getPassword());
for(Person tmp:list)
if(tmp.getName().equals(name_text.getText())&&tmp.getKey().equals(mima)&&tip.equals(yz_text.getText())) {
flag = 1;
shiyong = tmp;
break;
}
if(flag==0)tip_label.setText("信息错误或不存在");
else {
name_text.setText("");
pkey.setText("");
yz_label1.setText("");
yz_text.setText("");
tip_label.setText("");
card.show(Main,"user");
user_name.setText(shiyong.getName()+",你好");
}
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
name_text.setText("");
pkey.setText("");
yz_text.setText("");
yz_label1.setText("");
tip = RandomStr();
yz_labels1.setText(tip);
card.show(Main,"reg");
}
});
panel[0].add(name_label);
panel[0].add(name_text);
panel[0].add(key_label);
panel[0].add(pkey);
panel[0].add(yz_label);
panel[0].add(yz_text);
panel[0].add(yz_label1);
panel[0].add(tip_label);
panel[0].add(button1);
panel[0].add(button2);

panel[1].setLayout(null);
panel[1].setSize(300,350);
name_label1.setBounds(10,10,50,50);
name_text1.setBounds(70,10,150,50);
key_label1.setBounds(10,70,50,50);
pkey1.setBounds(70,70,150,50);
yz_labels.setBounds(10,130,50,50);
yz_text1.setBounds(70,130,100,50);
yz_labels1.setBounds(180,130,50,50);
button3.setBounds(125,240,100,50);
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("注册")) {
if(yz_text1.getText().equals(tip)) {
list.add(new Person(name_text1.getText(),pkey1.getText()));
button3.setText("注册成功");
}
}
if(e.getActionCommand().equals("注册成功")) {
name_text1.setText("");
pkey1.setText("");
yz_text1.setText("");
yz_labels1.setText("");
yz_label1.setText("");
button3.setText("注册");
tip_label.setText("");
tip = RandomStr();
yz_label1.setText(tip);
card.show(Main,"log");
}
}
});
panel[1].add(name_label1);
panel[1].add(name_text1);
panel[1].add(key_label1);
panel[1].add(pkey1);
panel[1].add(yz_labels);
panel[1].add(yz_text1);
panel[1].add(yz_labels1);
panel[1].add(button3);

panel[2].setLayout(null);
panel[2].setSize(300,350);
user_name.setBounds(0,0,300,50);
JTabbedPane panes = new JTabbedPane();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JLabel p1label = new JLabel(new ImageIcon("C:\\Program Files\\personal\\娱乐版\\头像1.jpg"));
p1.add(p1label);
panes.add("信息1",p1);
panes.add("信息2",p2);
panes.add("信息3",p3);
JScrollPane jsp = new JScrollPane(panes,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp.setBounds(0,60,280,200);
JButton exit_but = new JButton("退出");
exit_but.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

user_name.setText("");
card.show(Main,"log");
}
});
p4.add(exit_but);
p4.setBounds(0,260,300,80);
panel[2].add(user_name);
panel[2].add(jsp);
panel[2].add(p4);

this.add(Main);
this.setVisible(true);
}
public Login() {
super("XX系统");
init();
}
public static void main(String[]args) {
new Login();
}
}

4.

技术分享

技术分享

技术分享

 

登陆界面

标签:需要   退出   icon   class   on()   bounds   init   end   password   

原文地址:http://www.cnblogs.com/t1314/p/7636673.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!