简体   繁体   English

JInternalFrame中的JButton不会触发ActionListener

[英]JButton inside JInternalFrame won't trigger the ActionListener

I wanted to check if ActionListener under my InternalFrame if it's working. 我想检查InternalFrame下的ActionListener是否正常。 But the inner class that implementing ActionListener won't read the button that registered. 但是实现ActionListener的内部类不会读取注册的按钮。 Any reasons why? 有什么原因吗?

EmployeeFrameView EmployeeFrameView

public class EmployeeFrameView extends JInternalFrame
{
   JButton button;

   public EmployeeFrameView()
   {
    super("AddEmployee",true,true,true,true);
    addComponentsToPane(getContentPane());
    pack();
    setVisible(true);
    setLocation(xOffset,yOffset);
   }

   private void addComponentsToPane(final Container pane)
   {
    final JPanel content = new JPanel();
    panelEmployee = new JPanel();

    //Add to content and set layout
    content.setLayout(new BorderLayout());
    content.add(addComponentsToEmployeePanel(panelEmployee),BorderLayout.NORTH);

    //Adding ScrollPane to Container.
    final JScrollPane scrollPane = new JScrollPane(content, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    pane.add(scrollPane);
   }

   private JPanel addComponentsToEmployeePanel(JPanel panelEmployee)
   {
     panelEmployee.setLayout(grid);
     gbc.gridx = 0;
     gbc.gridy = 1;
     button = new JButton("Button");
     panelEmployee.add(button, gbc);
   }

   public void addAction(ActionListener l )
   {
    button.addActionListener(l);
   }
}

EmployeeFrameController EmployeeFrameController

public class EmployeeFrameController 
{
EmployeeFrameView theView;

public EmployeeFrameController(EmployeeFrameView theView)
{
    this.theView = theView;

    this.theView.addAction(new addAction());
}

class addAction implements ActionListener
{
    @Override
    public void actionPerformed(ActionEvent e) 
    {
        System.out.println("Working");
    }

}

}

Main 主要

public class Main
{
public static void main(String[] args)
{
   EmployeeFrameView employeeFrameView = new EmployeeFrameView();

   EmployeeFrameController employeeFrameController = new EmployeeFrameController(employeeFrameView);
}
}

I can't see any problem with the code you posted. 我看不到您发布的代码有任何问题。 Probably your issue is somewhere in the code your didn't include here (I would bet in something messing up references, as you didn't include the codification in which the button is added into the screen.). 可能您的问题在代码中未包含的位置(我敢打赌会弄乱引用,因为您未包含将按钮添加到屏幕中的代码)。

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

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