简体   繁体   English

按钮ActionListener未在扩展JPanel的类内部调用

[英]Button ActionListener not called inside class extending JPanel

I have a main frame, called FrontGUI, with a side panel added to it. 我有一个名为FrontGUI的主框架,并添加了一个侧面板。 This side panel is a class called FrontGUILinker that extends JPanel, and has a button on it. 这个侧面板是一个名为FrontGUILinker的类,它扩展了JPanel,并在其上有一个按钮。 When I run the program, the button doesn't do anything - the ActionListener doesn't ever seem to be called. 当我运行程序时,按钮不会执行任何操作 - 似乎似乎没有调用ActionListener。 I have several other components with similar setups, and the buttons all work on them - the only difference I can see is that they are extensions of JFrame, and they aren't direct fields of FrontGUI but instead of the maingui mentioned in the code below. 我有其他几个具有类似设置的组件,按钮都可以使用它们 - 唯一的区别是我们可以看到它们是JFrame的扩展,它们不是FrontGUI的直接字段,而是代替下面代码中提到的maingui 。 This maingui contains fields with Controller classes for each frame, including FrontGUIController, but the FrontGUILinkerController is a field in FrontGUIController. 这个maingui包含每个帧的Controller类,包括FrontGUIController,但FrontGUILinkerController是FrontGUIController中的一个字段。 Here are outlines of my classes, with hopefully just the unrelated things left out. 以下是我班级的概述,希望只是遗漏了无关的事情。 The main JFrame class: 主要的JFrame类:

public class FrontGUI extends JFrame {
    public FrontGUILinker linkerPanel;
    public JButton btnShowhideLinker;

    public FrontGUI() {
      linkerPanel = new FrontGUILinker();
      contentPane.add(linkerPanel, BorderLayout.EAST);

      btnShowhideLinker = new JButton("Show/Hide Linker");
      contentPane.add(btnShowhideLinker);
    }
}

Here's it's "controller" class. 这是它的“控制器”类。 The side panel can be shown or not shown, which is what this action listener does, and this seems to work just fine. 侧面板可以显示或不显示,这是这个动作监听器所做的,这似乎工作得很好。

public class FrontGUIController {
    public MAINGUI maingui;
    private FrontGUI frame;
    public FrontGUILinkerController linkerController;

    public FrontGUIController(MAINGUI parent) {
       maingui = parent;
       frame = new FrontGUI();
       linkerController = new FrontGUILinkerController(maingui);

       //Button: Show/Hide linkerPanel
       frame.btnShowhideLinker.addActionListener( new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent arg0) {
           frame.linkerPanel.setVisible(!frame.linkerPanel.isVisible());
       }});

    }

}

Here is the FrontGUILinker class: 这是FrontGUILinker类:

public class FrontGUILinker extends JPanel {
   public btnCreateLink;

   public FrontGUILinker() {
       btnCreateLink = new JButton("Create Link");
       add(btnCreateLink);
   }
}

Here is the controller for that class: 这是该类的控制器:

public class FrontGUILinkerController {
   public MAINGUI maingui;
   private FrontGUILinker frame;

   public FrontGUILinkerController(MAINGUI parent) {
      maingui = parent;
      frame = new FrontGUILinker();

      // Add listener to Create Link button
      frame.btnCreateLink.addActionListener( new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent arg0) {
             System.out.println("Create Link button has been clicked");
         }});
   }
}

Anybody have an idea why this isn't working? 任何人都知道为什么这不起作用?

You're creating FrontGUILinker instances twice , one you're displaying and one you're adding an ActionListener to. 您正在创建两次 FrontGUILinker实例,一个是您正在显示的,另一个是您要添加一个ActionListener。 Don't do that. 不要那样做。 Create this object once and use the same reference for both. 创建此对象一次,并对两者使用相同的引用。 To show that I'm right, just look at the code in your question above, and search out where you call new FrontGUILinker() and you'll see that you do it twice, here: 要证明我是对的,只需查看上面问题中的代码,然后搜索你调用new FrontGUILinker() ,你就会看到你做了两次,这里:

public FrontGUI() {
  linkerPanel = new FrontGUILinker();

and again here: 再来一次:

public FrontGUILinkerController(MAINGUI parent) {
  maingui = parent;
  frame = new FrontGUILinker();

Each instance is completely distinct from the other, and so adding an ActionListener to one will have no effect on the other one (the displayed one). 每个实例都与另一个实例完全不同,因此向一个实例添加一个ActionListener对另一个实例(显示的实例)没有任何影响。 To solve this, pass valid references to where they're needed. 要解决此问题,请将有效引用传递到需要的位置。

ie,

public class FrontGUILinkerController {
   public MAINGUI maingui;
   private FrontGUILinker frame;

   // **** note change
   public FrontGUILinkerController(MAINGUI parent, FrontGUILiner frame) {
      maingui = parent;
      this.frame = frame;  // **** added
      // frame = new FrontGUILinker(); // **** commented out

      // Add listener to Create Link button
      frame.btnCreateLink.addActionListener( new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent arg0) {
             System.out.println("Create Link button has been clicked");
         }});
   }
}

and

public FrontGUIController(MAINGUI parent) {
   maingui = parent;
   frame = new FrontGUI();
   linkerController = new FrontGUILinkerController(maingui, frame); // **** changed

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

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