JavaFX的多个按键相同的处理 [英] javafx multiple buttons to same handler

查看:254
本文介绍了JavaFX的多个按键相同的处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个简单的计算器与20按钮和一个处理器。在java中,我可以'如果'语句中使用与event.getSource()中的actionPerformed检查哪个按钮是pressed,但它不处理程序JavaFX的工作。是否有可能在JavaFX的所有按键都有一个处理程序? (我不希望使用的Java 8 Lambda表达式。)
我最后一次尝试与SETID /的getId但它同样无法正常工作(对我来说)。

I try to make a simple calculator with 20 buttons and one handler. In java I can use 'if' statement with event.getSource() in ActionPerformed to check which button is pressed, but it doesn't work with handler in javafx. Is it possible in javafx that all buttons has one handler? (I don't want to use java 8 Lambdas.) Last time I tried with setId/getId but it same not work (to me).

public class Calculator extends Application {

    public Button b0, b1;

    @Override
    public void start(Stage primaryStage) {

        GridPane grid = new GridPane();

        b0 = new Button("0");
        b0.setId("0");
        b0.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
        grid.add(b0, 0, 1);
        b0.setOnAction(myHandler);

        b1 = new Button("1");
        b1.setId("1");
        b1.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
        grid.add(b1, 0, 0);
        b1.setOnAction(myHandler);

        Scene scene = new Scene(grid, 365, 300);
        scene.getStylesheets().add
            (Calculator.class.getResource("calculator.css").toExternalForm());

        primaryStage.setScene(scene);
        primaryStage.setResizable(false);
        primaryStage.show();
    }

    final EventHandler<ActionEvent> myHandler = new EventHandler<ActionEvent>(){

        @Override
        public void handle(final ActionEvent event) {
            Button x = (Button) event.getSource();
            if (x.getId().equals(b0.getId()))
                System.out.println("0");
            else if(x.getId().equals(b1.getId()))
                System.out.println("1");
        }
    };
    public static void main(String[] args) {
        launch(args);
    }

}

推荐答案

我测试了code,它似乎工作得很好。

I tested your code and it seems to work just fine.

有没有真正的理由来测试这些按钮的ID,虽然。如果你的真正的要使用相同的处理(我不建议),只是测试每个按钮和事件的源之间的平等:

There's no real reason to test the ids of the buttons, though. If you really want to use the same handler (which I don't advise), just test for equality between each button and the source of the event:

final EventHandler<ActionEvent> myHandler = new EventHandler<ActionEvent>(){

    @Override
    public void handle(final ActionEvent event) {

        if (event.getSource() == b0)
            System.out.println("0");
        else if(event.getSource() == b1)
            System.out.println("1");
    }
};

但它(几乎?)总是更好地使用为每个操作不同的处理程序。它使code不含所有的的if / else 结构,这都使得它更清洁,更在性能方面。在这里,因为你的按钮做几乎同样的事情,你可以使用一个实现,但多个对象。

But it's (almost?) always better to use a different handler for each action. It keeps the code free of all the if/else constructs, which both makes it cleaner and better in terms of performance. Here, since your buttons do almost the same thing, you can use a single implementation but multiple objects.

下面是一个完整的例子:

Here's a complete example:

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;

public class Calculator extends Application {

    private final IntegerProperty value = new SimpleIntegerProperty();


    class NumberButtonHandler implements EventHandler<ActionEvent> {
        private final int number ;
        NumberButtonHandler(int number) {
            this.number = number ;
        }
        @Override
        public void handle(ActionEvent event) {
            value.set(value.get() * 10 + number);
        }

    }

    @Override
    public void start(Stage primaryStage) {

        GridPane grid = createGrid();

        for (int n = 1; n<10; n++) {
            Button button = createNumberButton(n);
            int row = (n-1) / 3;
            int col = (n-1) % 3 ;
            grid.add(button, col, 2 - row);
        }

        Button zeroButton = createNumberButton(0);
        grid.add(zeroButton, 1, 3);

        Button clearButton = createButton("C");

        // without lambdas:

//        clearButton.setOnAction(
//            new EventHandler<ActionEvent>() {
//                @Override
//                public void handle(ActionEvent event) {
//                    value.set(0);
//                }
//            }
//        );

        // with lambdas:
        clearButton.setOnAction(event -> value.set(0));

        grid.add(clearButton, 2, 3);

        TextField displayField = createDisplayField();

        BorderPane root = new BorderPane();
        root.setPadding(new Insets(10));
        root.setTop(displayField);
        root.setCenter(grid);

        Scene scene = new Scene(root, 365, 300);

        primaryStage.setScene(scene);
        primaryStage.setResizable(false);
        primaryStage.show();
    }

     private Button createNumberButton(int number) {
        Button button = createButton(Integer.toString(number));
        button.setOnAction(new NumberButtonHandler(number));
        return button ;
    }

    private Button createButton(String text) {
        Button button = new Button(text);
        button.setMaxSize(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
        GridPane.setFillHeight(button, true);
        GridPane.setFillWidth(button, true);
        GridPane.setHgrow(button, Priority.ALWAYS);
        GridPane.setVgrow(button, Priority.ALWAYS);
        return button ;
    }


    private GridPane createGrid() {
        GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        grid.setHgap(5);
        grid.setVgap(5);
        grid.setPadding(new Insets(10));
        return grid;
    }

    private TextField createDisplayField() {
        TextField displayField = new TextField();
        displayField.textProperty().bind(Bindings.format("%d", value));
        displayField.setEditable(false);
        displayField.setAlignment(Pos.CENTER_RIGHT);
        return displayField;
    }

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

这篇关于JavaFX的多个按键相同的处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆