【问题标题】:Java ActionListener not listening to eventJava ActionListener 不监听事件
【发布时间】:2012-12-01 16:33:56
【问题描述】:

当我单击我的 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);

    }

}

【问题讨论】:

  • "doesnt send a feedback when i click the open button in my Gui " -- 我在上面的代码中没有看到打开的“按钮”,所以你的问题没有意义。
  • 这将无法编译——您缺少 java.awt.event.ActionListener 的导入。请使用 SSCCE:sscce.org
  • 当我运行它时,擦除按钮溢出框架

标签: java events event-handling awt


【解决方案1】:

这似乎是名称别名的问题,您将侦听器添加到声明为JMenuItemopen 变量中,因此您将ActionListener 添加到菜单项而不是按钮(您永远不要声明,因为任何地方都没有JButton open = new JButton("open"))。

【讨论】:

  • 谢谢。事实上,我应该将我的对象实例化为 MenuItem 而不是 Jmenu。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-09
  • 1970-01-01
  • 2023-03-14
  • 2018-10-18
  • 1970-01-01
相关资源
最近更新 更多