我有这段代码,应该通过更改圆的半径来增加或减小圆的大小。我的问题是,由于某种原因,半径在更新时圆没有更新。半径已更新,但“圆”没有增长或缩小。谁能帮我吗?下面是我的代码。
我尝试了很多事情,但是最终变得越来越复杂,没有成功。
public class Circle_GUI extends Application {
@Override
public void start(Stage stage) throws Exception {
BorderPane root = new BorderPane();
CirclePane cp = new CirclePane();
root.setCenter(new CirclePane());
HBox hbox = new HBox();
hbox.setPadding(new Insets(10, 20, 10, 20));
hbox.setAlignment(Pos.CENTER);
hbox.setSpacing(10);
Button btnEnlarge = new Button("Enlarge");
btnEnlarge.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
cp.enlargeShrinkCircle(true);
System.out.println("You have enlarged");
}
});
Button btnShrink = new Button("Shrink");
btnShrink.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
cp.enlargeShrinkCircle(false);
System.out.println("You have shrunk");
}
});
hbox.getChildren().addAll(btnEnlarge, btnShrink);
root.setBottom(hbox);
Scene scene = new Scene(root, 400, 300);
stage.setScene(scene);
stage.setTitle("JFK=Event=Demo");
stage.show();
}
class CirclePane extends StackPane {
Circle circle = null;
CirclePane() {
circle = new Circle();
circle.setRadius(100);
circle.setStroke(Color.BLUE);
circle.setFill(Color.AQUA);
this.getChildren().add(circle);
}
void enlargeShrinkCircle(boolean enlarge) {
if (enlarge) {
circle.setRadius(circle.getRadius() + 10);
} else {
double r = circle.getRadius();
r -= 10;
if (r < 1.0) {
r = 1.0;
}
circle.setRadius(r);
}
}
}
我希望在按下“放大”按钮时增加GUI上的“圆圈”大小,而在按下“缩小”按钮时减小“圆圈”大小。