目錄
?事件處理
5.1.事件處理機(jī)制
5.2.事件處理的模型
?5.3.常見事件及事件監(jiān)視器
5.4.事件處理方法
5.5.事件處理:
5.5.1.ActionEvent 與 ActionListener
5.5.2.ItemEvent 與 ItemListener
5.5.3.KeyEvent 與 KeyListener
5.5.4.MouseEvent 與 MouseListener/MouseMotionListener
5.5.5.ListSelectionEvent與ListSelectionListener
5.5.6.ChangeEvent 與 ChangeListener
5.5.7.FocusEvent 與 FocusListener
5.5.8.事件適配器類(Adapter)
?編輯
?事件處理
5.1.事件處理機(jī)制
事件:Java語言將每一個(gè)鍵盤或鼠標(biāo)的操作定義為一個(gè)“事件”。
- 當(dāng)用戶點(diǎn)擊了一個(gè)按鈕,意味著一個(gè)按鈕事件的發(fā)生。
- 事件處理方法對該事件進(jìn)行響應(yīng)
- 事件響應(yīng):當(dāng)事件發(fā)生時(shí),程序應(yīng)該作出何種響應(yīng)。
5.2.事件處理的模型
事件處理的基本思路如下:
- 一個(gè)源(事件源)產(chǎn)生一個(gè)事件(事件對象)并把它送到監(jiān)聽器那里,監(jiān)聽器只是簡單地等待,直到它收到一個(gè)事件,一旦事件被接收,監(jiān)聽器將處理這些事件;
- 一個(gè)事件源必須注冊監(jiān)聽器,以便監(jiān)聽器可以接受關(guān)于一個(gè)特定事件的通知。
- 事件源:產(chǎn)生事件的組件叫事件源。
- 事件對象:描述系統(tǒng)中發(fā)生了什么的對象
- 事件監(jiān)聽器:對事件進(jìn)行處理的類。
- 事件源:按鈕
- 事件對象:動作事件
- 事件監(jiān)視器:動作事件-處理程序
?5.3.常見事件及事件監(jiān)視器
事件名稱 |
監(jiān)聽器 |
主要用途 |
WindowEvent |
WindowListener |
窗口發(fā)生變化,如關(guān)閉 |
ActionEvent |
ActionListener |
產(chǎn)生動作,如單擊按鈕 |
ItemEvent |
ItemListener |
項(xiàng)目變化,如復(fù)選框 |
ListSelectionEvent |
ListSelectionListener |
選擇列表中的項(xiàng)目時(shí) |
ChangeEvent |
ChangeListener |
狀態(tài)改變,如進(jìn)度條 |
FocusEvent |
FocusListener |
焦點(diǎn)獲得或失去 |
MouseEvent |
MouseListener |
鼠標(biāo)點(diǎn)擊、進(jìn)入或離開 |
MouseEvent |
MouseMotionListener |
鼠標(biāo)拖動或移動 |
KeyEvent |
KeyListener |
按鍵產(chǎn)生時(shí) |
MenuEvent |
MenuListener |
菜單選擇 |
5.4.事件處理方法
- 創(chuàng)建將要產(chǎn)生事件的組件對象
- 構(gòu)造實(shí)現(xiàn)相應(yīng)事件監(jiān)聽器接口的類,并創(chuàng)建事件監(jiān)聽器對象
- 為組件對象增加事件監(jiān)聽器對象:組件對象.addXxxListener(事件監(jiān)聽器對象);如:button.addActionListener(this);
- 注:接口中的方法都含有所產(chǎn)生的事件對象參數(shù),使用該參數(shù)的getSource()方法可以得到產(chǎn)生該事件的事件源
- ? ?例如:public void actionPerformed(ActionEvent e);
5.5.事件處理:
5.5.1.ActionEvent 與 ActionListener
- 當(dāng)單擊按鈕、在文本域中回車、選擇組合框中的項(xiàng)目、選擇菜單項(xiàng)時(shí)產(chǎn)生該事件
- ActionListener接口中的方法:void actionPerformed(ActionEvent e);
- ActionEvent中的常用方法:String getActionCommand();//獲得與該動作相聯(lián)系的組件的命令字符串名稱,
- 組件對象可使用setActionCommand(String str)方法進(jìn)行設(shè)置,默認(rèn)的命令字符串名稱是組件的標(biāo)簽名稱?? //使用該方法可實(shí)現(xiàn)不同組件共用同一段處理代碼
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainClass{
public static void main(String[] args){
new ActionListener_1();
}
}
class ActionListener_1 extends JFrame{
private JPanel panel = new JPanel();
private JButton btn1 = new JButton("粉色");
private JButton btn2 = new JButton("黃色");
private JButton btn3 = new JButton("綠色");
public ActionListener_1(){
super("ActionListener");
btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
panel.setBackground(Color.pink); // 設(shè)置面板背景顏色
}
});
btn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
panel.setBackground(Color.yellow);
}
});
btn3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
panel.setBackground(Color.green);
}
});
panel.add(btn1);
panel.add(btn2);
panel.add(btn3);
this.add(panel);
this.setSize(300,300);
this.setLocation(200,250);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
?
5.5.2.ItemEvent 與 ItemListener
- 當(dāng)單選按鈕、復(fù)選按鈕、下拉列表框中的項(xiàng)目狀態(tài)發(fā)生變化時(shí)產(chǎn)生該事件
- ItemListener接口中的方法:void ?itemStateChanged(ActionEvent e);
-
ItemEvent中的常用方法:
- Object getItem();//返回受該事件影響的項(xiàng)目對象,據(jù)需要可將Object轉(zhuǎn)換為相應(yīng)的類型
- int getStateChange();//返回項(xiàng)目狀態(tài)發(fā)生變化的類型
- 取值:ItemEvent.SELECTED? ? ItemEvent.DESELECTED
import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class MainClass {
public static void main(String[] args){
MyJFrame mf = new MyJFrame();
}
}
class MyJFrame extends JFrame {
private JPanel panel;
private JButton button;
private JRadioButton btnPink,btnOrag,btnGren;
private ButtonGroup buttonGroup;
public MyJFrame(){
super("itemListener");
button = new JButton("請為我選擇一種背景顏色");
panel = new JPanel();
btnPink = new JRadioButton("粉色");
btnOrag = new JRadioButton("橙色");
btnGren = new JRadioButton("青色");
buttonGroup = new ButtonGroup();
btnPink.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
button.setBackground(Color.PINK);
button.setText("你的背景色變?yōu)榱朔凵?);
}
});
btnOrag.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
button.setBackground(Color.ORANGE);
button.setText("你的背景色變?yōu)榱顺壬?);
}
});
btnGren.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
button.setBackground(Color.cyan);
button.setText("你的背景色變?yōu)榱饲嗌?);
}
});
buttonGroup.add(btnPink);
buttonGroup.add(btnOrag);
buttonGroup.add(btnGren);
panel.add(btnPink);
panel.add(btnOrag);
panel.add(btnGren);
this.add(button);
this.add(panel, BorderLayout.SOUTH);
this.setLocation(100,200);
this.setSize(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
?
5.5.3.KeyEvent 與 KeyListener
當(dāng)組件上發(fā)生擊鍵時(shí)產(chǎn)生該事件
KeyListener接口中的方法:
- ????????void keyPressed(KeyEvent e);
- ????????void keyReleased(KeyEvent e);
- ????????void keyTyped(KeyEvent e);
KeyEvent中的常用方法:
- char ?getKeyChar(); //返回字符鍵值
- int ? ? getKeyCode(); //返回整數(shù)鍵值
- boolean isAltDown();
- boolean isControlDown();
- boolean isShiftDown();
注:KeyEvent中定義了表示鍵的常量,如 VK_1
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class MainClass{
public static void main(String[] args) {
new KeyListener_2();
}
}
class KeyListener_2 extends JFrame{
private JPanel panel = new JPanel();
private JButton button = new JButton("走動");
public KeyListener_2(){
super("KeyListener");
button.addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
int x = button.getX();
int y = button.getY();
if(keyCode == KeyEvent.VK_RIGHT){
button.setLocation(x+20,y);
}else if(keyCode == KeyEvent.VK_LEFT){
button.setLocation(x-20,y);
}else if(keyCode == KeyEvent.VK_UP){
button.setLocation(x,y-20);
}else if(keyCode == KeyEvent.VK_DOWN){
button.setLocation(x,y+20);
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
});
panel.add(button);
add(panel);
setLocation(300,250);
setSize(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
5.5.4.MouseEvent 與 MouseListener/MouseMotionListener
5.5.4.1MouseListener
當(dāng)在組件上進(jìn)行鼠標(biāo)基本操作時(shí)產(chǎn)生該事件
MouseListener接口中的方法:
- ????????void mousePressed(MouseEvent e);
- ????????void mouseReleased(MouseEvent e);
- ????????void mouseClicked(MouseEvent e);
- ????????void mouseEntered(MouseEvent e);
- ????????void mouseExited(MouseEvent e);
MouseEvent中的常用方法:
- int getButton();
- boolean isAltDown();
- boolean isControlDown();
- boolean isShiftDown();
- int getClickCount();
- Point getPoint();
- int getX();
- int getY();
- boolean isPopupTrigger();//是否是觸發(fā)彈出式菜單的鼠標(biāo)操作
5.5.4.2.MouseMotionListener
當(dāng)在組件上進(jìn)行鼠標(biāo)拖動或移動時(shí)產(chǎn)生該事件
MouseMotionListener接口中的方法:
- ????????void mouseDragged(MouseEvent);
- ????????void mouseMoved(MouseEvent);
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
public class MainClass{
public static void main(String[] args) {
new MouseEvent_1();
}
}
class MouseEvent_1 extends JFrame {
private JPanel panel;
// 鼠標(biāo)上一次的坐標(biāo)
int pre_x = -1;
int pre_y = -1;
// 鼠標(biāo)當(dāng)前坐標(biāo)
int x;
int y;
public MouseEvent_1(){
super("MouseListener——MouseMotionListener");
panel = new JPanel();
panel.addMouseMotionListener(new MouseMotionListener() {
// 鼠標(biāo)拖拽事件
public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
// 重畫,調(diào)用repaint方法觸發(fā)paint
MouseEvent_1.this.repaint();
}
// 鼠標(biāo)移動事件
public void mouseMoved(MouseEvent e) {
}
});
panel.addMouseListener(new MouseListener() {
// 鼠標(biāo)點(diǎn)擊事件
public void mouseClicked(MouseEvent e) {
}
// 鼠標(biāo)按下事件
public void mousePressed(MouseEvent e) {
// 如果是右鍵
if(e.getButton() == MouseEvent.BUTTON3){
// 擦除原來的軌跡
MouseEvent_1.this.panel.repaint();
}
}
// 鼠標(biāo)松開事件
public void mouseReleased(MouseEvent e) {
// 松開后恢復(fù)歷史坐標(biāo)
pre_x = -1;
pre_y = -1;
}
// 鼠標(biāo)移入事件
public void mouseEntered(MouseEvent e) {
}
// 鼠標(biāo)移出事件
public void mouseExited(MouseEvent e) {
}
});
add(panel);
setSize(500,350);
setLocation(350,270);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void paint(Graphics graphics){
// 設(shè)置畫筆的顏色
graphics.setColor(Color.red);
// 歷史坐標(biāo) > 0
if(pre_x > 0 && pre_y > 0){
// 開始畫一條線
graphics.drawLine(pre_x,pre_y,x,y);
}
// 保存當(dāng)前鼠標(biāo)坐標(biāo),作為上一次的鼠標(biāo)坐標(biāo)
pre_x = x;
pre_y = y;
}
}
5.5.5.ListSelectionEvent與ListSelectionListener
- 當(dāng)列表框中的項(xiàng)目發(fā)生變化時(shí)產(chǎn)生該事件
- ListSelectionListener接口中的方法:void valueChanged(ListSelectionEvent e);
- 處理事件時(shí)常使用列表框(JList)對象本身提供的一些方法
5.5.6.ChangeEvent 與 ChangeListener
- 當(dāng)進(jìn)度條,滑動條、微調(diào)器、標(biāo)簽窗格等組件的狀態(tài)發(fā)生變化時(shí)產(chǎn)生該事件
- ChangeListener接口中的方法:void stateChanged(ChangeEvent);
5.5.7.FocusEvent 與 FocusListener
當(dāng)組件獲得或失去輸入焦點(diǎn)時(shí)產(chǎn)生該事件
FocusListener接口中的方法:
????????????????void focusGained(FocusEvent);
????????????????void focusLost(FocusEvent);
FocusEvent中的常用方法:
????????????????Component getOppositeComponent();
5.5.8.事件適配器類(Adapter)
只實(shí)現(xiàn)接口所需要處理的方法——通過覆蓋
對于接口中的其它方法: 系統(tǒng)會提供默認(rèn)的方法(方法體為空)
事件適配器類與事件監(jiān)聽器接口的區(qū)別?
- 利用事件適配器類,只需實(shí)現(xiàn)所需處理的方法
- 利用事件監(jiān)聽器接口,必須實(shí)現(xiàn)所有的方法
文章來源:http://www.zghlxwxcb.cn/news/detail-489155.html
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
public class MainClass{
public static void main(String[] args) {
new MouseEvent_2();
}
}
class MouseEvent_2 extends JFrame {
private JPanel panel;
int pre_x = -1;
int pre_y = -1;
int x;
int y;
public MouseEvent_2(){
super("MouseAdapter——MouseMotionAdapter");
panel = new JPanel();
panel.addMouseMotionListener(new MouseMotionAdapter() {
// 只需實(shí)現(xiàn)需處理的方法
public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
MouseEvent_2.this.repaint();
}
});
panel.addMouseListener(new MouseAdapter() {
// 只需實(shí)現(xiàn)需處理的方法
public void mousePressed(MouseEvent e){
if(e.getButton() == MouseEvent.BUTTON3){
MouseEvent_2.this.panel.repaint();
}
}
public void mouseReleased(MouseEvent e){
pre_x = -1;
pre_y = -1;
}
});
add(panel);
setSize(300,400);
setLocation(300,250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void paint(Graphics graphics){
graphics.setColor(Color.red);
if(pre_x>0 && pre_y>0){
graphics.drawLine(pre_x,pre_y,x,y);
}
pre_x = x;
pre_y = y;
}
}
(27條消息) Swing UI——容器(一)_Stuttering Guy的博客-CSDN博客https://blog.csdn.net/Mr_Morgans/article/details/125109643?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22125109643%22%2C%22source%22%3A%22Mr_Morgans%22%7D&ctrtid=l9JMT(27條消息) Swing UI——基本組件(二)_Stuttering Guy的博客-CSDN博客https://blog.csdn.net/Mr_Morgans/article/details/125110881?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22125110881%22%2C%22source%22%3A%22Mr_Morgans%22%7D&ctrtid=Kzbcx(27條消息) Swing UI——高級組件(三)_Stuttering Guy的博客-CSDN博客https://blog.csdn.net/Mr_Morgans/article/details/125115383?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22125115383%22%2C%22source%22%3A%22Mr_Morgans%22%7D&ctrtid=81Bbq(27條消息) Swing UI——布局管理器(四)_Stuttering Guy的博客-CSDN博客https://blog.csdn.net/Mr_Morgans/article/details/125115409?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22125115409%22%2C%22source%22%3A%22Mr_Morgans%22%7D&ctrtid=GZyFf文章來源地址http://www.zghlxwxcb.cn/news/detail-489155.html
到了這里,關(guān)于Java程序設(shè)計(jì)——Swing UI 事件處理(五)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!