2013-09-30 47 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

我知道会的,但我不知道他们是如何去做这件事的,所以我建议,但是谢谢你指定我的缺陷 –

相关问题