ActionEvent事件
ActionEvent可能是使用得最多的事件類型,其對應的監聽器是ActionListener接口,該監聽器接口的實現類必須重寫actionPerformed方法,當事件發生時就會調用該方法。
這個方法的原型是:public void actionPerformed(ActionEvent ?e); 這個actionPerformed方法就是當事件發生時,由系統自動調用的方法,因此我們希望事件發生時需要做的業務邏輯可以寫在這個方法中,但是這個方法我們只需要重寫即可,不需要調用,因為它是一個回調方法。
我們在14.4章節中使用Swing的常用控件實現了一個用戶登錄的靜態界面,但是沒用功能(點擊按鈕沒有產生任何事件),現在我們可以使用事件模型一起完成如下功能:
1. 按下退出按鈕時,應用程序結束。
2. 按下登錄按鈕時,如果用戶姓名是zhangsan,并且密碼是sxt,那么登錄界面消失,出現一個對話框,顯示“歡迎您:zhangsan”,否則顯示一個對話框,顯示“用戶姓名或密碼錯誤”。
具體代碼如示例1 ~ 示例4所示。
【示例1】ActionEvent事件—窗口類
packagecn.sxt.actionevent.views;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.JPasswordField;
importjavax.swing.JTextField;
importcn.sxt.actionevent.listeners.LoginFrame_btnLogin_ActionListener;
importcn.sxt.actionevent.listeners.LoginFrame_btnQuit_ActionListener;
/**
*登陸窗口
*@author高淇
*/
publicclassLoginFrameextendsJFrame {
privateJPanelpnlMain;
//標簽控件
privateJLabellblTitle;
privateJLabellblUserName;
privateJLabellblUserPwd;
//輸入用戶名的文本框控件
privateJTextFieldtxtUserName;
//輸入密碼的密碼框控件
privateJPasswordFieldpwdUserPwd;
//登錄和退出按鈕控件
privateJButtonbtnLogin;
privateJButtonbtnQuit;
publicLoginFrame() {
//實例化各種容器和控件
pnlMain=newJPanel(null);
lblTitle=newJLabel("用戶登錄");
lblUserName=newJLabel("用戶姓名:");
lblUserPwd=newJLabel("用戶密碼:");
txtUserName=newJTextField();
pwdUserPwd=newJPasswordField();
btnLogin=newJButton("登錄");
btnQuit=newJButton("退出");
init();
}
//對文本框對象和密碼框對象添加get方法
publicJTextField? getTxtUserName() {
returntxtUserName;
}
publicJPasswordField? getPwdUserPwd() {
returnpwdUserPwd;
}
//該方法對窗口做初始化
privatevoidinit() {
//設置窗口屬性
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("登錄窗口");
this.setSize(300, 220);
this.setResizable(false);
/*
*設置各個控件的位置和坐標
* setBounds方法的前兩個參數為控件的左上角坐標,后兩個參數為控件的寬和高
*/
lblTitle.setBounds(100, 10, 100, 30);
lblUserName.setBounds(20, 60, 75, 25);
lblUserPwd.setBounds(20, 100, 75, 25);
txtUserName.setBounds(100, 60, 120, 25);
pwdUserPwd.setBounds(100, 100, 120, 25);
btnLogin.setBounds(50, 140, 75, 25);
btnQuit.setBounds(150, 140, 75, 25);
/*
*在退出按鈕上添加按鈕按下監聽對象,
*并在實例化監聽對象中傳入當前窗口對象本身
*/
btnQuit.addActionListener(newLoginFrame_btnQuit_ActionListener(this));
/*
*在登錄按鈕上添加按鈕按下監聽對象,
*并在實例化監聽對象中傳入當前窗口對象本身
*/
btnLogin.addActionListener(newLoginFrame_btnLogin_ActionListener(this));
//將所有控件壓在容器上
pnlMain.add(lblTitle);
pnlMain.add(lblUserName);
pnlMain.add(lblUserPwd);
pnlMain.add(txtUserName);
pnlMain.add(btnLogin);
pnlMain.add(btnQuit);
this.add(pnlMain);
this.setVisible(true);
}
}
【示例2】ActionEvent事件—退出按鈕監聽類
packagecn.sxt.actionevent.listeners;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importcn.sxt.actionevent.views.LoginFrame;
/**
*退出功能監聽類
*@author高淇
*/
publicclassLoginFrame_btnQuit_ActionListenerimplementsActionListener {
privateLoginFramelf;
publicLoginFrame_btnQuit_ActionListener(LoginFramelf) {
this.lf=lf;
}
@Override
publicvoidactionPerformed(ActionEvente) {
//關閉登錄窗口(dispose方法為關閉窗口并釋放資源)
lf.dispose();
}
}
【示例3】ActionEvent事件—登錄按鈕監聽類
packagecn.sxt.actionevent.listeners;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JOptionPane;
importcn.sxt.actionevent.views.LoginFrame;
/**
*登陸功能監聽類
*@author高淇
*/
publicclassLoginFrame_btnLogin_ActionListenerimplementsActionListener {
privateLoginFramelf;
publicLoginFrame_btnLogin_ActionListener(LoginFramelf) {
this.lf=lf;
}
@Override
publicvoidactionPerformed(ActionEvente) {
//獲得用戶姓名的文本框對象的文本內容
StringuserName=lf.getTxtUserName().getText().trim();
//獲得用戶密碼的密碼框對象的文本內容
StringuserPwd=newString(lf.getPwdUserPwd().getPassword()).trim();
if(userName.equals("zhangsan") &&userPwd.equals("sxt")) {
/*
*彈出對話框,第1個參數為窗口,所以可以傳null,
*第2個參數為提示文本,第3個參數為標題信息,第4個參數為樣式
*/
JOptionPane.showMessageDialog(null,"歡迎您:"+userName,"提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
JOptionPane.showMessageDialog(null,"用戶姓名或密碼錯誤","錯誤",JOptionPane.ERROR_MESSAGE);
}
}
【示例4】ActionEvent事件—測試類
packagecn.sxt.actionevent.test;
importcn.sxt.actionevent.views.LoginFrame;
publicclassTest {
//創建登錄窗口
publicstaticvoidmain(String[ ]args) {
newLoginFrame();
}
}
執行結果如圖所示:
「全棧Java筆記」是一部能幫大家從零到一成長為全棧Java工程師系列筆記。筆者江湖人稱 Mr. G,10年Java研發經驗,曾在神州數碼、航天院某所研發中心從事軟件設計及研發工作,從小白逐漸做到工程師、高級工程師、架構師。精通Java平臺軟件開發,精通JAVAEE,熟悉各種流行開發框架。
筆記包含從淺入深的六大部分:
A-Java入門階段
B-數據庫從入門到精通
C-手刃移動前端和Web前端
D-J2EE從了解到實戰
E-Java高級框架精解
F-Linux和Hadoop