简体   繁体   English

将ActionListener从另一个类添加到JButton

[英]Add an actionListener to a JButton from another class

so I have two classes testPanel and testFrame. 所以我有两个类testPanel和testFrame。 All the buttons are in the testPanel class. 所有按钮都在testPanel类中。 I want to add ActionListeners to the Jbuttons in the testFrame class. 我想将ActionListeners添加到testFrame类的Jbuttons中。 How do I go about doing this? 我该怎么做呢?

testPanel: testPanel:

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: 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();
}

} }

First of all, I would be opposed to simply providing public access to the button in the panel, this leads to too many issues with management and scope of responsibility ... IMHO 首先,我反对仅向面板中的按钮提供公共访问权限,这会导致管理和责任范围过多的问题……恕我直言

You need some kind of reference to the testPane , which would then provide functionality to attach a ActionListener . 您需要某种对testPane的引用,该引用随后将提供附加ActionListener功能。 Then it's up to testPane to manage how that gets done. 然后由testPane管理如何完成。

Here is what you can do : 您可以执行以下操作:

1. First create a class that extends JPanel 1.首先创建一个扩展JPanel的类

2. In that class, define a setActionListener method like this : 2.在该类中,定义一个setActionListener方法,如下所示:

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

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

}

3, In the JFrame class , call the panel's setButtonsActionListener method with an anonymous implementation of the ActionLister interface : 3,在JFrame类中,使用ActionLister接口的匿名实现调用面板的setButtonsActionListener方法:

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

    });

Well you could try this (which would require you to have an instance of the testPanel class and the button1 to be set to public: 好吧,您可以尝试执行此操作(这将需要您拥有testPanel类的实例,并将button1设置为public:

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

Or you could make a function inside of the testPanel that sets the action listener. 或者,您可以在testPanel内部创建一个用于设置操作侦听器的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM