📄 loginframe.java
字号:
package loginInterface;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import data.*;
import user.*;
public class LoginFrame extends JFrame implements ActionListener {
JPanel contentPane;
JLabel nameLabel=new JLabel();
JLabel passwordLabel=new JLabel();
JLabel userCategoryLabel=new JLabel();
JTextField nameTextField=new JTextField();
JPasswordField passwordField=new JPasswordField();
JButton loginBtn=new JButton();
JButton exitBtn=new JButton();
JComboBox stateComboBox=new JComboBox();
Font font=new Font("Dialog",0,15);
public LoginFrame(){
try{jbInit();}
catch(Exception e){e.printStackTrace();}
}
private void jbInit() throws Exception{
//取得窗口面板
contentPane=(JPanel)this.getContentPane();
//定义窗口面板的布局
contentPane.setLayout(null);
//定义窗口的大小和标题
this.setSize(new Dimension(400,330));
this.setTitle("登录模块");
//定义标签的标题、字符大小和位置
nameLabel.setText("用户名:");
nameLabel.setFont(font);
nameLabel.setBounds(new Rectangle(65,57,81,16));
passwordLabel.setText("密码:");
passwordLabel.setFont(font);
passwordLabel.setBounds(new Rectangle(65,112,79,16));
userCategoryLabel.setText("用户类型:");
userCategoryLabel.setFont(font);
userCategoryLabel.setBounds(new Rectangle(65,156,79,16));
//定义编辑框的位置
nameTextField.setBounds(new Rectangle(194,67,118,22));
passwordField.setBounds(new Rectangle(194,112,118,22));
//定义按钮的标题,动作字符串,字符大小,位置和加入动作接收器
loginBtn.setText("登录");
loginBtn.setActionCommand("login");
loginBtn.setFont(font);
loginBtn.setBounds(new Rectangle(65,224,109,25));
loginBtn.addActionListener(this);
exitBtn.setText("退出");
exitBtn.setActionCommand("exit");
exitBtn.setFont(font);
exitBtn.setBounds(new Rectangle(203,224,109,25));
exitBtn.addActionListener(this);
//定义下拉列表框的内容,第1个显示项和位置
stateComboBox.addItem("管理员");
stateComboBox.addItem("普通用户");
stateComboBox.setSelectedIndex(0);
stateComboBox.setBounds(new Rectangle(194,158,118,22));
//为面板加入各个控件
contentPane.add(nameLabel,null);
contentPane.add(nameTextField,null);
contentPane.add(passwordLabel,null);
contentPane.add(passwordField,null);
contentPane.add(userCategoryLabel,null);
contentPane.add(stateComboBox,null);
contentPane.add(loginBtn,null);
contentPane.add(exitBtn,null);
}
protected void processWindowEvent(WindowEvent e){
if(e.getID()==WindowEvent.WINDOW_CLOSING){
System.exit(0);
}
}
//按钮单击事件处理代码
public void actionPerformed(ActionEvent e){
//取得按钮的动作字符串
String actionCommand=e.getActionCommand().trim();
//取得用户名
String name=nameTextField.getText();
//取得密码
String password=new String(passwordField.getPassword());
//取得用户类型
int state=stateComboBox.getSelectedIndex();
//创建返回的字符串
String message="";
if(actionCommand.equals("login")){
LoginData login=new LoginData();
int result=login.checkUser(name,password,state);
switch(result){
case 0:
//管理员验证
Manager manager=new Manager(name,password,state);
message="该用户是管理员";
break;
case 1:
User user=new User(name,password,state);
message="该用户是普通用户";
break;
case -1:
message="用户名或密码错误!";
break;
}
//显示返回的信息
JOptionPane.showMessageDialog(null, message);
}
if(actionCommand.equals("exit")){
//清空内存
System.exit(0);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -