代码之家  ›  专栏  ›  技术社区  ›  devaent

JavaFX如何使用按钮更改文本节点?

  •  0
  • devaent  · 技术社区  · 7 年前

    我正在制作一个计算器。到目前为止,我已经创建了几个类。有一个整数按钮,按下该按钮时,应在监视器上显示其值。

    package calculator.buttons;
    
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.control.Button;
    
    public class IntegerButton extends Button{
        int value;
    
        public IntegerButton(int value) {
            this.value = value;
            this.setText(Integer.toString(value));
            this.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
    //                Handle the event as an integer
    //                send event to monitor?
                }
            });
        }
    
    }
    

    然而,我不明白如何做到这一点。

    在每个按钮的构造函数中定义句柄(ActionEvent事件)时,我无法引用监视器。此外,我完全不懂EventHandler的概念。我的监视器应该监听事件吗?我的按钮是否应该在start(Stage primaryStage)方法中调用其setOnAction?我完全不知所措。

    下面是我的应用程序的启动方法:包计算器;

    import javafx.application.Application;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.layout.ColumnConstraints;
    import javafx.scene.layout.GridPane;
    import javafx.scene.layout.RowConstraints;
    import javafx.stage.Stage;
    
    /**
     *
     * @author souten
     */
    public class Calculator extends Application {
    
        public static final double MIN_HEIGHT = 525;
        public static final double MIN_WIDTH = 350;
        public static final double DEFAULT_GAP = 6;
    
        @Override
        public void start(Stage primaryStage) {
    //        Window settings
            primaryStage.setTitle("Calculator");
            primaryStage.setMinHeight(MIN_HEIGHT);
            primaryStage.setMinWidth(MIN_WIDTH);
    
    //        container is the base content node
    //        - it contains 1 column and 3 rows
            GridPane container = new GridPane();
            container.setAlignment(Pos.CENTER);
            ColumnConstraints column1 = new ColumnConstraints();
            column1.setPercentWidth(100);
            container.getColumnConstraints().add(column1);
            RowConstraints[] rows = new RowConstraints[3];
            for (int i = 0; i < rows.length; i++)
                rows[i] = new RowConstraints();
            rows[0].setPercentHeight(40);
            rows[1].setPercentHeight(10);
            rows[2].setPercentHeight(50);
            container.getRowConstraints().addAll(rows);
            container.setHgap(0);
            container.setVgap(DEFAULT_GAP);
    
    //        row 0: the monitor, outputs the calculations in hex, dec, oct, and bin
    
            Monitor m = new Monitor();
            container.add(m, 0, 0);
    
    //        row 1: contains misc buttons i.e. bit-toggler, bit measurement, and memory buttons
    
    //        row 2: the bottom row, keypadContainer contains all the buttons that create the calculation/function
    
            Keypad keypad = new Keypad();
            container.add(keypad, 0, 2);
    
    //        Scene initialization
            Scene scene = new Scene(container, MIN_WIDTH, MIN_HEIGHT);
            primaryStage.setScene(scene);
            scene.getStylesheets().add
              (Calculator.class.getResource("Calculator.css").toExternalForm());
            primaryStage.show();
        }
    
        /** 
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            launch(args);
        }
    
    }
    

    键盘类别:

    package calculator;
    
    import calculator.buttons.IntegerButton;
    import javafx.scene.control.Button;
    import javafx.scene.layout.GridPane;
    
    public class Keypad extends GridPane{
        String[][] labels = {
            {"Lsh", "Rsh", "Or", "Xor", "Not", "And"},
               {"↑", "Mod", "CE", "C", "⌫", "÷"},
                 {"A", "B", "7", "8", "9", "×"},     
                 {"C", "D", "4", "5", "6", "−"},
                 {"E", "F", "1", "2", "3", "+"}, 
                 {"(", ")", "±", "0", ".", "="}
        };
    
        Button[][] buttons = new Button[6][6];
    
        public Keypad(){
            //        initialize buttons
            buttons[5][3] = new IntegerButton(0);
            buttons[4][2] = new IntegerButton(1);
            buttons[4][3] = new IntegerButton(2);
            buttons[4][4] = new IntegerButton(3);
            buttons[3][2] = new IntegerButton(4);
            buttons[3][3] = new IntegerButton(5);
            buttons[3][4] = new IntegerButton(6);
            buttons[2][2] = new IntegerButton(7);
            buttons[2][3] = new IntegerButton(8);
            buttons[2][4] = new IntegerButton(9);
            for (int i = 0; i < 6; i++){
                for (int j = 0; j < 6; j++) {
                if(buttons[i][j] != null)
                    this.add(buttons[i][j], j, i);
                }
            }
    
    
        }
    
    }
    

    和我的监视器类:

    package calculator;
    
    import javafx.scene.text.Text;
    
    public class Monitor extends Text {
    
    
        public Monitor() {
            this.setText("Hello World!");
    //      this.addListener?
        }
    }
    

    基本上,我对如何通过事件连接这些节点有一个基本的误解。怎么做?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Steve    7 年前

    下面是一个非常基本的示例,它说明了按钮事件在您尝试使用它的上下文中是如何工作的。

    public class SimpleButtonApp extends Application {
    
    @Override
    public void start(Stage stage) throws Exception {
        GridPane container = new GridPane();
    
        Label displayLabel = new Label();
    
        String[][] labels = {
                {"Lsh", "Rsh", "Or", "Xor", "Not", "And"},
                   {"↑", "Mod", "CE", "C", "⌫", "÷"},
                     {"A", "B", "7", "8", "9", "×"},     
                     {"C", "D", "4", "5", "6", "−"},
                     {"E", "F", "1", "2", "3", "+"}, 
                     {"(", ")", "±", "0", ".", "="}};
    
        for(int i = 0; i < labels.length; i++) {
            for(int j = 0; j < labels[i].length; j++) {
                String label = labels[i][j];
    
                Button button = new Button(label);
                button.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent event) {
                        displayLabel.setText(displayLabel.getText() + label);
                    }
                });
                container.add(button, i, j);
            }
        }
    
        container.getChildren().addAll(button, displayLabel);
        Scene scene = new Scene(container, 300, 100);
        stage.setScene(scene);
        stage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
    }
    }
    

    制作计算器的一些设计提示:

    1) 基本的JavaFX标签应该可以用来显示数字

    2) 您不需要IntegerButton类。按钮就是一个按钮。

    3) 事件处理程序应该在您可以访问按钮和标签的地方定义。