JavaFx图形界面编写简单计算器功能
时间: 2023-08-03 18:08:28 浏览: 112
好的,下面是一个简单的JavaFx图形界面计算器的示例代码:
```java
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Calculator extends Application {
private TextField textField;
private double num1 = 0, num2 = 0;
private String operator = "";
@Override
public void start(Stage primaryStage) {
// 创建布局
GridPane gridPane = new GridPane();
gridPane.setAlignment(Pos.CENTER);
gridPane.setHgap(10);
gridPane.setVgap(10);
gridPane.setPadding(new Insets(25, 25, 25, 25));
// 创建组件
Label label = new Label("请输入两个数并选择操作符:");
gridPane.add(label, 0, 0, 4, 1);
textField = new TextField();
gridPane.add(textField, 0, 1, 4, 1);
Button btn1 = new Button("1");
btn1.setOnAction(e -> textField.setText(textField.getText() + "1"));
gridPane.add(btn1, 0, 2);
Button btn2 = new Button("2");
btn2.setOnAction(e -> textField.setText(textField.getText() + "2"));
gridPane.add(btn2, 1, 2);
Button btn3 = new Button("3");
btn3.setOnAction(e -> textField.setText(textField.getText() + "3"));
gridPane.add(btn3, 2, 2);
Button btnPlus = new Button("+");
btnPlus.setOnAction(e -> {
num1 = Double.parseDouble(textField.getText());
operator = "+";
textField.setText("");
});
gridPane.add(btnPlus, 3, 2);
Button btn4 = new Button("4");
btn4.setOnAction(e -> textField.setText(textField.getText() + "4"));
gridPane.add(btn4, 0, 3);
Button btn5 = new Button("5");
btn5.setOnAction(e -> textField.setText(textField.getText() + "5"));
gridPane.add(btn5, 1, 3);
Button btn6 = new Button("6");
btn6.setOnAction(e -> textField.setText(textField.getText() + "6"));
gridPane.add(btn6, 2, 3);
Button btnMinus = new Button("-");
btnMinus.setOnAction(e -> {
num1 = Double.parseDouble(textField.getText());
operator = "-";
textField.setText("");
});
gridPane.add(btnMinus, 3, 3);
Button btn7 = new Button("7");
btn7.setOnAction(e -> textField.setText(textField.getText() + "7"));
gridPane.add(btn7, 0, 4);
Button btn8 = new Button("8");
btn8.setOnAction(e -> textField.setText(textField.getText() + "8"));
gridPane.add(btn8, 1, 4);
Button btn9 = new Button("9");
btn9.setOnAction(e -> textField.setText(textField.getText() + "9"));
gridPane.add(btn9, 2, 4);
Button btnMultiply = new Button("*");
btnMultiply.setOnAction(e -> {
num1 = Double.parseDouble(textField.getText());
operator = "*";
textField.setText("");
});
gridPane.add(btnMultiply, 3, 4);
Button btn0 = new Button("0");
btn0.setOnAction(e -> textField.setText(textField.getText() + "0"));
gridPane.add(btn0, 0, 5);
Button btnClear = new Button("C");
btnClear.setOnAction(e -> textField.setText(""));
gridPane.add(btnClear, 1, 5);
Button btnEquals = new Button("=");
btnEquals.setOnAction(e -> {
num2 = Double.parseDouble(textField.getText());
if (operator.equals("+")) {
textField.setText(Double.toString(num1 + num2));
} else if (operator.equals("-")) {
textField.setText(Double.toString(num1 - num2));
} else if (operator.equals("*")) {
textField.setText(Double.toString(num1 * num2));
} else if (operator.equals("/")) {
textField.setText(Double.toString(num1 / num2));
}
});
gridPane.add(btnEquals, 2, 5);
Button btnDivide = new Button("/");
btnDivide.setOnAction(e -> {
num1 = Double.parseDouble(textField.getText());
operator = "/";
textField.setText("");
});
gridPane.add(btnDivide, 3, 5);
// 创建场景
Scene scene = new Scene(gridPane, 300, 250);
// 设置窗口标题并显示场景
primaryStage.setTitle("Calculator");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
```
这个计算器支持加、减、乘、除四种基本运算,还有清空和等于两个功能键。你可以根据自己的需求进行修改和扩展。
阅读全文