3 回答

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

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

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您。
添加回答
舉報