3 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超13個(gè)贊
ButtonHandler1并沒(méi)有真正使用,因?yàn)閭鬟f給它的參數(shù)從未使用過(guò),并且在構(gòu)造它時(shí)它的單個(gè)方法被覆蓋。
所以這:
butSmall.addActionListener(new ButtonHandler1(this, 200){
@Override
public void actionPerformed(ActionEvent actionEvent) {
size = 200;
panel.repaint();
}
});
無(wú)需構(gòu)建即可編寫(xiě)B(tài)uttonHandler1:
butSmall.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent actionEvent) {
size = 200;
panel.repaint();
}
});
或者使用 lambda 表達(dá)式:
butSmall.addActionListener(actionEvent -> {
size = 200;
panel.repaint();
});
有很多方法可以實(shí)現(xiàn)您想要的功能。根據(jù)您所寫(xiě)的內(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一個(gè)內(nèi)部類FilledFrame使事情變得更簡(jiǎn)單:
class ButtonHandler1 implements ActionListener{
private final int theSize;
ButtonHandler1(int size){
theSize = size;
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
size = theSize;
repaint();
}
}
通過(guò)以下方式使用它:
butSmall.addActionListener(new ButtonHandler1(200));
butMedium.addActionListener(new ButtonHandler1(300));
butLarge.addActionListener(new ButtonHandler1(400));

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊
a 的任何處理程序*Listener都用于處理events從正在偵聽(tīng)的對(duì)象生成的處理程序。在主程序中,似乎有幾個(gè)可以改變窗口大小的按鈕。
對(duì)于實(shí)現(xiàn) ButtonHandler 的類,您沒(méi)有執(zhí)行任何操作,因?yàn)槟腶ctionPerformed方法沒(méi)有執(zhí)行任何操作。
并且anonymous classes是實(shí)現(xiàn)偵聽(tīng)器的可接受的方式。但是,我更喜歡使用,inner classes因?yàn)樗鼈兏蓛簦ㄋ∥抑毖裕┎⑶胰匀豢梢詓tate訪問(wèn)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));

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超8個(gè)贊
我不確定我是否理解這個(gè)問(wèn)題,但有兩種方法可以重構(gòu)代碼:
完全刪除您的ButtonHandler1類并實(shí)現(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來(lái)實(shí)現(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您。
添加回答
舉報(bào)