繁体   English   中英

如何在同一按钮JavaFX上多个setOnAction

[英]How to Multiple setOnAction on the same button JavaFX

我正在尝试使按钮jugar验证并从文本框和3个下拉框返回值,当您键入用户名和密码时,必须有一种方法可以验证多个值,例如,登录按钮。

这是代码

        package application;

        import javafx.scene.control.Button;
        import javafx.scene.control.ChoiceBox;
        import javafx.scene.control.TextField;
        import javafx.application.Application;
        import javafx.event.ActionEvent;
        import javafx.event.EventHandler;
        import javafx.geometry.Insets;
        import javafx.stage.Stage;
        import javafx.scene.Scene;
        import javafx.scene.layout.BorderPane;
        import javafx.scene.layout.GridPane;
        import javafx.scene.layout.StackPane;


        public class Main extends Application implements EventHandler<ActionEvent>{

        // crea los botones 
        Button historial;   
        Button jugar;

public static void main(String[] args) {
    launch(args);
}

@Override
//nombre de la ventana principal
public void start(Stage primaryStage) throws Exception {
    primaryStage.setTitle("Football Simulator 2016");

这里的下拉列表

    //crea los drop list
    ChoiceBox<String> jugadasArbitro = new ChoiceBox<>();
    jugadasArbitro.getItems().addAll("Saque de banda","Tiro de esquina","Falta","Falta(Tarjeta amarilla)", "Falta (Tarjeta roja)", "Mano","Posicion adelantada","Penal","Fuera de juego", "Gol");
    ChoiceBox<String> jugadasOfensivas = new ChoiceBox<>();
    jugadasOfensivas.getItems().addAll("Pase","Pase largo","Tiro a puerta");
    ChoiceBox<String> jugadasDefensivas = new ChoiceBox<>();
    jugadasDefensivas.getItems().addAll("Intercepcion");

在这里,我有setOnAction侦听器

    //inicializa botones
    TextField tiempo = new TextField();
    historial = new Button("Historial");
    historial.setOnAction(this);
    jugar= new Button("Jugar");
    jugar.setOnAction(e -> isInt(tiempo,tiempo.getText()));
    jugar.setOnAction(e -> getArbitro(jugadasArbitro));
    jugar.setOnAction(e -> getOfensiva(jugadasOfensivas));
    jugar.setOnAction(e -> getDefensa(jugadasDefensivas));

    //forma del Gui y posiciona los botones
    GridPane grid = new GridPane();
    grid.setPadding(new Insets(10,10,10,10));
    grid.setVgap(8);
    grid.setHgap(10);
    GridPane.setConstraints(historial, 1, 3);
    GridPane.setConstraints(jugar, 2, 2);
    GridPane.setConstraints(jugadasArbitro, 3, 3);
    GridPane.setConstraints(jugadasOfensivas, 3, 4);
    GridPane.setConstraints(jugadasDefensivas, 3, 5);
    grid.getChildren().addAll(historial, jugadasArbitro,jugadasDefensivas,jugadasOfensivas,jugar,tiempo);

    //tamanio de la ventana
    Scene scene = new Scene(grid, 540,300);
    primaryStage.setScene(scene);
    primaryStage.show();
}

读取保管箱值的方法

//leen el valor de los drop box
private void getDefensa(ChoiceBox<String> jugadasDefensivas) {
    // TODO Auto-generated method stub
    String opcionDefensa = jugadasDefensivas.getValue();
    System.out.println(opcionDefensa);
}

private void getOfensiva(ChoiceBox<String> jugadasOfensivas) {
    // TODO Auto-generated method stub
    String opcionOfensiva = jugadasOfensivas.getValue();
    System.out.println(opcionOfensiva);
}

private void getArbitro(ChoiceBox<String> jugadasArbitro) {
    // TODO Auto-generated method stub
    String opcionArbitro = jugadasArbitro.getValue();
    System.out.println(opcionArbitro);
}

//valida que el tiempo solo sea INT
private boolean isInt(TextField tiempo, String message) {
    // TODO Auto-generated method stub
    try{
        int minutos = Integer.parseInt(tiempo.getText());
        return true;
    }catch (NumberFormatException e){}
    System.out.println("Error: introduzca numeros solamente");
    return false;

}

@Override
public void handle(ActionEvent event) {


}

}

好吧,这很容易,只需设置即可。OnAction(e-> {(whatever)}); 例:

jugar.setOnAction(e ->{ isInt(tiempo,tiempo.getText());
    getArbitro(jugadasArbitro);
    getOfensiva(jugadasOfensivas);
    getDefensa(jugadasDefensivas);
    });

您难道不能将所有功能都放在一个OnActionHandler吗? 或者,如果只希望处理其中一个,则可以使用变量确定要测试的变量(将通过其他事件和交互进行更改)。

jugar.setOnAction(e ->{
    isInt(tiempo,tiempo.getText());
    getArbitro(jugadasArbitro);
    getOfensiva(jugadasOfensivas);
    getDefensa(jugadasDefensivas);
});

要么

jugar.setOnAction(e -> {
    if(method == 1)
    {
        isInt(tiempo,tiempo.getText());
    }
    else if(method == 2)
    {
        getArbitro(jugadasArbitro);
    }
    else if(method == 3)
    {
        getOfensiva(jugadasOfensivas);
    }
    else if(method == 4)
    {
        getDefensa(jugadasDefensivas);
    }
});

取决于您的需要。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM