Java事件处理 java.awt.event
事件:程序對某些操作的響應
例子:點擊按鈕,打印“Hello”
Step1:事件響應類必須能夠聽到相應的命令
點擊按鈕如果要響應,必須讓響應類實現java.awt.event.ActionListener 接口
Step2:將事件響應的代碼,放在接口中重寫的函數內
Step3:綁定,確保按鈕發出命令,響應對象能夠執行
actionPerformed:我們可以看到第一段代碼額外構建了一個函數Printer(),而第二個代碼則運用用到了actionPerformed()
思考問題
問題1:點擊按鈕,需要響應類實現ActionListener,這是誰規定的?
答:Java中定義,不同的事件,由不同的XXXListener來監聽。
問題2:列出什么樣的事件,由什么樣的Listener監聽?
答案:后面再講
問題3:事件響應函數中,ActionEvent參數是什么?
答案:表示命令發出時,封裝的命令發出方的信息。
例:兩個按鈕,一個按鈕點擊,界面變紅,另一個點擊,界面變藍
import javax.swing.*; import java.awt.*; import java.awt.event.*;class EventTest3 extends JFrame implements ActionListener{private JButton jbt1 = new JButton("變紅"); private JButton jbt2 = new JButton("變藍");private JPanel jpl = new JPanel();public EventTest3(){this.add(jbt1,BorderLayout.NORTH);this.add(jbt2,BorderLayout.SOUTH); this.add(jpl,BorderLayout.CENTER); jbt1.addActionListener(this); jbt2.addActionListener(this); this.setSize(300,400);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);}public void actionPerformed(ActionEvent e){ if(e.getSource()==jbt1){jpl.setBackground(Color.red);}else{jpl.setBackground(Color.blue); }}public static void main(String[] args) {new EventTest3();} }——————事件處理講解完畢——————
①實現接口(監聽器)②編寫函數 ③綁定
這叫做:事件監聽機制
什么樣的事件,由什么樣的Listener監聽?
ActionListener(非常重要)
監聽按鈕點擊,文本框內回車,菜單單擊、其他支持單擊響應的控件,以及一些擁有addActionListener函數的控件
ItemListener(比較重要)
監聽選項變化時要響應的事件,如下拉菜單等
下拉菜單中有紅綠藍三個選項,選擇時,界面自動變色
import javax.swing.*; import java.awt.*; import java.awt.event.*; class EventTest4 extends JFrame implements ItemListener {private JComboBox jcb = new JComboBox(); private JPanel jpl = new JPanel();public void itemStateChanged(ItemEvent e){if(jcb.getSelectedItem().equals("紅")){jpl.setBackground(Color.red);}else if(jcb.getSelectedItem().equals("綠")){jpl.setBackground(Color.green); }else{jpl.setBackground(Color.blue); }}public EventTest4(){this.add(jcb,BorderLayout.NORTH); this.add(jpl,BorderLayout.CENTER); jcb.addItemListener(this); jcb.addItem("紅"); jcb.addItem("綠"); jcb.addItem("藍"); this.setSize(300,400);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);}public static void main(String[] args) {new EventTest4();} }MouseListener:監聽鼠標操作(單擊,雙擊,進入,離開等)(比較重要)
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
例1:鼠標進入按鈕,按鈕變紅,退出,按鈕變白
import javax.swing.*; import java.awt.*; import java.awt.event.*;class EventTest5 extends JFrame implements MouseListener{private JButton jbt = new JButton("按鈕");public EventTest5(){jbt.addMouseListener(this); this.add(jbt,BorderLayout.NORTH);this.setSize(300,400);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);}public void mouseClicked(MouseEvent e) {}public void mouseEntered(MouseEvent e) {jbt.setBackground(Color.red);}public void mouseExited(MouseEvent e) {jbt.setBackground(Color.white);}public void mousePressed(MouseEvent e) {}public void mouseReleased(MouseEvent e) {}public static void main(String[] args) {new EventTest5();} }例2:界面上有一個圖片,鼠標進入,圖片隨機躲開
import javax.swing.*; import java.awt.event.*;class EventTest6 extends JFrame implements MouseListener{private JLabel jlb = new JLabel();int X=50,Y=50;public EventTest6(){Icon icon = new ImageIcon("img.jpg");jlb.setIcon(icon);this.setLayout(null); this.add(jlb);jlb.setSize(icon.getIconWidth(),icon.getIconHeight());jlb.setLocation(X,Y);jlb.addMouseListener(this); this.setSize(400,600);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);}public void mouseClicked(MouseEvent e) {}public void mouseEntered(MouseEvent e) {X = (int)(Math.random()*this.getWidth())-jlb.getWidth();Y = (int)(Math.random()*this.getHeight())-jlb.getHeight();jlb.setLocation(X,Y);}public void mouseExited(MouseEvent e) {}public void mousePressed(MouseEvent e) {}public void mouseReleased(MouseEvent e) {}public static void main(String[] args) {new EventTest6();} }MouseMotionListener(比較重要)
監聽鼠標移動和拖動操作
例:鼠標在界面上移動,標題欄顯示鼠標的當前坐標
import javax.swing.*; import java.awt.event.*;class EventTest7 extends JFrame implements MouseMotionListener{public EventTest7(){this.addMouseMotionListener(this);this.setSize(400,600);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);}public void mouseDragged(MouseEvent e) {String str = e.getX() + "," + e.getY();this.setTitle(str);}public void mouseMoved(MouseEvent e) {}public static void main(String[] args) {new EventTest7();} }作業
作業:界面上有一個圖片,鼠標可以將圖片從一個地方拖動到另一個地方
作業:界面上有10個圖片,鼠標可以將某個圖片從一個地方拖動到另一個地方
KeyListener(比較重要)
監聽鍵盤輸入時要響應的事件(如俄羅斯方塊)
例:使用鍵盤上的上下左右鍵,能控制界面上一個圖片的上下左右移動
import javax.swing.*; import java.awt.event.*;class EventTest8 extends JFrame implements KeyListener{private JLabel jlb = new JLabel();int X=50,Y=50;public EventTest8(){Icon icon = new ImageIcon("img.jpg");jlb.setIcon(icon);this.setLayout(null); this.add(jlb);jlb.setSize(icon.getIconWidth(),icon.getIconHeight());jlb.setLocation(X,Y);this.addKeyListener(this);this.setSize(400,600);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);} public void keyPressed(KeyEvent e) {int keyCode = e.getKeyCode();if(keyCode==KeyEvent.VK_UP){Y-=30;}else if(keyCode==KeyEvent.VK_DOWN){Y+=30;}else if(keyCode==KeyEvent.VK_LEFT){X-=30;}else if(keyCode==KeyEvent.VK_RIGHT){X+=30;}jlb.setLocation(X,Y);} public void keyReleased(KeyEvent e) {} public void keyTyped(KeyEvent e) {}public static void main(String[] args) {new EventTest8();} }注意:鍵盤事件一般被更大的容器先截獲。
綜合案例:界面一個圖片掉下,鼠標進入,暫停,離開,繼續掉
import javax.swing.*; import java.awt.event.*;class EventTest9 extends JFrame implements Runnable,MouseListener {private JLabel jlb = new JLabel();int X=50,Y=50;boolean RUN = true;public EventTest9(){Icon icon = new ImageIcon("img.jpg");jlb.setIcon(icon);this.setLayout(null); this.add(jlb);jlb.setSize(icon.getIconWidth(),icon.getIconHeight());jlb.setLocation(X,Y);this.addMouseListener(this);this.setSize(400,600);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);new Thread(this).start();} public void mouseClicked(MouseEvent e) {}public void mouseEntered(MouseEvent e) {RUN = false;}public void mouseExited(MouseEvent e) {RUN = true;new Thread(this).start();}public void mousePressed(MouseEvent e) {}public void mouseReleased(MouseEvent e) {} public void run(){while(RUN){try{Thread.sleep(100); }catch(Exception e){}Y+=20; jlb.setLocation(X,Y);}}public static void main(String[] args) {new EventTest9();} }AdjustmentListener:監聽一些具有調整功能的控件(有點重要)
FocusListener:監聽當控件獲得或者失去焦點時要響應的事件
總結
以上是生活随笔為你收集整理的Java事件处理 java.awt.event的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机硬件软件的英语,关于微型计算机的硬
- 下一篇: Java AWT布局