编写一个登录窗口
目的
编写一个登录窗口, 密码输入采用密码框,输入密码显示为“*”,当输入用户名admin密码123的时候点击确定跳转到学生信息录入窗口界面,其他输入显示用户名密码错误。点击取消退出运行。
package login;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
//登录窗口
public class login_in extends JFrame implements ActionListener
{
JButton byes=new JButton("确认");
JButton bcancle=new JButton("取消");
JTextField tx1;
JPasswordField tx2;
public login_in()
{
setTitle("登录窗口");
setLocation(300,300);
setSize(600, 300);
setBackground(Color.lightGray);
setLayout(new GridLayout(5,1));
JLabel blank1=new JLabel("");
add(blank1);
JPanel yonghu=new JPanel();
yonghu.setLayout(new FlowLayout());
JLabel l1=new JLabel("用户名:");
yonghu.add(l1);
tx1=new JTextField(30);
yonghu.add(tx1);
add(yonghu);
JPanel mima=new JPanel();
mima.setLayout(new FlowLayout());
JLabel l2=new JLabel("密码: ");
mima.add(l2);
tx2=new JPasswordField(30);
tx2.setEchoChar('*');
mima.add(tx2);
add(mima);
JPanel anjian=new JPanel();
anjian.add(byes);
anjian.add(bcancle);
add(anjian);
//响应
byes.addActionListener(this);
bcancle.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==byes)//如果正确则跳转
{
String password=new String(tx2.getPassword());//获取密码框里的输入
if(tx1.getText().equals("admin")&&(password.equals("123")))//JPasswordField 得到char[] 不能用equals
{
new details();//创建新界面
dispose();//关闭第一个窗口
}
else if(!tx1.getText().equals("admin"))
{
JOptionPane.showMessageDialog(null,"用户不存在,请重新输入");
tx1.setText("");
tx2.setText("");
}
else
{
JOptionPane.showMessageDialog(null,"密码错误,请重新输入");
tx2.setText("");
}
}
else if(e.getSource()==bcancle)
{
System.exit(0);
}
}
public static void main(String args[])
{
new login_in();
}
}
//学生信息录入窗口
class details extends JFrame implements ActionListener//学生信息录入窗口
{
JTextField txid;
JTextField txna;
JRadioButton sexF;
JRadioButton sexM;
JTextField txbirthday;
JCheckBox ckmb;
JComboBox jcbma;//下拉组合框
String maj[];
JTextField txhome;
JTextArea taresume1;
JTextArea taresume2;
JButton save;
JButton cancel;
details()
{
setTitle("学生信息录入窗口");
setLocation(300,200);
setSize(500,600);
setBackground(Color.LIGHT_GRAY);
setLayout(new GridLayout(10,1));
setVisible(true);
JPanel studentID=new JPanel();//学号排
studentID.setLayout(new FlowLayout(0));
add(studentID);
JLabel id=new JLabel("学号");
studentID.add(id);
txid = new JTextField(10);
studentID.add(txid);
JPanel SName = new JPanel();//姓名排
SName.setLayout(new FlowLayout(0));
JLabel name = new JLabel ("姓名");
txna = new JTextField(15);
add(SName);
SName.add(name);
SName.add(txna);
JPanel SSex=new JPanel();//性别排
SSex.setLayout(new FlowLayout(0));
JLabel sex = new JLabel("性别");
ButtonGroup se = new ButtonGroup();
sexF=new JRadioButton("女",false);
sexM=new JRadioButton("男",false);
add(SSex);
se.add(sexF);
se.add(sexM);
SSex.add(sex);
SSex.add(sexF);
SSex.add(sexM);
JPanel SBirth=new JPanel();//生日排
SBirth.setLayout(new FlowLayout(0));
JLabel birth = new JLabel("出生日期");
txbirthday = new JTextField(10);
add(SBirth);
SBirth.add(birth);
SBirth.add(txbirthday);
JPanel Smember = new JPanel();//团员排
Smember.setLayout(new FlowLayout(0));
JLabel member = new JLabel("团员");
ckmb= new JCheckBox();
add(Smember);
Smember.add(member);
Smember.add(ckmb);
JPanel Smajor = new JPanel();//专业排
Smajor.setLayout(new FlowLayout(0));
JLabel major = new JLabel("专业");
maj = new String[]{"计算机","电子","自动化","数学","信息管理","会计","统计"};
jcbma= new JComboBox(maj);
add(Smajor);
Smajor.add(major);
Smajor.add(jcbma);
JPanel SHome = new JPanel();//家庭住址排
SHome.setLayout(new FlowLayout(0));
JLabel home=new JLabel("家庭住址");
txhome = new JTextField(30);
add(SHome);
SHome.add(home);
SHome.add(txhome);
JPanel Sresume = new JPanel();//简历行1
Sresume.setLayout(new BorderLayout());
JLabel resume = new JLabel("简历");
taresume1 =new JTextArea(20,20);
add(Sresume);
Sresume.add("West",resume);
Sresume.add("Center",taresume1);
JPanel Sresume2 = new JPanel();//简历行2
Sresume2.setLayout(new BorderLayout());
taresume2 =new JTextArea(20,20);
add(Sresume2);
Sresume2.add("Center",taresume2);
JPanel last = new JPanel();//按钮行
save = new JButton("保存");
cancel = new JButton("取消");
add(last);
last.add(save);
last.add(cancel);
save.addActionListener(this);
cancel.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==save)//保存
{
taresume1.setText("");
taresume2.setText("");
taresume1.append("学号:"+txid.getText()+"\t");
taresume1.append("姓名:"+txna.getText()+"\n");
if(sexM.isSelected())
{
taresume1.append("性别:男\t");
}
else
{
taresume1.append("性别:女\t");
}
taresume1.append("出生日期:"+txbirthday.getText()+"\n");
if(ckmb.isSelected())
{
taresume1.append("团员:√\t");
}
else
{
taresume1.append("团员:×\t");
}
String majo=jcbma.getSelectedItem().toString();
taresume1.append("专业:"+majo);
taresume2.append("家庭住址:"+txhome.getText());
}
else
{
System.exit(-1);
}
}
static void main(String args[])
{
new details();
}
}
真难写,我吐了。