Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.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未侦听事件_Java_Events_Event Handling_Awt - Fatal编程技术网

Java ActionListener未侦听事件

Java ActionListener未侦听事件,java,events,event-handling,awt,Java,Events,Event Handling,Awt,当我在Gui中单击“打开”按钮时,任何人都可以告诉我为什么OpenMenuListener类不发送反馈?“擦除”按钮仍然有效。它给了我一个反馈。我被吓坏了 import java.awt.*; import javax.swing.*; public class DrawingApplication extends JFrame { JComponent drawingArea; class EraseButtonListener implements ActionLis

当我在Gui中单击“打开”按钮时,任何人都可以告诉我为什么OpenMenuListener类不发送反馈?“擦除”按钮仍然有效。它给了我一个反馈。我被吓坏了

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


public class DrawingApplication extends JFrame {

    JComponent drawingArea;

    class EraseButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Clicked erase");
        }
    }

    class OpenMenuListener implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            System.out.println("Clicked open");
    }
    }
        public DrawingApplication() {
        JPanel frame = new JPanel();
        add(frame);
        // panel1.add( new JButton(Figuur),BorderLayout.CENTER);

        drawingArea = new JLabel();
        // label1.add(drawingArea);
        frame.add(drawingArea);

        // Creates a menubar for a JFrame
        JMenuBar menuBar = new JMenuBar();
        // Add the menubar to the frame
        setJMenuBar(menuBar);
        JMenu fileMenu = new JMenu("File");
        menuBar.add(fileMenu);


        JMenu open = new JMenu("Open");
        fileMenu.add(open);
        fileMenu.addSeparator();
        JMenu save = new JMenu("Save");
        fileMenu.add(save);
        fileMenu.addSeparator();
        JMenu close  =new JMenu("Close");
        fileMenu.add(close);
        JMenu helpMenu = new JMenu("Help");

        menuBar.add(helpMenu);
        helpMenu.add(new JMenu("Info"));

        JPanel panel2 = new JPanel();
        add(BorderLayout.SOUTH, frame);
        frame.add(new JLabel("figuurkeuze"));
        frame.add(panel2);
        setVisible(true);

        JRadioButton rectButton = new JRadioButton("Rectangle");
        JRadioButton triangleButton = new JRadioButton("Triangle");
        JRadioButton circleButton = new JRadioButton("Circle");
        frame.add(rectButton);
        frame.add(triangleButton);
        frame.add(circleButton);

        JButton erase = new JButton("Erase");
        frame.add(erase);

        EraseButtonListener eraselistener = new EraseButtonListener();
        erase.addActionListener(eraselistener);

        OpenMenuListener openMenuListener = new OpenMenuListener();
        open.addActionListener(openMenuListener);



    }

    public static void main(String[] args) {

        DrawingApplication frame = new DrawingApplication();
        frame.setTitle("My prgram");
        frame.setSize(400, 300);

        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

}

这似乎是一个名称别名问题,您将侦听器添加到声明为
JMenuItem
open
变量中,因此您将
ActionListener
添加到菜单项,而不是按钮中(由于任何地方都没有
JButton open=new JButton(“open”)

“单击Gui中的“打开”按钮时不发送反馈”
--我看不到“打开”按钮在上面的代码中,您的问题没有意义。这将无法编译--您缺少java.awt.event.ActionListener的导入。请使用SSCCE:当我运行它时,擦除按钮溢出框架外谢谢。实际上,我应该将我的对象实例化为MenuItem而不是Jmenu。非常感谢。