在JavaFx实例(二)中,改变窗口的大小,绘制的圆形不会随窗口改变而改变位置,要想使圆形始终在窗口的中心,就需要用到bind()方法。用法是:target.bind(source);下面这个程序将这圆形始终保持在窗口的中心,不论怎样改变窗口大小。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class ShowCircleCentered extends Application {
   @Override // Override the start method in the Application class
   public void start(Stage primaryStage) {
     // Create a pane to hold the circle 
     Pane pane = new Pane();
     // Create a circle and set its properties
     Circle circle = new Circle();
     circle.centerXProperty().bind(pane.widthProperty().divide(2));
     circle.centerYProperty().bind(pane.heightProperty().divide(2));
     circle.setRadius(50);
     circle.setStroke(Color.BLACK); 
     circle.setFill(Color.WHITE);
     pane.getChildren().add(circle);// Add circle to the pane
     // Create a scene and place it in the stage
     Scene scene = new Scene(pane, 200,200);
     primaryStage.setTitle("ShowCircleCentered");// Set the stage title
     primaryStage.setScene(scene); // Place the scene in the stage
     primaryStage.show();// Display the stage
   }
}

运行结果如下:

冯斌:JavaFx实例(四)“ShowCircleCentered”


冯斌:JavaFx实例(四)“ShowCircleCentered”


转载于:https://blog.51cto.com/fengbin8606/1419860

相关文章:

  • 2021-09-15
  • 2022-12-23
  • 2021-09-19
  • 2021-08-14
  • 2021-10-08
  • 2022-01-05
  • 2022-02-04
  • 2021-07-25
猜你喜欢
  • 2021-09-03
  • 2021-04-16
  • 2021-10-05
  • 2021-10-29
  • 2021-12-30
  • 2021-08-23
  • 2021-11-18
相关资源
相似解决方案