javafx编程题。窗口中放置“顺转”和“逆转”两个按钮,当单击按钮时,将椭圆每次旋转30。。
时间: 2023-08-18 17:06:58 浏览: 28
以下是一个可能的JavaFX程序,可以实现你所描述的功能:
```java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Ellipse;
import javafx.stage.Stage;
public class EllipseRotation extends Application {
private static final int ROTATION_ANGLE = 30; // 旋转角度
private Ellipse ellipse;
private int currentAngle = 0; // 当前角度
@Override
public void start(Stage primaryStage) {
// 创建椭圆
ellipse = new Ellipse(100, 100, 50, 30);
// 创建按钮
Button clockwiseButton = new Button("顺转");
clockwiseButton.setOnAction(e -> rotateClockwise());
Button counterclockwiseButton = new Button("逆转");
counterclockwiseButton.setOnAction(e -> rotateCounterclockwise());
// 将椭圆和按钮添加到布局中
StackPane root = new StackPane();
root.getChildren().addAll(ellipse, clockwiseButton, counterclockwiseButton);
// 创建场景并显示窗口
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Ellipse Rotation");
primaryStage.setScene(scene);
primaryStage.show();
}
// 顺时针旋转椭圆
private void rotateClockwise() {
currentAngle += ROTATION_ANGLE;
if (currentAngle >= 360) {
currentAngle -= 360;
}
ellipse.setRotate(currentAngle);
}
// 逆时针旋转椭圆
private void rotateCounterclockwise() {
currentAngle -= ROTATION_ANGLE;
if (currentAngle < 0) {
currentAngle += 360;
}
ellipse.setRotate(currentAngle);
}
public static void main(String[] args) {
launch(args);
}
}
```
这个程序使用`Ellipse`类创建椭圆,使用`Button`类创建两个按钮,并在按钮单击事件中调用`rotateClockwise()`或`rotateCounterclockwise()`方法来旋转椭圆。这个程序还使用一个整数类型的变量`currentAngle`来跟踪当前椭圆的角度。每次旋转时,程序将旋转角度加上或减去30,并在角度超过360度时将其减去360度或小于0度时将其加上360度。最后,程序使用`setRotate()`方法将椭圆旋转到新的角度。