【问题标题】:Add an actionListener to a JButton from another class将 actionListener 添加到另一个类的 JButton
【发布时间】:2013-10-06 21:27:59
【问题描述】:

所以我有两个类 testPanel 和 testFrame。所有按钮都在 testPanel 类中。我想将 ActionListeners 添加到 testFrame 类中的 Jbuttons 中。我该怎么做呢?

测试面板:

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

测试帧:

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

}

【问题讨论】:

  • 您将想要尝试 MadProgrammer 和/或 YAT 的建议,或者如果失败,请展示您尝试执行他们的建议以及您在此尝试中遇到的问题。

标签: java swing awt actionlistener


【解决方案1】:

首先,我反对简单地提供对面板中按钮的公共访问权限,这会导致管理和责任范围出现太多问题......恕我直言

您需要某种对testPane 的引用,然后它会提供附加ActionListener 的功能。然后由testPane 管理如何完成。

【讨论】:

    【解决方案2】:

    您可以这样做:

    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类中,调用面板的setButtonsActionListener方法,匿名实现ActionLister接口:

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

    【讨论】:

    • 我尝试在testPanel类中添加方法,但是NetBeans找不到setActionListener();
    • 对不起,我弄错了,是 addActionListener 不是 setActionListener
    • 你应该学会从 javadoc 中读取对象类型中可用的方法列表,(只需 google JButton javadoc)
    【解决方案3】:

    你可以试试这个(这需要你有一个 testPanel 类的实例,并且 button1 被设置为 public:

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

    或者你可以在 testPanel 中创建一个函数来设置动作监听器。

    【讨论】:

    • 这样会破坏封装,不推荐。
    • 我知道它会,但我不知道他们是如何尝试这样做的,所以我建议这样做,但感谢您指出我的缺陷
    猜你喜欢
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 2023-02-02
    • 2010-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-10
    相关资源
    最近更新 更多