第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決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);    }}到目前為止,一切正常,這很棒。然而,根據(jù)要求,我被要求為每個類制作一個按鈕處理程序。有人可以向我解釋一下我的 ButtonHandler 在這里實際上在做什么嗎?因為我覺得我可以用更好的方法來完成它(在按鈕處理程序類中創(chuàng)建事件并根據(jù)按下的按鈕影響其大?。?,而不是創(chuàng)建匿名函數(shù)并覆蓋 actionPerformed 事件。我不知道該怎么做,所以任何解釋的幫助都會很棒!
查看完整描述

3 回答

?
烙印99

TA貢獻1829條經(jīng)驗 獲得超13個贊

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

所以這:


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

            @Override

            public void actionPerformed(ActionEvent actionEvent) {

                size = 200;

                panel.repaint();

            }

      });

無需構(gòu)建即可編寫B(tài)uttonHandler1:


    butSmall.addActionListener(new ActionListener(){

        @Override

        public void actionPerformed(ActionEvent actionEvent) {

            size = 200;

            panel.repaint();

        }

    });

或者使用 lambda 表達式:


    butSmall.addActionListener(actionEvent -> {

        size = 200;

        panel.repaint();

    });

有很多方法可以實現(xiàn)您想要的功能。根據(jù)您所寫的內(nèi)容,您可以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));

創(chuàng)建ButtonHandler1一個內(nèi)部類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條經(jīng)驗 獲得超5個贊

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


對于實現(xiàn) ButtonHandler 的類,您沒有執(zhí)行任何操作,因為您的actionPerformed方法沒有執(zhí)行任何操作。


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


您的處理程序類應(yīng)如下所示:


// 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();

   }

}

以下是將該處理程序添加到按鈕的調(diào)用。


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

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

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


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

TA貢獻1806條經(jīng)驗 獲得超8個贊

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


完全刪除您的ButtonHandler1類并實現(xiàn)匿名類中的所有邏輯:

    //FilledFrame.java


    butSmall.addActionListener(new ActionListener(){

                @Override

                public void actionPerformed(ActionEvent actionEvent) {

                    FilledFrame.this.size = 200;

                    panel.repaint();

                }

            });


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

    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();

        }


    }

我將創(chuàng)建 getter/setter 的工作留給FilledFrame您。


查看完整回答
反對 回復 2023-12-21
  • 3 回答
  • 0 關(guān)注
  • 211 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號