Events 对多个操作使用一个事件处理程序

Events 对多个操作使用一个事件处理程序,events,event-handling,javafx,Events,Event Handling,Javafx,今天我做了一些家庭作业,我已经完成了作业的所有目标,我相信我会得到满分 但是,在前面的一个类中,我们对多个操作使用了相同的事件处理程序(在本例中,您可以在文本字段中键入颜色,或者单击按钮更改框的背景颜色) 我不知道在这种情况下我会怎么做。。。我必须在构造函数中选择一个类型吗?如果第一个参数可以是按钮或文本字段,那么我认为这会有所帮助 我只是想弄清楚如何在任何可能的地方涂上干的(不要重复) public class ColorChooserApplication extends Applicati

今天我做了一些家庭作业,我已经完成了作业的所有目标,我相信我会得到满分

但是,在前面的一个类中,我们对多个操作使用了相同的事件处理程序(在本例中,您可以在文本字段中键入颜色,或者单击按钮更改框的背景颜色)

我不知道在这种情况下我会怎么做。。。我必须在构造函数中选择一个类型吗?如果第一个参数可以是按钮或文本字段,那么我认为这会有所帮助

我只是想弄清楚如何在任何可能的地方涂上干的(不要重复)

public class ColorChooserApplication extends Application
{

@Override
public void start(Stage stage)
{
    // Create all UI components
    VBox backgroundBox = new VBox(10);
    backgroundBox.setPadding(new Insets(10));
    HBox topBox = new HBox(10);
    HBox bottomBox = new HBox(10);

    TextField colorPrompt = new TextField();
    colorPrompt.setOnAction(new ColorHandler(colorPrompt, backgroundBox));

    Button redButton = new Button("Red");
    redButton.setOnAction(new ButtonHandler(redButton, backgroundBox));

    Button whiteButton = new Button("White");
    whiteButton.setOnAction(new ButtonHandler(whiteButton, backgroundBox));

    Button blueButton = new Button("Blue");
    blueButton.setOnAction(new ButtonHandler(blueButton, backgroundBox));



    // Assemble
    topBox.getChildren().add(colorPrompt);
    bottomBox.getChildren().addAll(redButton, whiteButton, blueButton);

    backgroundBox.getChildren().addAll(topBox, bottomBox);
    backgroundBox.setAlignment(Pos.CENTER);
    topBox.setAlignment(Pos.CENTER);
    bottomBox.setAlignment(Pos.CENTER);

    // Set scene and show
    stage.setScene(new Scene(backgroundBox));
    stage.show();

}

class ColorHandler implements EventHandler<ActionEvent>
{

    TextField colorTf;
    VBox bgVbox;

    public ColorHandler(TextField colorTf, VBox bgVbox)
    {
        this.colorTf = colorTf;
        this.bgVbox = bgVbox;
    }

    @Override
    public void handle(ActionEvent event)
    {
        String color = colorTf.getText();
        bgVbox.setStyle("-fx-background-color:" + color);
    }

}

class ButtonHandler implements EventHandler<ActionEvent>
{

    Button colorButton;
    VBox bgVbox;

    public ButtonHandler(Button colorButton, VBox bgVbox)
    {
        this.colorButton = colorButton;
        this.bgVbox = bgVbox;
    }

    @Override
    public void handle(ActionEvent event)
    {
        String color = colorButton.getText();
        bgVbox.setStyle("-fx-background-color:" + color);
    }

}

public static void main(String[] args)
{
    launch(args);
}
公共类ColorChooseApplication扩展了应用程序
{
@凌驾
公众假期开始(阶段)
{
//创建所有UI组件
VBox backgroundBox=新的VBox(10);
背景框。设置填充(新插图(10));
HBox机顶盒=新的HBox(10);
HBox bottomBox=新的HBox(10);
TextField colorpromt=新建TextField();
setOnAction(新的ColorHandler(colorPrompt,backgroundBox));
按钮红色按钮=新按钮(“红色”);
设置操作(新按钮句柄(红色按钮,背景框));
按钮白色按钮=新按钮(“白色”);
设置操作(新按钮句柄(白按钮,背景框));
按钮蓝色按钮=新按钮(“蓝色”);
设置动作(新按钮手柄(背景框);
//集合
topBox.getChildren().add(colorPrompt);
bottomBox.getChildren().addAll(红色按钮、白色按钮、蓝色按钮);
backgroundBox.getChildren().addAll(topBox,bottomBox);
背景框。设置对齐(位置中心);
机顶盒设置对齐(位置中心);
底盒。设置对齐(位置中心);
//布景
舞台场景(新场景(背景框));
stage.show();
}
类ColorHandler实现EventHandler
{
TextField-colorTf;
VBox-bgVbox;
公共ColorHandler(文本字段colorTf、VBox bgVbox)
{
this.colorTf=colorTf;
this.bgVbox=bgVbox;
}
@凌驾
公共无效句柄(ActionEvent事件)
{
字符串color=colorTf.getText();
bgVbox.setStyle(“-fx背景色:“+颜色”);
}
}
类ButtonHandler实现EventHandler
{
按钮颜色按钮;
VBox-bgVbox;
公共按钮句柄(按钮颜色按钮,VBox bgVbox)
{
this.colorButton=colorButton;
this.bgVbox=bgVbox;
}
@凌驾
公共无效句柄(ActionEvent事件)
{
字符串color=colorButton.getText();
bgVbox.setStyle(“-fx背景色:“+颜色”);
}
}
公共静态void main(字符串[]args)
{
发射(args);
}

}如果您使用的是Java 8,您可以

class ColorHandler implements EventHandler<ActionEvent> {
    Supplier<String> colorSupplier ;
    VBox bgVbox ;

    public ColorHandler(Supplier<String> colorSupplier, VBox bgVbox) {
        this.colorSupplier = colorSupplier ;
        this.bgVbox = bgVbox ;
    }

    @Override
    public void handle(ActionEvent event) {
        String color = colorSupplier.get();
        bgVbox.setStyle("-fx-background-color: "+color);
    }
}
请注意,为第一个参数提供的只是返回正确字符串以在css中使用的函数。所以你可以做像这样的事情

whiteButton.setOnAction(new ColorHandler(() -> "#ffffff", backgroundBox));
blueButton.setOnAction(new ColorHandler(() -> "cornflowerblue", backgroundBox));
等等

whiteButton.setOnAction(new ColorHandler(() -> "#ffffff", backgroundBox));
blueButton.setOnAction(new ColorHandler(() -> "cornflowerblue", backgroundBox));