2013-11-21 73 views
3

我有一堆不同的複選框和文本字段的JPanel,我有一個禁用的按鈕,需要在特定配置設置時啓用。 我需要的是在整個JPanel尋找事件時的一個監聽器,只要有任何改變。 我相信我需要一個動作偵聽器,但我無法找到任何與JpanelJPanel Action Listener

JPanel Window = new JPanel(); 
Window.addActionListener(new ActionListener(){ 
//Check if configurations is good 
} 

彌合的動作監聽我想我可以在面板中複製和粘貼我的代碼一堆次到每一個聽衆,但這對我來說似乎是不好的編碼習慣。

+1

具體到你說話的配置。 'JPanel'不是一個容器,而是一個能夠執行操作的'Controller'組件。點擊一個按鈕是一個動作,就像你開啓和關閉它一樣。點擊一個複選框是一個動作,就像您使用刻度線選擇您感興趣的數據一樣。 – Sage

回答

4

首先作爲@Sage在他的comment中提到a JPanel是一個容器,而不是執行操作的組件。所以你不能附加ActionListenerJPanel

我想我可以複製粘貼一段代碼到面板中的每個 監聽器中,但這對我來說似乎是不好的編碼習慣。

你說的完全正確,根本不是一個好習慣(見DRY principle)。取而代之的是你可以定義一個ActionListener並將其連接到您的JCheckBox ES這樣的:

final JCheckBox check1 = new JCheckBox("Check1"); 
final JCheckBox check2 = new JCheckBox("Check2"); 
final JCheckBox check3 = new JCheckBox("Check3"); 

final JButton buttonToBeEnabled = new JButton("Submit"); 
buttonToBeEnabled.setEnabled(false); 

ActionListener actionListener = new ActionListener() { 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     boolean enable = check1.isSelected() && check3.isSelected(); 
     buttonToBeEnabled.setEnabled(enable); 
    } 
}; 

check1.addActionListener(actionListener); 
check2.addActionListener(actionListener); 
check3.addActionListener(actionListener); 

這意味着:如果check1check3都選中,則該按鈕必須啓用,否則必須將其禁用。當然,只有您知道應該選擇哪些複選框組合才能設置啓用按鈕。

看看How to Use Buttons, Check Boxes, and Radio Buttons教程。

0

有人建議可以從每個您使用的組件的派生類,並添加冒泡容器樹,尋找實現這樣的定製界面的第一個容器一個ActionListener:

public interface MyCommandProcessor { 
    void execute(String actionCommand); 
} 

public class MyButton extends JButton { 
    public MyButton(string actionCommand) { 
    setActionCommand(actionCommand); 
    addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent ae) { 
     Container traverser = MyButton.this; 
     while (traverser != null && !(traverser instanceof MyCommandProcessor)) 
      traverser = traverser.getParent(); 
     if (traverser != null) 
      ((CommandListener)traverser).execute(ae.getActionCommand()); 
     } 
    }); 
    } 
} 

public class MyApp extends JFrame implements MyCommandListener { 
    public MyApp() { 
    JPanel panel = new Panel(); 
    panel.add(new MyButton("MyButton got pressed")); 
    } 

    public void execute(String actionCommand) { 
    System.out.println(actionCommand); 
    } 
} 
0

您需要創建自定義組件偵聽器。看看這裏:

Create a custom event in Java

Creating Custom Listeners In Java

http://www.javaworld.com/article/2077333/core-java/mr-happy-object-teaches-custom-events.html

我這樣做是拋出標準的ActionListener 例

public class MyPanel extends JPanel { 
    private final JComboBox<String> combo1; 
    private final JButton btn2; 

    ....... 
    //catch the actions of inside components 
    btn2.addActionListener(new MyPanelComponentsActionListener()); 
    ........ 

    //assign actionlistener to panel class 
    public void addActionListener(ActionListener l) { 
     listenerList.add(ActionListener.class, l); 
    } 
    public void removeActionListener(ActionListener l) { 
     listenerList.remove(ActionListener.class, l); 
    } 

    //handle registered listeners from components used MyPanel class 
    protected void fireActionPerformed(ActionEvent event) { 
    // Guaranteed to return a non-null array 
    Object[] listeners = listenerList.getListenerList(); 
    ActionEvent e = null; 
    // Process the listeners last to first, notifying 
    // those that are interested in this event 
    for (int i = listeners.length-2; i>=0; i-=2) { 
     if (listeners[i]==ActionListener.class) { 
      // Lazily create the event: 
      if (e == null) { 
        String actionCommand = event.getActionCommand(); 
        if(actionCommand == null) { 
        actionCommand = "FontChanged"; 
        } 
        e = new ActionEvent(FontChooserPanel.this, 
             ActionEvent.ACTION_PERFORMED, 
             actionCommand, 
             event.getWhen(), 
             event.getModifiers()); 
      } 
       // here registered listener executing 
       ((ActionListener)listeners[i+1]).actionPerformed(e); 
      } 
     } 
    } 

//!!! here your event generator 
    class MyPanelComponentsActionListener implements ActionListener { 
    public void actionPerformed(ActionEvent e) { 
     //do something usefull 
     //..... 
     fireActionPerformed(e); 
     } 
    } 
.... 
} 
相關問題