Java之事件處理


監聽器

ActionListener接口 ——通常用自己創建的新類implements接口

建議使用匿名內部類實現,因為內部類可以訪問類內的變量,而匿名類可以大大簡化代碼,不需要構造函數。

實例:處理按鈕點擊事件

 1 package button;
 2 
 3 import java.awt.*;
 4 import java.awt.event.*;
 5 import javax.swing.*;
 6 
 7 /**
 8  * A frame with a button panel
 9  */
10 public class ButtonFrame extends JFrame
11 {
12    private JPanel buttonPanel;
13    private static final int DEFAULT_WIDTH = 300;
14    private static final int DEFAULT_HEIGHT = 200;
15 
16    public ButtonFrame()
17    {      
18       setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
19 
20       // create buttons
21       JButton yellowButton = new JButton("Yellow");
22       JButton blueButton = new JButton("Blue");
23       JButton redButton = new JButton("Red");
24 
25       buttonPanel = new JPanel();
26 
27       // add buttons to panel
28       buttonPanel.add(yellowButton);
29       buttonPanel.add(blueButton);
30       buttonPanel.add(redButton);
31 
32       // add panel to frame
33       add(buttonPanel);
34 
35       // create button actions
36       ColorAction yellowAction = new ColorAction(Color.YELLOW);
37       ColorAction blueAction = new ColorAction(Color.BLUE);
38       ColorAction redAction = new ColorAction(Color.RED);
39 
40       // associate actions with buttons
41       yellowButton.addActionListener(yellowAction);
42       blueButton.addActionListener(blueAction);
43       redButton.addActionListener(redAction);
44    }
45 
46    /**
47     * An action listener that sets the panel's background color.
48     */
49    private class ColorAction implements ActionListener
50    {
51       private Color backgroundColor;
52 
53       public ColorAction(Color c)
54       {
55          backgroundColor = c;
56       }
57 
58       public void actionPerformed(ActionEvent event)
59       {
60          buttonPanel.setBackground(backgroundColor);
61       }
62    }
63 }
View Code

實例:改變觀感

 1 package plaf;
 2 
 3 import java.awt.event.*;
 4 import javax.swing.*;
 5 
 6 /**
 7  * A frame with a button panel for changing look and feel
 8  */
 9 public class PlafFrame extends JFrame
10 {
11    private JPanel buttonPanel;
12    public PlafFrame()
13    {
14       buttonPanel = new JPanel();
15       
16       UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
17       for (UIManager.LookAndFeelInfo info : infos)
18          makeButton(info.getName(), info.getClassName());
19       
20       add(buttonPanel);
21       pack();
22    }
23 
24    /**
25     * Makes a button to change the pluggable look and feel.
26     * @param name the button name
27     * @param plafName the name of the look and feel class
28     */
29    void makeButton(String name, final String plafName)
30    {
31       // add button to panel
32 
33       JButton button = new JButton(name);
34       buttonPanel.add(button);
35 
36       // set button action
37 
38       button.addActionListener(new ActionListener()
39          {
40             public void actionPerformed(ActionEvent event)
41             {
42                // button action: switch to the new look and feel
43                try
44                {
45                   UIManager.setLookAndFeel(plafName);
46                   SwingUtilities.updateComponentTreeUI(PlafFrame.this);
47                   pack();
48                }
49                catch (Exception e)
50                {
51                   e.printStackTrace();
52                }
53             }
54          });
55    }  
56 }
View Code

適配器

由WindowAdapter類實現的WindowListener接口中的眾多方法

由於ActionListener接口只有一個方法所以不需要提供適配器類

動作

Action接口 由AbstractAction類實現 包含以下方法:

void actionPerformed(ActionEvent event); //擴展於ActionListener接口
void setEnabled(boolean b); //啟用或禁用這個這個動作
boolean isEnabled(); //檢查動作是否啟用
void putValue(String key, Object value); //存儲名/值對到動作對象中
Object getvalue(String key); //檢索動作對象中的任意名/值對
void addPropertyChangeListener(PropertyChangeListener listener);
void remove PropertyChangeListener(PropertyChangeListener listener); 
//最后兩個方法能夠讓其他對象在動作對象的屬性發生變化時得到通告

 

預定義的動作表名稱
名稱
NAME 動作名稱,顯示在按鈕和菜單上
SMALL_ICON 存儲小圖標的地方;顯示在按鈕、菜單項或工具欄中
SHORT_DESCRIPTION 圖標的簡要說明;顯示在工具提示中
LONG_DESCRIPTION 圖標的詳細說明;顯示在在線幫助中。沒有Swing組件使用這個值
MNEMONIC_KEY 快捷鍵縮寫;顯示在菜單項中
ACCELERATOR_KEY 存儲加速擊鍵的地方;Swing組件不使用這個值
ACTION_COMMAND_KEY 歷史遺留;僅在舊版本的registerKeyboardAction方法中使用
DEFAULT 常用的綜合屬性;Swing組件不使用這個值

 

 

 

 

 

 

 

 

鍵盤事件

KeyStroke類將擊鍵與動作相關聯

每個組件可以有三個輸入映射InputMap和一個動作映射ActionMap 用get獲取 put改變

 

輸入映射條件
標志 激活動作
WHEN_FOCUSED 當這個組件擁有鍵盤焦點時
WHEN_ANCESTOR_OF_FOCUSED_COMPONENT 當這個組件包含了擁有鍵盤焦點的組件時
WHEN_IN_FOCUSED_WINDOW 當這個組件被包含在一個擁有鍵盤焦點組件的窗口中時

 

 

 

 

動作實例:

 1 package action;
 2 
 3 import java.awt.*;
 4 import java.awt.event.*;
 5 import javax.swing.*;
 6 
 7 /**
 8  * A frame with a panel that demonstrates color change actions.
 9  */
10 public class ActionFrame extends JFrame
11 {
12    private JPanel buttonPanel;
13    private static final int DEFAULT_WIDTH = 300;
14    private static final int DEFAULT_HEIGHT = 200;
15 
16    public ActionFrame()
17    {
18       setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
19 
20       buttonPanel = new JPanel();
21 
22       // define actions
23       Action yellowAction = new ColorAction("Yellow", new ImageIcon("yellow-ball.gif"),
24             Color.YELLOW);
25       Action blueAction = new ColorAction("Blue", new ImageIcon("blue-ball.gif"), Color.BLUE);
26       Action redAction = new ColorAction("Red", new ImageIcon("red-ball.gif"), Color.RED);
27 
28       // add buttons for these actions
29       buttonPanel.add(new JButton(yellowAction));
30       buttonPanel.add(new JButton(blueAction));
31       buttonPanel.add(new JButton(redAction));
32 
33       // add panel to frame
34       add(buttonPanel);
35 
36       // associate the Y, B, and R keys with names
37       InputMap imap = buttonPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
38       imap.put(KeyStroke.getKeyStroke("ctrl Y"), "panel.yellow");
39       imap.put(KeyStroke.getKeyStroke("ctrl B"), "panel.blue");
40       imap.put(KeyStroke.getKeyStroke("ctrl R"), "panel.red");
41 
42       // associate the names with actions
43       ActionMap amap = buttonPanel.getActionMap();
44       amap.put("panel.yellow", yellowAction);
45       amap.put("panel.blue", blueAction);
46       amap.put("panel.red", redAction);
47    }
48    
49    public class ColorAction extends AbstractAction
50    {
51       /**
52        * Constructs a color action.
53        * @param name the name to show on the button
54        * @param icon the icon to display on the button
55        * @param c the background color
56        */
57       public ColorAction(String name, Icon icon, Color c)
58       {
59          putValue(Action.NAME, name);
60          putValue(Action.SMALL_ICON, icon);
61          putValue(Action.SHORT_DESCRIPTION, "Set panel color to " + name.toLowerCase());
62          putValue("color", c);
63       }
64 
65       public void actionPerformed(ActionEvent event)
66       {
67          Color c = (Color) getValue("color");
68          buttonPanel.setBackground(c);
69       }
70    }
71 }
View Code

 

鼠標事件

如果只希望用戶點擊按鈕或菜單,則不需要顯式地處理鼠標事件。然而,如果希望用戶使用鼠標畫圖,就需要捕獲鼠標移動點擊和拖動事件。

鼠標點擊事件:

MouseHandler類 extends MouseAdapter類 implement MouseListener接口

鼠標第一次被按下時,調用mouse Pressed方法;鼠標被釋放時調用mouseReleased方法;最后調用mouseClicked方法。如果只對最終的點擊事件感興趣,可以忽略前兩個方法。

擴展——getModifiersEx方法能夠准確地報告鼠標事件的鼠標按鈕和鍵盤修飾符。有下列掩碼

  BUTTON1_DOWN_MASK

  BUTTON2_DOWN_MASK

  BUTTON3_DOWN_MASK

  SHIFT_DOWN_MASK

  CTRL_DOWN_MASK

  ALT_DOWN_MASK

  ALT_GRAPH_DOWN_MASK

  META_DOWN_MASK

鼠標移動與拖動事件:

MouseMotionHandler類 implement MouseMotionListener接口

鼠標移動調用mouseMoved方法,鼠標拖動調用mouseDragged方法。

鼠標光標可以參考Cursor類的getPredefinedCursor方法。

  1 package mouse;
  2 
  3 import java.awt.*;
  4 import java.awt.event.*;
  5 import java.awt.geom.*;
  6 import java.util.*;
  7 import javax.swing.*;
  8 
  9 /**
 10  * A component with mouse operations for adding and removing squares.
 11  */
 12 public class MouseComponent extends JComponent
 13 {
 14    private static final int DEFAULT_WIDTH = 300;
 15    private static final int DEFAULT_HEIGHT = 200;
 16 
 17    private static final int SIDELENGTH = 10;
 18    private ArrayList<Rectangle2D> squares;
 19    private Rectangle2D current; // the square containing the mouse cursor
 20 
 21    public MouseComponent()
 22    {
 23       squares = new ArrayList<>();
 24       current = null;
 25 
 26       addMouseListener(new MouseHandler());
 27       addMouseMotionListener(new MouseMotionHandler());
 28    }
 29 
 30    public void paintComponent(Graphics g)
 31    {
 32       Graphics2D g2 = (Graphics2D) g;
 33 
 34       // draw all squares
 35       for (Rectangle2D r : squares)
 36          g2.draw(r);
 37    }
 38 
 39    /**
 40     * Finds the first square containing a point.
 41     * @param p a point
 42     * @return the first square that contains p
 43     */
 44    public Rectangle2D find(Point2D p)
 45    {
 46       for (Rectangle2D r : squares)
 47       {
 48          if (r.contains(p)) return r;
 49       }
 50       return null;
 51    }
 52 
 53    /**
 54     * Adds a square to the collection.
 55     * @param p the center of the square
 56     */
 57    public void add(Point2D p)
 58    {
 59       double x = p.getX();
 60       double y = p.getY();
 61 
 62       current = new Rectangle2D.Double(x - SIDELENGTH / 2, y - SIDELENGTH / 2, SIDELENGTH,
 63             SIDELENGTH);
 64       squares.add(current);
 65       repaint();
 66    }
 67 
 68    /**
 69     * Removes a square from the collection.
 70     * @param s the square to remove
 71     */
 72    public void remove(Rectangle2D s)
 73    {
 74       if (s == null) return;
 75       if (s == current) current = null;
 76       squares.remove(s);
 77       repaint();
 78    }
 79 
 80    private class MouseHandler extends MouseAdapter
 81    {
 82       public void mousePressed(MouseEvent event)
 83       {
 84          // add a new square if the cursor isn't inside a square
 85          current = find(event.getPoint());
 86          if (current == null) add(event.getPoint());
 87       }
 88 
 89       public void mouseClicked(MouseEvent event)
 90       {
 91          // remove the current square if double clicked
 92          current = find(event.getPoint());
 93          if (current != null && event.getClickCount() >= 2) remove(current);
 94       }
 95    }
 96 
 97    private class MouseMotionHandler implements MouseMotionListener
 98    {
 99       public void mouseMoved(MouseEvent event)
100       {
101          // set the mouse cursor to cross hairs if it is inside
102          // a rectangle
103 
104          if (find(event.getPoint()) == null) setCursor(Cursor.getDefaultCursor());
105          else setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
106       }
107 
108       public void mouseDragged(MouseEvent event)
109       {
110          if (current != null)
111          {
112             int x = event.getX();
113             int y = event.getY();
114 
115             // drag the current rectangle to center it at (x, y)
116             current.setFrame(x - SIDELENGTH / 2, y - SIDELENGTH / 2, SIDELENGTH, SIDELENGTH);
117             repaint();
118          }
119       }
120    }
121    
122    public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); }
123 }
View Code

 

AWT事件繼承層次

 AWT事件類的繼承關系

事件處理總結
事件 接口 方法 訪問方法 事件源
語義事件類 ActionEvent ActionListener actionPerformed

getActionCommand

getModifiers

AbstractButton

JComboBox

JTextField

Timer

AdjustmentEvent AdjustmentListener adjustmentValueChanged

getAdjustable

getAdjustmentType

getValue

JScrollbar
ItemEvent ItemListener itemStateChanged

getIten

getItemSelectable

getStateChange

AbstractButton

JComboBox

低級事件類 FocusEvent FocusListener

focusGained

focusLost

isTemporary Component
KeyEvent KeyListener

keyPressed

keyReleased

keyTyped

getKeyChar

getKeyCode

getKeyModifiersText

getKeyText

isActionKey

Component
MouseEvent MouseListener

mousePressed

mouseReleased

mouseEntered

mouseExited

mouseClicked

getClickCount

getX

getY

getPoint

translatePoint

Component
MouseEvent MouseMotionListener

mouseDragged

mouseMoved

  Component
MouseWheelEvent MouseWheelListener mouseWheelMoved

getWheelRotation

getScrollAmount

Component
WindowEvent WindowListener

windowClosing

windowOpened

windowIconified

windowDeiconified

windowClosed

windowActivated

windowDeactivated

getWindow

Window
WindowEvent WindowFocusListener

windowGainFocus

windowLostFocus

getOppositeWindow Window
WindowEvent WindowStateListener windowStateChanged

getOldState

getNewState

Window


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM