?? loginframe.java
字號(hào):
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);
//定義窗口的大小和標(biāo)題
this.setSize(new Dimension(400,330));
this.setTitle("登錄模塊");
//定義標(biāo)簽的標(biāo)題、字符大小和位置
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));
//定義按鈕的標(biāo)題,動(dòng)作字符串,字符大小,位置和加入動(dòng)作接收器
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);
//定義下拉列表框的內(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")){
LoginData login=new LoginData();
int result=login.checkUser(name,password,state);
switch(result){
case 0:
//管理員驗(yàn)證
Manager manager=new Manager(name,password,state);
message="該用戶是管理員";
break;
case 1:
User user=new User(name,password,state);
message="該用戶是普通用戶";
break;
case -1:
message="用戶名或密碼錯(cuò)誤!";
break;
}
//顯示返回的信息
JOptionPane.showMessageDialog(null, message);
}
if(actionCommand.equals("exit")){
//清空內(nèi)存
System.exit(0);
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -