简体   繁体   English

来自另一个 class 的 JButton 的 ActionListener 未触发

[英]ActionListener for JButton from another class not triggering

My main class with a static CardLayout JPanel which includes a JPanel from the Home class.我的主要 class 带有 static CardLayout JPanel,其中包括来自 Home class 的 JPanel。

import java.awt.*;
import javax.swing.*;

public class Runner1 extends JPanel{

    public Runner1() {
        initComponents();
    }

    private void initComponents() {
        setLayout(new CardLayout());

        pnlHome = new Home();
        pnlSignIn = new SignIn();
        add(pnlHome, "Home");
        add(pnlSignIn, "SignIn");
    }

    public static void showJPanel(String s) {
        CardLayout cl = (CardLayout) (pnlRunner.getLayout());
        cl.show(pnlRunner, s);
    }
    
    public static void createAndShowGUI(){
        pnlRunner = new Runner1();
        
        JFrame frame = new JFrame();
        frame.setTitle("Delivery System");
        frame.setSize(new Dimension(500, 400));
        frame.getContentPane().add(pnlRunner);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
    
    public static Runner1 pnlRunner;
    private JPanel pnlHome;
    private JPanel pnlSignIn;
}

My other class where the actionlistener for the JButton doesn't get triggered, when debugging, the btnNewOrderActionPerformed doesn't get executed.我的另一个 class 没有触发 JButton 的动作监听器,在调试时,btnNewOrderActionPerformed 没有被执行。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Home extends JPanel  {
    public Home() {
        initComponents();
    }
    
    private void initComponents(){
        
        setLayout(new BorderLayout());
        
        add(new TextArea("Active Orders"),BorderLayout.CENTER);
        
        JButton btnNewOrder1 = new JButton("New Order");
        btnNewOrder1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                btnNewOrderActionPerformed(e);
            }
        });
        add(btnNewOrder1, BorderLayout.PAGE_END);
    }
    
    private void btnNewOrderActionPerformed(ActionEvent e){
        System.out.println("test");
    }
    
    private OrderMap[] JOrders; //Lists of JPanel of orders
    private JButton btnNewOrder;
}

Another question regarding static implementation of the CardLayout JPanel, is there a non-static way of accomplishing the same thing (where the shown JPanel can be controlled by components from external classes)?关于 CardLayout JPanel 的 static 实现的另一个问题,是否有一种非静态的方式来完成同样的事情(显示的 JPanel 可以由外部类的组件控制)?

My other class where the actionlistener for the JButton doesn't get triggered我的其他 class JButton 的动作监听器没有被触发

Works fine for me, after fixing up the code so it compiles.在修复代码以便编译之后,对我来说工作正常。 See comment of your initial question.请参阅您最初问题的评论。

is there a non-static way of accomplishing the same thing (where the shown JPanel can be controlled by components from external classes)?是否有一种非静态的方式来完成同样的事情(显示的 JPanel 可以由外部类的组件控制)?

First, get rid of the static keyword on the method.首先,去掉方法上的 static 关键字。

Then you have a couple of options:然后你有几个选择:

  1. pass a reference of the Runner1 class to each child panel.将 Runner1 class 的引用传递给每个子面板。
  2. in the child panel you can use the getParent() method to get a reference to the Runner1 class.在子面板中,您可以使用getParent()方法获取对 Runner1 class 的引用。

Once you have a reference you can then reference any method in the Runner1 class.获得参考后,您可以参考 Runner1 class 中的任何方法。

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

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