【问题标题】:Using one event handler for multiple actions对多个操作使用一个事件处理程序
【发布时间】:2014-09-28 22:34:30
【问题描述】:

我今天做了一些功课,我已经完成了作业的所有目标,我相信这会让我得到满分。

然而,在较早的课程中,我们为多个操作使用了相同的事件处理程序(在此示例中,您可以在文本字段中键入颜色,或者单击按钮来更改框的背景颜色)。

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

我只是想弄清楚如何尽可能地应用 DRY(不要重复自己)。

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

}

【问题讨论】:

    标签: events event-handling javafx


    【解决方案1】:

    如果你使用的是 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);
        }
    }
    

    然后

    colorPrompt.setOnAction(new ColorHandler(colorPrompt::getText, backgroundBox));
    redButton.setOnAction(new ColorHandler(redButton::getText, backgroundBox));
    

    请注意,您需要为第一个参数提供一些函数,该函数返回正确的字符串以供在 css 中使用。所以你可以做类似的事情

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

    等等

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-04
      • 1970-01-01
      • 1970-01-01
      • 2012-10-31
      • 2015-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多