2014-11-19 5 views
2

나는 명백한 대답이 필요 없다. 이 문제를 어떻게 해결할 수 있을지에 대한 제안. 내 기본 클래스에서 수행 된 작업을 호출 할 수 있도록 MyButton에 동작 수신기를 추가하려고합니다.사용자 정의 Button 클래스 (Java)가있는 ActionListener

이것은 구성된 버튼입니다. 그들은 나타나고 actionListener를 제외한 모든 것이 작동합니다.

upButton = new MyButton(upStaticImageLocation, upRolledImageLocation, upClickedImageLocation); 
    upButton.addActionListener(this); 
    downButton = new MyButton(downStaticImageLocation, downRolledImageLocation, downClickedImageLocation); 
    downButton.addActionListener(this); 

이것은 작업 수신기 메서드가있는 MyButton 클래스입니다.

downButton.addActionListener(this); 

사용합니다 :

downButton.addActionListener(downButton); 

당신이 어딘가에 외부 액션 청취자를 추가하려고하기 때문에

public class MyButton extends JComponent implements MouseListener { 

private Dimension size = new Dimension(32, 32); 

private Image staticImage; 
private Image rolledImage; 
private Image clickedImage; 

private ArrayList<ActionListener> listeners = new ArrayList<ActionListener>(); 

public MyButton(String staticImage, String rolledImage, String clickedImage) { 
    super(); 

    this.staticImage = new ImageIcon(staticImage).getImage(); 
    this.rolledImage = new ImageIcon(rolledImage).getImage(); 
    this.clickedImage = new ImageIcon(clickedImage).getImage(); 

    enableInputMethods(true); 
    addMouseListener(this); 

    setSize(size.width, size.height); 
    setFocusable(true); 
} 
+0

* 메인 클래스에서 수행 된 작업을 * 호출하는 것은 무엇을 의미합니까? –

+0

@Forager 질문은 FX가 아닌 스윙과 관련이 있습니다 – MadProgrammer

+0

아, 죄송합니다. 나는 그것을 알아야했다. – Forager

답변

2

이 작업은 이미 완료된 것으로 보입니다. 자세한 내용은 How to Use Buttons, Check Boxes, and Radio Buttons을 참조하십시오.

그러나 listenersList, 예를 들면 ...

public class MyButton extends JComponent implements MouseListener { 

    //private ArrayList<ActionListener> listeners = new ArrayList<ActionListener>(); 

    public MyButton(String staticImage, String rolledImage, String clickedImage) { 
     //... 
    } 

    public void addActionListener(ActionListener listener) { 
     listenerList.add(ActionListener.class, listener); 
    } 

    public void removeActionListener(ActionListener listener) { 
     listenerList.remove(ActionListener.class, listener); 
    } 

좋아,하지만 어떻게 당신이 이벤트를 트리거 마십시오 JComponent 이미 EventListenerList에 대한 액세스를 제공합니다, 그래서 당신은 필요하지 않습니다? 당신이 ActionPerformed 이벤트를 보낼 때마다

protected void fireActionPerformed() { 
    ActionListener[] listeners = listenerList.getListeners(ActionListener.class); 
    if (listeners != null) { 
     ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Awesome button action"); 
     for (ActionListener listener : listeners) { 
      listener.actionPerformed(evt); 
     } 
    } 
} 

그래서, 당신은 단순히 내가 액션 청취자를 고정하고, 모든 일의 일을 만든 방법을 게시해야

0

내가 제대로 이해하지만, 대신이라고 생각하면 확실하지 않다 MyButton 'this'는 MyButton의 인스턴스를 가리 키지 않으며 MyButton은 ActionListener 즉이 메서드를 구현해야합니다. 즉

@Override public void actionPerformed(ActionEvent e) 
{ 
    // doo your woodoo here 
} 
0

생각하여 버튼 내에서 fireActionPerformed 메서드를 호출합니다. 그의 대답은 madProgrammer에게 감사드립니다.

 public void addActionListener(ActionListener listener) { 
     listenerList.add(ActionListener.class, listener); 
    } 

protected void fireActionPerformed() { 
     ActionListener[] listeners = listenerList.getListeners(ActionListener.class); 
     if (listeners != null) { 
      ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Awesome button action"); 
      for (ActionListener listener : listeners) { 
       listener.actionPerformed(evt); 
      } 
     } 
    } 

public void mouseClicked(MouseEvent e) { 
    fireActionPerformed(); 
} 

마우스를 클릭했을 때 fireActionPerformed에 대한 호출이 필요했습니다.