JavaFX如何使用Button更改Text节点?

时间:2018-03-09 20:59:50

标签: java javafx netbeans javafx-8

我正在构建一个计算器。到目前为止我已经创建了几个类。 IntegerButton应该在按下时在监视器上显示它的值。

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事件)时,我无法引用Monitor。另外,我完全迷失了EventHandlers的概念。我的监听器应该听事吗?我的Button应该在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 个答案:

答案 0 :(得分:0)

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

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)您的事件处理程序应该在您可以访问按钮和标签的位置定义。