0

任何人都可以说出为什么当我单击我的 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);

    }

}
4

1 回答 1

0

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

于 2012-12-13T18:06:16.163 回答