javafx窗口中放置“顺转”和“逆转”两个按钮,当单击按钮时,将椭圆每次旋转30度
时间: 2023-08-28 22:05:06 浏览: 100
好的,你需要创建一个JavaFX窗口并在窗口中放置两个按钮,然后将椭圆添加到窗口中并使其旋转。以下是一个示例代码,你可以参考一下:
```java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
import javafx.stage.Stage;
public class EllipseRotation extends Application {
private double rotation = 0;
@Override
public void start(Stage primaryStage) {
// 创建椭圆
Ellipse ellipse = new Ellipse(100, 60, 80, 40);
ellipse.setFill(Color.LIGHTBLUE);
// 创建“顺转”按钮
Button clockwise = new Button("顺转");
clockwise.setOnAction(event -> {
rotation += 30;
ellipse.setRotate(rotation);
});
// 创建“逆转”按钮
Button counterclockwise = new Button("逆转");
counterclockwise.setOnAction(event -> {
rotation -= 30;
ellipse.setRotate(rotation);
});
// 创建栈面板并添加组件
StackPane root = new StackPane();
root.getChildren().addAll(ellipse, clockwise, counterclockwise);
// 创建场景并添加根节点
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Ellipse Rotation");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
```
运行上述代码,你就可以在窗口中看到一个椭圆和两个按钮。每当你单击“顺转”按钮时,椭圆就会顺时针旋转30度;每当你单击“逆转”按钮时,椭圆就会逆时针旋转30度。
阅读全文
相关推荐












