2010-08-12 78 views

回答

2

除非我看到這個錯誤,否則當你自己添加了JPanel時,你可以添加一個actionlistener到按鈕。

JButton.addActionListener(... some listener); 

或者是你在這裏問的其他東西?例如如果自定義JPanel不是由您開發的。那麼在這種情況下,看是否面板暴露了一個API的偵聽器添加到它的按鈕,如果不是那麼最後一個選項是遍歷它的孩子找到一個JButton:

Component[] comp = customPanel.getComponents(); 
for(Component c: comp) { 
    if(c is a button i am interested in) { 
    c.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      // implement the logic of what happens when button is clicked! 
     } 
    }); 
    } 
} 
+0

我如何實現對API一個動作監聽器? (我也是JPanel組件的開發者) – 2010-08-12 06:40:50

+0

@whydna我修改了我的代碼來添加一個動作偵聽器 – naikus 2010-08-12 06:47:09