62,635
社区成员




package view;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LoginView extends JFrame implements ActionListener {
//定义组件
ImageIcon welcomeIcon;
JButton jb1,jb2,jb3;
JPanel jp1,jp2,jp3,jp4;
JTextField jtf;
JPasswordField jpf;
JLabel jlb1,jlb2,jlb3;
//设定用户名和密码
final String id="root";
final String pwd="root";
public LoginView(){
//创建组件
jb1=new JButton("登录");
jb2=new JButton("重置");
jb3=new JButton("退出");
welcomeIcon=new ImageIcon(".src/images/welcome.jpg");
//设置监听
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jlb1=new JLabel("用户名:");
jlb2=new JLabel("密 码:");
jlb3=new JLabel(welcomeIcon,JLabel.CENTER);
jtf=new JTextField(10);
jpf=new JPasswordField(10);
//加入到JPanel中
jp4.add(jlb3);
jp1.add(jlb1);
jp1.add(jtf);
jp2.add(jlb2);
jp2.add(jpf);
jp3.add(jb1); //添加按钮
jp3.add(jb2);
jp3.add(jb3);
//加入JFrame中
this.add(jp4);
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.setLayout(new GridLayout(4,1)); //选择GridLayout布局管理器
this.setTitle("长途汽车MIS");
this.setSize(300, 200);
this.setLocation(400, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if(e.getActionCommand()=="登录"){
login();
}else if(e.getActionCommand()=="重置") {
clear();
}else if(e.getActionCommand()=="退出") {
System.exit(0);
}
}
public void login() {
if(id.equals(jtf.getText())&&pwd.equals(jpf.getText())) {
JOptionPane.showMessageDialog(null, "登陆成功!","提示消息",JOptionPane.WARNING_MESSAGE);
clear();
dispose();
UI ui=new UI();
}else if(jtf.getText().isEmpty()&&jpf.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "请输入用户名和密码!", "提示消息", JOptionPane.WARNING_MESSAGE);
}else if(jtf.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "请输入用户名!", "提示消息", JOptionPane.WARNING_MESSAGE);
}else if(jpf.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "请输入密码!", "提示消息", JOptionPane.WARNING_MESSAGE);
}else {
JOptionPane.showMessageDialog(null, "用户名或密码错误!\n请重新输入", "提示消息", JOptionPane.WARNING_MESSAGE);
clear();
}
}
public void clear() {
jtf.setText("");
jpf.setText("");
}
}