登录页面:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;
import javax.swing.AbstractButton;
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 Login2 extends JFrame implements ActionListener{
JPanel jp=new JPanel();
JLabel name=new JLabel("请输入用户名");
JLabel password=new JLabel("请输入密码");
JLabel show=new JLabel("");
JLabel[] jl= {name,password,show};
JButton login=new JButton("登陆");
JButton reset=new JButton("重置");
JButton register=new JButton("注册");
JButton[] jb= {login,reset,register};
private JTextField JName=new JTextField();
private JPasswordField JPassword=new JPasswordField();
private int i;
public Login2() {
jp.setLayout(null);
for(int i=0;i<3;i++) {
jl[i].setBounds(30,20+40*i,180,20);
jb[i].setBounds(30+100*i,100,80,20);
jp.add(jl[i]);
jp.add(jb[i]);
jb[i].addActionListener(this);
}
JName.setBounds(130,15,100,20);
jp.add(JName);
JName.addActionListener(this);
JPassword.setBounds(130,60,100,20);
jp.add(JPassword);
JPassword.setEchoChar('*');
JPassword.addActionListener(this);
jl[2].setBounds(10,180,270,20);
jp.add(jl[2]);
this.add(jp);
this.setTitle("农产品销售系统登录窗口");
this.setBounds(200,200,350,250);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent arg0) {
EventObject e = null;
if(e.getSource()==JName) {
JPassword.requestFocus();
}
else if(e.getSource()==jb[i]) {
jl[2].setText("");
JName.setText("");
JPassword.setText("");
JName.requestFocus();
}
else if(e.getSource()==jb[2]) {
show.setText("进入农产品销售系统注册页面");
}
else {
if(JName.getText().equals("qwer")&&
String.valueOf(JPassword.getPassword()).equals("1234")) {
jl[2].setText("登陆成功,欢迎您的到来!");
}
else {
jl[2].setText("对不起,您的用户名或密码错误!");
}
}
}
public static void main(String[] args) {
new Login2();
}
}