【发布时间】:2018-08-18 12:22:09
【问题描述】:
我正在构建一个计算器。到目前为止,我已经创建了一些类。有一个 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 事件)时,我无法引用监视器。此外,我完全迷失了 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?
}
}
基本上我对如何通过事件连接这些节点有一个基本的误解。怎么做?
【问题讨论】:
-
你为什么使用显示器?
-
向用户显示他们正在输入的内容。
-
您需要将
Monitor实例引用到IntegerButtons。您可能应该将它传递给构造函数。但是,我并没有真正看到为程序中使用的每个节点创建新类的目的,尤其是因为它们甚至每个节点都不包含一个方法...... -
感谢费边。我也看不到目的。我真的只是在这里摇摆,希望我能开发出一些有组织的东西。我不想让我的任何课程太胖。但是,我没有任何基础。我几乎不知道编写良好的 javafx 应用程序是什么样的。这是我的第一次。我的计算器将有 30 个按钮:10 个整数、一个符号开关、打开/关闭括号、小数点、运算符、变量、清除和清除方程、Mod、Lsh、Rsh,......它继续。我想我不会在 main 方法中定义每一个。
-
@BenSoutendijk 看看一些在线教程和东西。一本好书是《干净的代码:敏捷软件工艺手册》
标签: java javafx netbeans javafx-8