亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從未在本地使用的操作偵聽器

從未在本地使用的操作偵聽器

一只萌萌小番薯 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 關注
  • 91 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號