Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在另一个类中定义ActionListener时发生AWT-EVENT-0错误_Java_Swing - Fatal编程技术网

Java 在另一个类中定义ActionListener时发生AWT-EVENT-0错误

Java 在另一个类中定义ActionListener时发生AWT-EVENT-0错误,java,swing,Java,Swing,我是Java新手,我希望在JFrameframe中的按钮上添加一个操作 当我在同一个类中使用actionListener时,一切都很好,但是当我将它放在另一个名为Orga_Listener的类中以使代码更干净时,我得到了“awt-event-0”错误。我知道这是因为我的新班级没有收到这个动作 有人能解释一下我如何让另一个类中定义的ActionListener与我的FrameOrganizationJFrame中的按钮一起工作吗 下面是frame和myActionListner派生类的源代码 im

我是Java新手,我希望在
JFrame
frame中的按钮上添加一个操作

当我在同一个类中使用actionListener时,一切都很好,但是当我将它放在另一个名为
Orga_Listener
的类中以使代码更干净时,我得到了“awt-event-0”错误。我知道这是因为我的新班级没有收到这个动作

有人能解释一下我如何让另一个类中定义的
ActionListener
与我的
FrameOrganization
JFrame
中的按钮一起工作吗

下面是frame和my
ActionListner
派生类的源代码

import ActionListener.Orga_Listener;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import javax.swing.*;


public class FrameOrganisation extends JFrame {
  private JTextField jtf = new JTextField("");
  private JLabel label = new JLabel("TITRE");

  public FrameOrganisation() {
      createAndShowGUI();
  }

  private void createAndShowGUI() {

      setTitle("Organisation");
      setSize(400,400);
      setVisible(true);
      setLocationRelativeTo(null);

      JPanel top = new JPanel();
      Font police = new Font("Arial", Font.BOLD, 14);
      jtf.setFont(police);
      jtf.setPreferredSize(new Dimension(100, 20));
      top.add(label);
      top.add(jtf);
      getContentPane().add(top, BorderLayout.PAGE_START);

      JPanel buttonPane = new JPanel();
      JButton button = new JButton("Terminé");
      buttonPane.add(button);

      button.addActionListener(new Orga_Listener());
      getContentPane().add(buttonPane, BorderLayout.PAGE_END);
      setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

@Override
public JRootPane createRootPane() {
    JRootPane rootPan = new JRootPane();
    KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE");
    Action action = new AbstractAction() {

        private static final long serialVersionUID = 1L;

        public void actionPerformed(ActionEvent e) {
            setVisible(false);
            dispose();
        }                
    };
    InputMap inputMap = rootPan.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(stroke, "ESCAPE");
    rootPan.getActionMap().put("ESCAPE", action);
    return rootPan;
  }
}
第二个:

public class Orga_Listener implements ActionListener { 
  private FrameOrganisation dialog;

    //close and dispose of the window.
    public void actionPerformed(ActionEvent e) {
        dialog.setVisible(false);
        dialog.dispose();
    }
}
我得到的错误是:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ActionListener.Orga_Listener.actionPerformed(Orga_Listener.java:21)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6297)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
at java.awt.Component.processEvent(Component.java:6062)
at java.awt.Container.processEvent(Container.java:2039)
at java.awt.Component.dispatchEventImpl(Component.java:4660)
at java.awt.Container.dispatchEventImpl(Container.java:2097)
at java.awt.Component.dispatchEvent(Component.java:4488)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4575)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4236)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4166)
at java.awt.Container.dispatchEventImpl(Container.java:2083)
at java.awt.Window.dispatchEventImpl(Window.java:2489)
at java.awt.Component.dispatchEvent(Component.java:4488)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:674)
at java.awt.EventQueue.access$400(EventQueue.java:81)
at java.awt.EventQueue$2.run(EventQueue.java:633)
at java.awt.EventQueue$2.run(EventQueue.java:631)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:647)
at java.awt.EventQueue$3.run(EventQueue.java:645)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:644)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

问题是您试图在对话框上调用一个方法,但没有对该对话框的引用

一种解决方案是使用
ActionEvent
中的信息查找对话框:

Component component = (Component)e.getSource();
Window window = SwingUtilities.windowForComponent( component );
window.setVisible( false );
window.dispose();

什么错误?是否有堆栈跟踪?抱歉,我忘记添加错误。我编辑了这条消息,现在应该更好了,看到了吗