从未在本地使用的操作侦听器

动作听镜从未在本地使用过,我知道我错过了一些东西,但我无法弄清楚。我不明白为什么当它已经添加到动作列表时,它从未被使用过


public class GUIinterface extends JFrame{


    JMenuBar MenuBar;

    JMenu file;

    JMenu help;

    JMenuItem load;

    JMenuItem save;

    JMenuItem about;

    JTextField commandLine;

    GraphicsPanel gp;


    public GUIinterface() {

        setTitle("Menu");

        setSize(640,480);

        setLocationRelativeTo(null);

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        gp = new GraphicsPanel();

        add(gp);


        MenuBar = new JMenuBar();

        file = new JMenu("File");

        help = new JMenu("Help");

        load = new JMenuItem("Load");

        save = new JMenuItem("Save");

        about = new JMenuItem("About");

        commandLine = new JTextField();


        file.add(load);

        file.add(save);

        help.add(about);


        MenuBar.add(file);

        MenuBar.add(help);


        add(MenuBar);

        add(commandLine, BorderLayout.PAGE_END);


        file.addMenuListener(new myMenuListener());

        load.addActionListener(new myActionListener());

        save.addActionListener(new myActionListener());

        about.addActionListener(new myActionListener());


        setJMenuBar(MenuBar);

        setVisible(true);


        commandLine.addActionListener(new myActionListener());

    }


    private class myActionListener implements ActionListener{

        private class MyActionListener implements ActionListener {

            @Override

            public void actionPerformed(ActionEvent e) {

                if (e.getSource() == about) {

                    System.out.println("A");

                    gp.about();

                }

            }

        }            

当按下时,侦听器应调用 gp.about() 方法,这将创建一个简单的图形


一只萌萌小番薯
浏览 77回答 1
1回答

湖上湖

您有两个操作侦听器和 ,并且您只使用MyActionListenermyActionListenermyActionListener尝试将两者统一为一,就会有这样的东西:private class myActionListener implements ActionListener{    public void actionPerformed(ActionEvent e) {        System.out.println(commandLine.getText());        if (e.getSource() == about) {            System.out.println("A");            gp.about();        }        else if (e.getSource() == commandLine)        {            if (commandLine.getText().contains("penup"))            {                gp.penUp();                commandLine.setText("");            }            else if (commandLine.getText().contains("pendown"))            {                gp.penDown();                commandLine.setText("");            }            else if (commandLine.getText().contains("turnleft"))            {                gp.turnLeft();                commandLine.setText("");            }            else if (commandLine.getText().contains("turnright"))            {                gp.turnRight();                commandLine.setText("");            }            else if (commandLine.getText().contains("forward"))            {                gp.forward(50);                commandLine.setText("");            }            else if (commandLine.getText().contains("backward"))            {                gp.forward(-50);                commandLine.setText("");            }            else if (commandLine.getText().contains("black"))            {                gp.setPenColour(Color.black);                commandLine.setText("");            }            else if (commandLine.getText().contains("red"))            {                gp.setPenColour(Color.red);                commandLine.setText("");            }             else if (commandLine.getText().contains("green"))            {                gp.setPenColour(Color.green);                commandLine.setText("");            }         }    }}编辑:通过在爪哇语中命名修道院类名应为名词,大小写混合,每个内部单词的第一个字母大写。尽量保持类名简单和描述性。使用整个单词 - 避免使用首字母缩略词和缩写(除非缩写比长格式更广泛地使用,例如URL或HTML)。所以我建议你使用蜈螈MyActionListenermyActionListener
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java