javafx窗口中放置“顺转”和“逆转”两个按钮,当单击按钮时,将椭圆每次旋转30。其中椭圆居中 两个按钮位于底部居中对齐
时间: 2023-09-18 20:06:37 浏览: 36
以下是一个简单的 JavaFX 程序,实现了你所描述的功能:
```java
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.shape.Ellipse;
import javafx.stage.Stage;
public class EllipseRotation extends Application {
private final int ELLIPSE_RADIUS_X = 100;
private final int ELLIPSE_RADIUS_Y = 50;
private final int ROTATION_ANGLE = 30;
@Override
public void start(Stage primaryStage) throws Exception {
// 创建椭圆
Ellipse ellipse = new Ellipse(ELLIPSE_RADIUS_X, ELLIPSE_RADIUS_Y);
// 创建按钮
Button clockwiseButton = new Button("顺转");
clockwiseButton.setOnAction(event -> {
ellipse.setRotate(ellipse.getRotate() + ROTATION_ANGLE);
});
Button counterclockwiseButton = new Button("逆转");
counterclockwiseButton.setOnAction(event -> {
ellipse.setRotate(ellipse.getRotate() - ROTATION_ANGLE);
});
// 创建按钮容器
HBox buttonBox = new HBox(clockwiseButton, counterclockwiseButton);
buttonBox.setAlignment(Pos.CENTER);
// 创建布局
BorderPane pane = new BorderPane();
pane.setCenter(ellipse);
pane.setBottom(buttonBox);
// 创建场景和窗口
Scene scene = new Scene(pane, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
```
程序中创建了一个椭圆和两个按钮,将它们放置在一个 `BorderPane` 布局中,椭圆放在中间,按钮放在底部居中。当点击按钮时,通过设置椭圆的 `rotate` 属性来实现旋转。每次旋转的角度为 `ROTATION_ANGLE`,在本程序中为 30 度。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)