带界面的java 科学计算器代码
时间: 2023-07-09 13:43:48 浏览: 72
Java 计算器代码
以下是一个带界面的 Java 科学计算器代码,使用了 JavaFX 框架:
```
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.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import java.lang.Math;
public class Calculator extends Application {
private TextField num1Input;
private TextField num2Input;
private Label resultLabel;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Scientific Calculator");
// Create input fields for numbers
num1Input = new TextField();
num2Input = new TextField();
// Create buttons for arithmetic operations
Button addBtn = new Button("+");
addBtn.setOnAction(e -> add());
Button subtractBtn = new Button("-");
subtractBtn.setOnAction(e -> subtract());
Button multiplyBtn = new Button("*");
multiplyBtn.setOnAction(e -> multiply());
Button divideBtn = new Button("/");
divideBtn.setOnAction(e -> divide());
// Create buttons for trigonometric functions
Button sinBtn = new Button("sin");
sinBtn.setOnAction(e -> sin());
Button cosBtn = new Button("cos");
cosBtn.setOnAction(e -> cos());
Button tanBtn = new Button("tan");
tanBtn.setOnAction(e -> tan());
// Create button to clear input fields
Button clearBtn = new Button("Clear");
clearBtn.setOnAction(e -> clear());
// Create label to display result
resultLabel = new Label();
resultLabel.setFont(new Font("Arial", 20));
// Create grid pane to hold input fields and arithmetic operation buttons
GridPane gridPane = new GridPane();
gridPane.setAlignment(Pos.CENTER);
gridPane.setHgap(10);
gridPane.setVgap(10);
gridPane.add(new Label("Number 1:"), 0, 0);
gridPane.add(num1Input, 1, 0);
gridPane.add(new Label("Number 2:"), 0, 1);
gridPane.add(num2Input, 1, 1);
gridPane.add(addBtn, 0, 2);
gridPane.add(subtractBtn, 1, 2);
gridPane.add(multiplyBtn, 0, 3);
gridPane.add(divideBtn, 1, 3);
// Create HBox to hold trigonometric function buttons
HBox trigBox = new HBox(10);
trigBox.setAlignment(Pos.CENTER);
trigBox.getChildren().addAll(sinBtn, cosBtn, tanBtn);
// Create VBox to hold all UI elements
VBox vbox = new VBox(10);
vbox.setAlignment(Pos.CENTER);
vbox.setPadding(new Insets(10));
vbox.getChildren().addAll(gridPane, trigBox, resultLabel, clearBtn);
// Create scene and show stage
Scene scene = new Scene(vbox, 400, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
private void add() {
double num1 = Double.parseDouble(num1Input.getText());
double num2 = Double.parseDouble(num2Input.getText());
double result = num1 + num2;
resultLabel.setText("Result: " + result);
}
private void subtract() {
double num1 = Double.parseDouble(num1Input.getText());
double num2 = Double.parseDouble(num2Input.getText());
double result = num1 - num2;
resultLabel.setText("Result: " + result);
}
private void multiply() {
double num1 = Double.parseDouble(num1Input.getText());
double num2 = Double.parseDouble(num2Input.getText());
double result = num1 * num2;
resultLabel.setText("Result: " + result);
}
private void divide() {
double num1 = Double.parseDouble(num1Input.getText());
double num2 = Double.parseDouble(num2Input.getText());
double result = num1 / num2;
resultLabel.setText("Result: " + result);
}
private void sin() {
double num1 = Double.parseDouble(num1Input.getText());
double result = Math.sin(num1);
resultLabel.setText("Result: " + result);
}
private void cos() {
double num1 = Double.parseDouble(num1Input.getText());
double result = Math.cos(num1);
resultLabel.setText("Result: " + result);
}
private void tan() {
double num1 = Double.parseDouble(num1Input.getText());
double result = Math.tan(num1);
resultLabel.setText("Result: " + result);
}
private void clear() {
num1Input.clear();
num2Input.clear();
resultLabel.setText("");
}
}
```
该代码使用了 JavaFX 框架创建了一个简单的用户界面,包括输入数字的文本框、算术运算和三角函数的按钮、显示结果的标签以及清空输入的按钮。通过调用相应的方法来处理用户的输入和按钮点击事件,并将结果显示在标签中。
阅读全文