JavaFX 几何图形计算器的代码
时间: 2024-06-23 09:00:52 浏览: 169
JavaFX 是 Java 平台的一个图形用户界面 (GUI) 库,它允许开发者创建丰富的桌面应用程序。设计一个几何图形计算器,我们可以使用 JavaFX 中的 Shape 类和一些基本的事件处理来实现。以下是一个简单的示例,展示如何创建一个窗口,用户可以选择不同形状并输入边长,然后计算图形的面积:
```java
import javafx.application.Application;
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.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.scene.shape.Square;
import javafx.stage.Stage;
public class GeometryCalculator extends Application {
private TextField radiusField, lengthField, widthField;
private Shape shape;
private double area;
@Override
public void start(Stage primaryStage) {
// 创建窗口
primaryStage.setTitle("Geometry Calculator");
// 创建布局
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
Label shapeLabel = new Label("Shape:");
Button circleButton = new Button("Circle");
Button squareButton = new Button("Square");
Button rectangleButton = new Button("Rectangle");
// 设置按钮事件
circleButton.setOnAction(e -> shape = new Circle(Double.parseDouble(radiusField.getText())));
squareButton.setOnAction(e -> shape = new Square(Double.parseDouble(lengthField.getText())));
rectangleButton.setOnAction(e -> shape = new Rectangle(Double.parseDouble(lengthField.getText()), Double.parseDouble(widthField.getText())));
// 添加字段和按钮
radiusField = new TextField();
lengthField = new TextField();
widthField = new TextField();
grid.add(shapeLabel, 0, 0);
grid.add(circleButton, 1, 0);
grid.add(squareButton, 2, 0);
grid.add(rectangleButton, 3, 0);
grid.add(new Label("Radius:"), 1, 1);
grid.add(radiusField, 2, 1);
grid.add(new Label("Length:"), 1, 2);
grid.add(lengthField, 2, 2);
grid.add(new Label("Width:"), 3, 2);
grid.add(widthField, 4, 2);
// 计算区域和显示结果
Button calculateButton = new Button("Calculate");
calculateButton.setOnAction(e -> {
if (shape instanceof Circle) {
area = Math.PI * Math.pow(Double.parseDouble(radiusField.getText()), 2);
} else if (shape instanceof Square || shape instanceof Rectangle) {
area = shape.getBoundsInLocal().getWidth() * shape.getBoundsInLocal().getHeight();
}
displayArea();
});
grid.add(calculateButton, 5, 2);
// 清除结果
Button clearButton = new Button("Clear");
clearButton.setOnAction(e -> {
shape = null;
area = 0;
radiusField.clear();
lengthField.clear();
widthField.clear();
displayArea();
});
grid.add(clearButton, 6, 2);
Scene scene = new Scene(grid, 400, 300);
scene.getStylesheets().add(getClass().getResource("styles.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
private void displayArea() {
if (shape != null) {
areaLabel.setText("Area: " + area);
} else {
areaLabel.setText("Shape not selected.");
}
}
public static void main(String[] args) {
launch(args);
}
private Label areaLabel = new Label("Area: ");
}
```
这个示例中,我们创建了一个简单的窗口,有选择形状的按钮(圆形、正方形和矩形),以及输入边长的文本框。用户点击“计算”按钮后,根据选择的形状计算并显示面积。同时,我们还有“清除”按钮用于重置输入和结果。
阅读全文