?? 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);
//定義窗口的大小和標(biāo)題
this.setSize(new Dimension(400, 330));
this.setTitle("登陸模塊");
//定義標(biāo)簽的標(biāo)題、字符大小和位置
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));
//定義按鈕的標(biāo)題、動(dòng)作字符串、字符大小、位置和加入動(dòng)作接收器
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);
//定義下拉列表框的內(nèi)容、第1個(gè)顯示項(xiàng)和位置
stateComboBox.addItem("管理員");
stateComboBox.addItem("用戶");
stateComboBox.setSelectedIndex(0);
stateComboBox.setBounds(new Rectangle(194, 158, 118, 22));
//為面板加入各個(gè)控件
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) {
//取得按鈕的動(dòng)作字符串
String actionCommand = e.getActionCommand().trim();
//取得用戶名
String name = nameTextField.getText();
//取得密碼
String password = new String(passwordField.getPassword());
//取得用戶類別
int state = stateComboBox.getSelectedIndex();
//創(chuàng)建返回的字符串
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, "用戶名或者密碼輸入錯(cuò)誤。");
}
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")){
//清空內(nèi)存
System.exit(0);
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -