2010-05-27 58 views
1

對於hw分配,我們應該創建一個自定義按鈕以熟悉擺動和響應事件。我們也想讓這個按鈕成爲讓我困惑的事件源。我有一個ArrayList來跟蹤將註冊來偵聽我的CustomButton的偵聽器。我感到困惑的是如何通知聽衆。我的老師暗示有一個通知和重寫操作執行了我試過的操作,但後來我不確定如何創建一個查看構造函數文檔的ActionEvent對象。來源,編號,字符串都讓我困惑。任何幫助,將不勝感激。在Java中爲CustomButton創建ActionEvent對象

代碼:ActionEvent

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.util.List; 
import java.util.ArrayList; 

public class CustomButton 
{ 
    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       CustomButtonFrame frame = new CustomButtonFrame(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public void addActionListener(ActionListener al) 
    { 
     listenerList.add(al); 
    } 

    public void removeActionListener(ActionListener al) 
    { 
     listenerList.remove(al); 
    } 

    public void actionPerformed(ActionEvent e) 
    { 
     System.out.println("Button Clicked!"); 
    } 

    private void notifyListeners() 
    { 
     ActionEvent event = new ActionEvent(CONFUSED HERE!!!!; 
     for (ActionListener action : listenerList) { 
      action.actionPerfomed(event); 
     } 
    } 

    List<ActionListener> listenerList = new ArrayList<ActionListener>(); 
} 

class CustomButtonFrame extends JFrame 
{ 
    // constructor for CustomButtonFrame 
    public CustomButtonFrame() 
    { 
     setTitle("Custom Button"); 
     CustomButtonSetup buttonSetup = new CustomButtonSetup(); 
     this.add(buttonSetup); 
     this.pack(); 
    } 
} 

class CustomButtonSetup extends JComponent 
{ 
    public CustomButtonSetup() 
    { 
     ButtonAction buttonClicked = new ButtonAction(); 
     this.addMouseListener(buttonClicked); 
    } 

    // because frame includes borders and insets, use this method 
    public Dimension getPreferredSize() 
    { 
     return new Dimension(200, 200); 
    } 

    public void paintComponent(Graphics g) 
    { 
     Graphics2D g2 = (Graphics2D) g; 

     // first triangle coords 
     int x[] = new int[TRIANGLE_SIDES]; 
     int y[] = new int[TRIANGLE_SIDES]; 
     x[0] = 0; y[0] = 0; 
     x[1] = 200; y[1] = 0; 
     x[2] = 0; y[2] = 200; 
     Polygon firstTriangle = new Polygon(x, y, TRIANGLE_SIDES); 

     // second triangle coords 
     x[0] = 0; y[0] = 200;  
     x[1] = 200; y[1] = 200; 
     x[2] = 200; y[2] = 0; 
     Polygon secondTriangle = new Polygon(x, y, TRIANGLE_SIDES); 

     g2.drawPolygon(firstTriangle); 
     g2.setColor(firstColor); 
     g2.fillPolygon(firstTriangle); 

     g2.drawPolygon(secondTriangle); 
     g2.setColor(secondColor); 
     g2.fillPolygon(secondTriangle); 

     // draw rectangle 10 pixels off border 
     int s1[] = new int[RECT_SIDES]; 
     int s2[] = new int[RECT_SIDES]; 
     s1[0] = 5; s2[0] = 5; 
     s1[1] = 195; s2[1] = 5; 
     s1[2] = 195; s2[2] = 195; 
     s1[3] = 5; s2[3] = 195; 
     Polygon rectangle = new Polygon(s1, s2, RECT_SIDES); 
     g2.drawPolygon(rectangle); 
     g2.setColor(thirdColor); 
     g2.fillPolygon(rectangle); 
    } 

    private class ButtonAction implements MouseListener { 
     public void mousePressed(MouseEvent e) 
     { 
      System.out.println("Click!"); 
      firstColor = Color.GRAY; 
      secondColor = Color.WHITE; 

      repaint(); 
     } 

     public void mouseReleased(MouseEvent e) 
     { 
      System.out.println("Released!"); 
      firstColor = Color.WHITE; 
      secondColor = Color.GRAY; 
      repaint(); 
     } 

     public void mouseEntered(MouseEvent e) 
     {} 

     public void mouseExited(MouseEvent e) 
     {} 

     public void mouseClicked(MouseEvent e) 
     {} 
    } 

    public static final int TRIANGLE_SIDES = 3; 
    public static final int RECT_SIDES = 4; 
    private Color firstColor = Color.WHITE; 
    private Color secondColor = Color.DARK_GRAY; 
    private Color thirdColor = Color.LIGHT_GRAY; 
} 

回答

1

的總體思路是:

  • 你保持聽衆的集合。
  • 如果必須通知偵聽器(發生了一個事件),則循環訪問偵聽器的集合並在每個偵聽器(您的案例中的ActionListener)上調用適當的方法。

我沒有看到ActionListener和ActionEvent的聲明。通過你的模式,ActionEvent很可能會有一個表示實際事件的狀態字段,所以它有一個像public ActionEvent(int value)左右的構造函數。監聽器收到ActionEvent,查看ActionEvent對象內部,併發出通知他爲什麼被通知。

編輯

從其他國家人民的回答我剛剛得知的ActionListener和動作事件是AWT類。所以看看他們的java文檔,其餘的答案仍然有效。

EDIT 2

最簡單的構造是這樣的一個:

public ActionEvent(Object source, int id, String command); 

source是對象,觸發事件,那麼在你的情況下,最有可能的按鈕。 id標識事件的類型。從ActionEventAWTEvent的靜態字段中選擇。該命令是您可以提供有關該事件的其他信息的區域。

+0

ActionEvent是來自'awt'包的標準類。 – Roman 2010-05-27 09:29:15

1

閱讀文檔。有關於它的構造函數的一節,請閱讀每個參數的含義。

對於你的情況appliable代碼將是這樣的:

int uniqueId = System.currentTimeMillis().intValue(); 
String commandName = ""; //it can be like "show" or "hide" or whatever else; 
         //you can get this string with getActionCommand() method 
         //and make some actions based on its value 
         //... but you don't need it now 
ActionEvent event = new ActionEvent(this, uniqueId, commandName); 
+2

您確定uniqueId嗎?你不必使用任何定義的ID像'ActionEvent.ACTION_FIRST'嗎? – 2010-05-27 09:53:58

+0

@Andreas_D:不,我不確定。這可能取決於情況。 OP基於處理鼠標事件創建了自己的按鈕,所以他甚至不需要使用ActionEvent類。他可以創造屬於自己的品質。 – Roman 2010-05-27 10:11:38

+0

我試圖創建一個ActionEvent這種方式,與單一的UniqueID,和一個 「」 字符串,我仍然得到一個錯誤: rivate空隙notifyListeners() \t { \t \t INT UNIQUEID = 0; \t \t ActionEvent event = new ActionEvent(this,uniqueId,「」); (ActionListener action:listenerList){ \t \t \t action.actionPerfomed(event); \t \t} \t} – Crystal 2010-05-29 03:16:35