为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

一只萌萌小番薯 2022-09-21 16:43:16
动作听镜从未在本地使用过,我知道我错过了一些东西,但我无法弄清楚。我不明白为什么当它已经添加到动作列表时,它从未被使用过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() 方法,这将创建一个简单的图形
查看完整描述

1 回答

?
湖上湖

TA贡献2003条经验 获得超2个赞

您有两个操作侦听器和 ,并且您只使用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


查看完整回答
反对 回复 2022-09-21
  • 1 回答
  • 0 关注
  • 56 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信