2014-09-28 89 views
0

我今天正在做一些功课,并完成了作业的所有目标,我相信这会让我满意。然而,在较早的类中,我们使用相同的事件处理程序进行多个操作(在本例中,您要么在文本字段中键入颜色,要么单击按钮以更改框的背景颜色) 。使用一个事件处理程序进行多个操作

我无法弄清楚在这种情况下我该怎么做......我必须在构造函数中选择一个类型吗?如果第一个参数可能是按钮或文本框,那么我认为这会有所帮助。

我只是想弄清楚如何应用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); 
} 

}

回答

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

相关问题