📄 loginframe.java
字号:
package logininterface;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import method.*;
import goodsinterface.*;
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();
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(new java.awt.Font("Dialog", 0, 15));
nameLabel.setBounds(new Rectangle(65, 67, 81, 16));
passwordLabel.setText("密码:");
passwordLabel.setFont(new java.awt.Font("Dialog", 0, 15));
passwordLabel.setBounds(new Rectangle(65, 112, 79, 16));
userCategoryLabel.setText("用户类别:");
userCategoryLabel.setFont(new java.awt.Font("Dialog", 0, 15));
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(new java.awt.Font("Dialog", 0, 15));
loginBtn.setBounds(new Rectangle(65, 224, 109, 25));
loginBtn.addActionListener(this);
exitBtn.setText("退出");
exitBtn.setActionCommand("exit");
exitBtn.setFont(new java.awt.Font("Dialog", 0, 15));
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")){
CheckUser login = new CheckUser(name, password, state);
message = login.checkLogin();
JFrame frame = null;
if (message.equals("0")) {
frame = new GoodsFrame("manager");
}
else if (message.equals("1")) {
frame = new GoodsFrame("user");
}
else if (message.equals("-1")) {
JOptionPane.showMessageDialog(null, "用户名或者密码输入错误。");
}
if (message.equals("0") | message.equals("1")) {
//使窗口居中显示
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
frame.setLocation( (screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
//隐藏登陆窗口
this.setVisible(false);
}
}
if(actionCommand.equals("exit")){
//清空内存
System.exit(0);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -