《零基礎學Java》 事件監聽器 為按鈕等添加事件監聽器,事件監聽器的作用是在用戶單擊按鈕時,設置窗體要實現的功能。 動作事件監聽器 動作事件監聽器(AbstractAction)監聽器是Swing中比較常用的事件監聽器,很多最近的動作都會使用它監聽(比如:按鈕被單擊)。 動作事件監聽器 動作事件監 ...
《零基礎學Java》
-
事件監聽器
為按鈕等添加事件監聽器,事件監聽器的作用是在用戶單擊按鈕時,設置窗體要實現的功能。
-
動作事件監聽器
動作事件監聽器(AbstractAction)監聽器是Swing中比較常用的事件監聽器,很多最近的動作都會使用它監聽(比如:按鈕被單擊)。
動作事件監聽器
動作事件監聽器 實例
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
public class Demo17 extends JFrame {
public Demo17(){
setBounds(100,100,300,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
JLabel jLabel = new JLabel("標簽");
c.add(jLabel);
/**
* 為 按鈕 添加動作監聽
*/
JButton jButton = new JButton("按鈕");
jButton.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
jLabel.setText("按鈕被點擊");
}
});
c.add(jButton);
/**
* 為 文本框 添加動作監聽
*/
JTextField jTextField = new JTextField(10);
c.add(jTextField);
jTextField.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();//捕捉這個動作由那個按鍵觸發的
JTextField jTextField1 = (JTextField)obj;
System.out.println(jTextField1.getText());
jLabel.setText("文本框中點擊了回車");
}
});
/**
* 為 多選框 添加動作監聽
*/
JCheckBox jCheckBox = new JCheckBox("多選框");
c.add(jCheckBox);
jCheckBox.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
jLabel.setText("多選框被點擊");
}
});
/**
* 為 單選框 添加動作監聽
*/
JRadioButton jRadioButton = new JRadioButton("單選框");
c.add(jRadioButton);
jRadioButton.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
jLabel.setText("單選框被點擊");
}
});
/**
* 為 下拉列表 添加動作監聽
*/
String string[] = {"選項1","選項2"};
JComboBox jComboBox = new JComboBox(string);
c.add(jComboBox);
jComboBox.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
jLabel.setText("下拉列表被選擇");
}
});
setVisible(true);
}
public static void main(String[] args) {
new Demo17();
}
}
動作事件監聽器 實例輸出結果:
-
鍵盤事件
當向文本框中輸入文本內容時,將觸發鍵盤事件。KeyEvent類 負責捕獲鍵盤事件,可以通過為組
件添加實現了 KeyListener介面 的監聽器類,來處理該組件觸發的鍵盤事件。KeyListener介面的三個方法:
keyListener.addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent e) { //發生擊鍵事件時觸發 } @Override public void keyPressed(KeyEvent e) { //按鍵被按下時觸發 } @Override public void keyReleased(KeyEvent e) { //按鍵被釋放時觸發 } });
KeyListener類的常用方法:
(PS:在 KeyEvent類 中以 “VK_” 開頭的靜態常量代表各個按鍵的 keyCode,可以通過這些
靜態常量判斷事件中的按鍵,獲得按鍵的標簽。)
鍵盤事件 實例:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class LoginAuthentication extends JFrame {
private JPanel jPanel1;
private JPanel jPanel2;
private JTextField account;
private JPasswordField password;
private JButton jButton;
private String zh = "123";
private String mm = "456";
public LoginAuthentication() {
setBounds(100, 100, 320, 230);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("目前狀態:正在登錄中");
Container c = getContentPane();
c.setLayout(new FlowLayout());
jPanel1 = new JPanel(new FlowLayout());
jPanel1.setBorder(BorderFactory.createTitledBorder("賬號"));
account = new JTextField(20);
account.addKeyListener(new KeyListener() {//創建鍵盤事件
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
int dl = e.getKeyCode();
switch (dl) {
case KeyEvent.VK_ENTER:
password.requestFocus();
return;
}
}
});
jPanel1.add(account);
c.add(jPanel1);
jPanel2 = new JPanel(new FlowLayout());
jPanel2.setBorder(BorderFactory.createTitledBorder("密碼"));
password = new JPasswordField(20);
jPanel2.add(password);
c.add(jPanel2);
jButton = new JButton("登錄");
jButton.addActionListener(new AbstractAction() {//創建滑鼠事件
@Override
public void actionPerformed(ActionEvent e) {
if (account.getText().equals(zh)) {
char ch[] = password.getPassword();
String zhu = new String(ch);
if (zhu.equals(mm)) {
System.out.println("登錄成功");
setTitle("目前狀態:登錄成功");
}else{
System.out.println("登錄失敗");
setTitle("目前狀態:登錄失敗");
}
}else {
System.out.println("登錄失敗");
setTitle("目前狀態:登錄失敗");
}
}
});
c.add(jButton);
setVisible(true);
}
public static void main(String[] args) {
new LoginAuthentication();
}
}
鍵盤事件 實例輸出結果:
-
滑鼠事件
所有組件都能應用滑鼠事件, MouseEvent類 負責捕獲滑鼠事件,可以通過為組件添加實現了
MouseListener介面的監聽器類來處理該組件觸發的滑鼠事件。MouseListener介面 共有5個抽象方法,分別在游標移入、游標移出、滑鼠按鍵被按下、滑鼠按鍵被釋放、滑鼠按鍵被單擊 事件時被觸發。MouseListener介面的五個方法:
clickArea.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
//滑鼠點擊
}
@Override
public void mousePressed(MouseEvent e) {
//按鍵按下
}
@Override
public void mouseReleased(MouseEvent e) {
//按鍵釋放
}
@Override
public void mouseEntered(MouseEvent e) {
//滑鼠進入
}
@Override
public void mouseExited(MouseEvent e) {
//滑鼠退出
}
});
MouseEvent類的常用方法:
MouseEvent類中代表滑鼠按鍵的靜態常量:
滑鼠事件 實例:
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Demo19 extends JFrame {
private JScrollPane jScrollPane;
private JTextArea out;
private JPanel promptArea;
private JPanel clickArea;
public Demo19(){
setBounds(100,100,450,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("滑鼠事件");
Container c =getContentPane();
c.setLayout(new GridLayout(1,2));
promptArea=new JPanel(new FlowLayout());
promptArea.setBorder(BorderFactory.createTitledBorder("輸出區域"));
out=new JTextArea("",18,18);
jScrollPane=new JScrollPane(out);
promptArea.add(jScrollPane);
c.add(promptArea);
clickArea=new JPanel(new FlowLayout());
clickArea.setBorder(BorderFactory.createTitledBorder("點擊區域"));
clickArea.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {//滑鼠點擊
int hitCount = e.getClickCount();//滑鼠點擊次數
int clickButton = e.getButton();//滑鼠點擊的按鍵
switch (clickButton){
case MouseEvent.BUTTON1:
out.append("滑鼠左鍵點擊了"+hitCount+"次組件\n");
break;
case MouseEvent.BUTTON2:
out.append("滑鼠中鍵點擊了"+hitCount+"次組件\n");
break;
case MouseEvent.BUTTON3:
out.append("滑鼠右鍵點擊了"+hitCount+"次組件\n");
break;
}
}
@Override
public void mousePressed(MouseEvent e) {//按鍵按下
}
@Override
public void mouseReleased(MouseEvent e) {//按鍵釋放
}
@Override
public void mouseEntered(MouseEvent e) {//滑鼠進入
out.append("滑鼠進入組件\n");
}
@Override
public void mouseExited(MouseEvent e) {//滑鼠退出
out.append("滑鼠退出組件\n");
}
});
c.add(clickArea);
setVisible(true);
}
public static void main(String[] args) {
new Demo19();
}
}
滑鼠事件 實例輸出結果:
-
窗體事件
WindowListener介面中五個方法:
public DemoWindw(){ addWindowListener(new MywindowListener());//創建窗體事件監聽 } class MywindowListener implements WindowListener{ @Override public void windowOpened(WindowEvent e) { //窗體打開 } @Override public void windowClosing(WindowEvent e) { //窗體將要關閉 } @Override public void windowClosed(WindowEvent e) { //窗體已經關閉 } @Override public void windowIconified(WindowEvent e) { //窗體最小化 } @Override public void windowDeiconified(WindowEvent e) { //窗體恢復正常大小 } @Override public void windowActivated(WindowEvent e) { //窗體激活 } @Override public void windowDeactivated(WindowEvent e) { //窗體失去激活 } }
窗體事件 實例:
import javax.swing.*; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; public class Demo20 extends JFrame { public Demo20(){ setBounds(100,100,300,300); /** * EXIT_ON_CLOSE:關閉視窗同時停止程式 * DISPOSE_ON_CLOSE:關閉視窗時釋放窗體資源 */ setDefaultCloseOperation(DISPOSE_ON_CLOSE); setVisible(true); addWindowListener(new MywindowListener());//創建窗體事件監聽 } class MywindowListener implements WindowListener{ @Override public void windowOpened(WindowEvent e) { //窗體打開 System.out.println("窗體被打開"); } @Override public void windowClosing(WindowEvent e) {//窗體將要關閉 System.out.println("窗體將要被關閉"); } @Override public void windowClosed(WindowEvent e) {//窗體已經關閉 /** * 如果使用EXIT_ON_CLOSE(關閉視窗同時停止程式)可能無法觸發此方法 */ System.out.println("窗體已經被關閉"); } @Override public void windowIconified(WindowEvent e) {//窗體最小化 System.out.println("窗體最小化"); } @Override public void windowDeiconified(WindowEvent e) {//窗體恢復正常大小 System.out.println("窗體恢復正常大小"); } @Override public void windowActivated(WindowEvent e) {//窗體激活 System.out.println("窗體被激活"); } @Override public void windowDeactivated(WindowEvent e) {//窗體失去激活 System.out.println("窗體失去激活"); } } public static void main(String[] args) { new Demo20(); } }
窗體事件 實例輸出結果: