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

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

Java Swing - 從處理程序類重繪

Java Swing - 從處理程序類重繪

紅顏莎娜 2023-12-21 10:44:30
因此,我首先將我的兩個類的代碼放在這里。SquareSimp.javaimport javax.swing.*;import java.awt.*;import java.awt.event.*;public class SquareSimp{    public static void main( String[] args )    {        FilledFrame frame = new FilledFrame();        frame.setVisible( true );    }}class FilledFrame extends JFrame{    int size = 400;    public FilledFrame()    {        JButton butSmall   = new JButton("Small");        JButton butMedium  = new JButton("Medium");        JButton butLarge   = new JButton("Large");        JButton butMessage = new JButton("Say Hi!");        SquarePanel panel = new SquarePanel(this);        JPanel butPanel = new JPanel();        butSmall.addActionListener(new ButtonHandler1(this, 200){            @Override            public void actionPerformed(ActionEvent actionEvent) {                size = 200;                panel.repaint();            }        });        butMedium.addActionListener(new ButtonHandler1(this, this.size){            @Override            public void actionPerformed(ActionEvent actionEvent) {                size = 300;                panel.repaint();            }        });        butLarge.addActionListener(new ButtonHandler1(this, this.size){            @Override            public void actionPerformed(ActionEvent actionEvent) {                size = 400;                panel.repaint();            }        });        butPanel.add(butSmall);        butPanel.add(butMedium);        butPanel.add(butLarge);        butPanel.add(butMessage);        add(butPanel, BorderLayout.NORTH);        add(panel, BorderLayout.CENTER);        setSize( size+100, size+100 );        setDefaultCloseOperation(EXIT_ON_CLOSE);    }}到目前為止,一切正常,這很棒。然而,根據要求,我被要求為每個類制作一個按鈕處理程序。有人可以向我解釋一下我的 ButtonHandler 在這里實際上在做什么嗎?因為我覺得我可以用更好的方法來完成它(在按鈕處理程序類中創建事件并根據按下的按鈕影響其大?。皇莿摻涿瘮挡⒏采w actionPerformed 事件。我不知道該怎么做,所以任何解釋的幫助都會很棒!
查看完整描述

3 回答

?
烙印99

TA貢獻1829條經驗 獲得超13個贊

ButtonHandler1并沒有真正使用,因為傳遞給它的參數從未使用過,并且在構造它時它的單個方法被覆蓋。

所以這:


     butSmall.addActionListener(new ButtonHandler1(this, 200){

            @Override

            public void actionPerformed(ActionEvent actionEvent) {

                size = 200;

                panel.repaint();

            }

      });

無需構建即可編寫ButtonHandler1:


    butSmall.addActionListener(new ActionListener(){

        @Override

        public void actionPerformed(ActionEvent actionEvent) {

            size = 200;

            panel.repaint();

        }

    });

或者使用 lambda 表達式:


    butSmall.addActionListener(actionEvent -> {

        size = 200;

        panel.repaint();

    });

有很多方法可以實現您想要的功能。根據您所寫的內容,您可以ButtonHandler1這樣定義:


class ButtonHandler1 implements ActionListener{

    private final FilledFrame theApp;

    private final int theSize;

    ButtonHandler1(FilledFrame app, int size){

        theApp = app;

        theSize = size;

    }


    @Override

    public void actionPerformed(ActionEvent actionEvent) {

        theApp.size = theSize; //better use a setter in FilledFrame

        theApp.repaint();

    }

}

并像這樣使用它:


    butSmall.addActionListener(new ButtonHandler1(this, 200));


    butMedium.addActionListener(new ButtonHandler1(this, 300));


    butLarge.addActionListener(new ButtonHandler1(this, 400));

創建ButtonHandler1一個內部類FilledFrame使事情變得更簡單:


class ButtonHandler1 implements ActionListener{

    private final int theSize;

    ButtonHandler1(int size){

        theSize = size;

    }


    @Override

    public void actionPerformed(ActionEvent actionEvent) {

        size = theSize;

        repaint();

    }

}

通過以下方式使用它:


    butSmall.addActionListener(new ButtonHandler1(200));


    butMedium.addActionListener(new ButtonHandler1(300));


    butLarge.addActionListener(new ButtonHandler1(400));


查看完整回答
反對 回復 2023-12-21
?
慕后森

TA貢獻1802條經驗 獲得超5個贊

a 的任何處理程序*Listener都用于處理events從正在偵聽的對象生成的處理程序。在主程序中,似乎有幾個可以改變窗口大小的按鈕。


對于實現 ButtonHandler 的類,您沒有執行任何操作,因為您的actionPerformed方法沒有執行任何操作。


并且anonymous classes是實現偵聽器的可接受的方式。但是,我更喜歡使用,inner classes因為它們更干凈(恕我直言)并且仍然可以state訪問enclosing class.


您的處理程序類應如下所示:


// This is a class whose object will handle the event.

class ButtonHandler1 implements ActionListener {

   private FilledFrame theApp;

   private int         theSize;


   ButtonHandler1(FilledFrame app, int size) {

      theApp = app;

      theSize = size;

   }


   public void actionPerformed(ActionEvent actionEvent) {

      theApp.size = theSize;

      theApp.repaint();

   }

}

以下是將該處理程序添加到按鈕的調用。


      butSmall.addActionListener(new ButtonHandler1(this, 200));

      butMedium.addActionListener(new ButtonHandler1(this, 300));

      butLarge.addActionListener(new ButtonHandler1(this, 400));


查看完整回答
反對 回復 2023-12-21
?
慕森卡

TA貢獻1806條經驗 獲得超8個贊

我不確定我是否理解這個問題,但有兩種方法可以重構代碼:


完全刪除您的ButtonHandler1類并實現匿名類中的所有邏輯:

    //FilledFrame.java


    butSmall.addActionListener(new ActionListener(){

                @Override

                public void actionPerformed(ActionEvent actionEvent) {

                    FilledFrame.this.size = 200;

                    panel.repaint();

                }

            });


您可以在類上添加一些 getter 和 setter 方法FilledFrame,并調用它們ButtonHandler1.actionPerformed來實現其中的邏輯,如下所示:

    package Lab2;


    import javax.swing.*;

    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    // This is a class whose object will handle the event.

    public class ButtonHandler1 implements ActionListener{

        private FilledFrame theApp;

        private int theSize;

        ButtonHandler1(FilledFrame app, int size){

            theApp = app;

            theSize = size;

        }


        public void actionPerformed(ActionEvent actionEvent) {

            theApp.setSizeMember(200); //do not use theApp.setSize(), create a method with a different name

            theApp.getSquarePanel().repaint();

        }


    }

我將創建 getter/setter 的工作留給FilledFrame您。


查看完整回答
反對 回復 2023-12-21
  • 3 回答
  • 0 關注
  • 214 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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