2013-09-30 49 views
1

所以我有兩個類testPanel和testFrame。所有按鈕都在testPanel類中。我想添加ActionListeners到testFrame類中的Jbuttons。我如何去做這件事?從另一個類添加一個actionListener到JButton

パ:

public class testPanel extends JPanel{ 

JLabel codeLbl = new JLabel("Code"); 
JLabel titleLbl = new JLabel("Title"); 
JLabel priceLbl = new JLabel("Price"); 

JTextField codeTxt = new JTextField(20); 
JTextField titleTxt = new JTextField(20); 
JTextField priceTxt = new JTextField(20); 

JButton addBtn = new JButton("Add"); 
JButton updateBtn = new JButton("Update"); 
JButton delBtn = new JButton("Delete"); 
JButton exitBtn = new JButton("Exit"); 
JButton firstBtn = new JButton("First"); 
JButton prevBtn = new JButton("Previous"); 
JButton nextBtn = new JButton("Next"); 
JButton lastBtn = new JButton("Last"); 

JPanel info = new JPanel(); 
JPanel buttons = new JPanel(); 

public testPanel(){ 
    info.setLayout(new GridLayout(3,2)); 

    info.add(codeLbl); 
    info.add(codeTxt); 
    info.add(titleLbl); 
    info.add(titleTxt); 
    info.add(priceLbl); 
    info.add(priceTxt); 

    buttons.setLayout(new GridLayout(2,4)); 

    buttons.add(addBtn); 
    buttons.add(updateBtn); 
    buttons.add(delBtn); 
    buttons.add(exitBtn); 
    buttons.add(firstBtn); 
    buttons.add(prevBtn); 
    buttons.add(nextBtn); 
    buttons.add(lastBtn); 

    JPanel container = new JPanel(); 
    container.setLayout(new BorderLayout()); 

    container.add(BorderLayout.CENTER, info); 
    container.add(BorderLayout.SOUTH, buttons); 

    add(container); 
} 
} 

testFrame:

public class testFrame extends JFrame{ 
JPanel p = new testPanel(); 

public testFrame(){ 
    super("BLAH");   

    this.getContentPane().add(p);setVisible(true); 
    pack(); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 

public static void main(String[] args){ 
    new testFrame(); 
} 

}

+0

你會想嘗試MadProgrammer和/或YAT的建議,或做不到這一點,顯示你嘗試做什麼,他們所提出的建議,你用這種嘗試有問題​​。 –

回答

1

首先,我會反對簡單地提供在面板公衆訪問按鈕,這導致與管理層和責任範圍......恕我直言

你的東東太多的問題d對testPane的某種引用,然後它將提供附加ActionListener的功能。那麼最高達到testPane來管理如何完成。

0

這裏是你可以做什麼:

1.首先創建一個擴展JPanel

2.在類的類,定義的setActionListener方法是這樣的:

public void setButtonsActionListener(ActionListener listener){ 
    // and in here set your buttons action listeners 

    button1.addActionListener(listener); 
    button2.addActionListener(listener); 
    ... 

} 

3,在JFrame類,使用匿名實現的ActionLister接口調用面板的setButtonsActionListener方法:

thePanel.setButtonsActionListener(new ActionListener(){ 
     @Override 
     void actionPerformed(ActionEvent e){ 
      // here do what you gotta do when the button is clicked 
     } 

    }); 
+0

我嘗試在testPanel類中添加方法,但NetBeans找不到setActionListener(); – kyros

+0

抱歉,我犯了一個錯誤,它的addActionListener沒有的setActionListener – Turkish

+0

,你應該學會閱讀的Javadoc中的對象類型可用方法的列表,(只是谷歌的JButton的javadoc) – Turkish

-1

那麼你可以試試這個(這需要你有パ類的實例和Button1以設置爲公開:

testFrame.button1.setActionListener(new ActionListener(@Override public void actionPerformed(ActionEvent event){}}); 

或者你可以作出這樣的設置操作聽者パ的內部函數。

+0

這會破壞封裝,因此不推薦。 –

+0

我知道會的,但我不知道他們是如何去做這件事的,所以我建議,但是謝謝你指定我的缺陷 –

相關問題