JInternalFrame中的JButton不会触发ActionListener

时间:2016-12-22 15:09:09

标签: java swing

我想检查我的InternalFrame下的ActionListener是否正常工作。但是实现ActionListener的内部类不会读取已注册的按钮。有什么原因吗?

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

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

}

}

主要

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

   EmployeeFrameController employeeFrameController = new EmployeeFrameController(employeeFrameView);
}
}

1 个答案:

答案 0 :(得分:0)

我发现您发布的代码没有任何问题。可能你的问题出现在你没有在这里包含的代码中的某个地方(我会打赌搞乱引用的东西,因为你没有包含按钮被添加到屏幕中的编码。)。