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 ShowCircle extends Application {
   @Override // Override the start method in the Application class
   public void start(Stage primaryStage) {
     // Create a circle and set its properties
     Circle circle = new Circle();
     circle.setCenterX(100);
     circle.setCenterY(100);
     circle.setRadius(50);
     circle.setStroke(Color.BLACK);
     circle.setFill(Color.WHITE);
     // Create a pane to hold the circle 
     Pane pane = new Pane();
     pane.getChildren().add(circle);
     // Create a scene and place it in the stage
     Scene scene = new Scene(pane, 200,200);
     primaryStage.setTitle("ShowCircle");// Set the stage title
     primaryStage.setScene(scene); // Place the scene in the stage
     primaryStage.show();// Display the stage
   }
}

说明:

1、上面的代码演示绘制一个圆形,执行结果如下图:

冯斌:JavaFx实例(二)“ShowCircle”_冯斌 JavaFx

2、当用鼠标改变窗口的大小时,园的位置不会改变


3、javaFx各种容器的关系如下图

冯斌:JavaFx实例(二)“ShowCircle”_绘制圆形_02

4、getChildren()方法返回的是javafx.collections.ObservableList.ObservableList的一个实例。


5、本实例省略了main方法public static void main(String[] args) { Application.launch(args); }

   用命令行的执行上述代码可以不需要main方法。