运行程序时{JavaFX Circle()形状未显示

时间:2019-06-27 03:41:40

标签: inheritance javafx shapes

我创建了一个类xxxCircle,该类具有方法draw()。本质上,应该使用JavaFX的预定义类Circlejavafx.scene.shape.Circle)来画一个圆。当我运行该程序时,没有任何错误,但是圆圈没有显示。我该怎么做才能解决此问题?

xxxCircle.java

package sample;

import javafx.scene.shape.Circle;

public class xxxCircle extends xxxShape {

    double radius;
    double xvalue;
    double yvalue;

    xxxCircle(double radius, double xvalue, double yvalue){
     this.radius = radius;
     this.xvalue = xvalue;
     this.yvalue = yvalue;
    }

    public double getRadius(){
        return radius;
    }
    public void setRadius(double radius) {
        this.radius = radius;

    }
    public void setXvalue(double xvalue){
        this.xvalue=xvalue;
    }
    public void setYvalue(double yvalue){
        this.yvalue=yvalue;
    }
    public void draw(){
        Circle circle = new Circle();
        circle.setCenterX(xvalue);
        circle.setCenterY(yvalue);
        circle.setRadius(radius);
    }
}

Main.java

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.shape.Circle;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.Group;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{

        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Circle test");
        Scene scene = new Scene(root, 300, 300);
        primaryStage.setScene(scene);

        xxxCircle circle1 = new xxxCircle(100,250,250);
        Group group = new Group();
        Scene circScene = new Scene(group, 500, 500);
        circle1.draw();

        primaryStage.setScene(circScene);

        primaryStage.show();


    }


    public static void main(String[] args) {

        launch(args);
    }
}

1 个答案:

答案 0 :(得分:1)

add Circle遗失了

Group group = new Group();
group.getChildren().add(circle1);  <---missing

Scene circScene = new Scene(group, 500, 500);
circle1.draw();
primaryStage.setScene(circScene);

primaryStage.show();