标签未显示在窗格JAVAFX

时间:2019-02-02 18:33:10

标签: javafx label pane

您好,我在使用Java FX制作的SnakeGame中将新标签插入窗格中时遇到问题。我想插入一个标签文本,以显示比分,但是当我补充一点,它不显示。

现在,我将显示代码的主要部分,并在GamePanel类的名为“ initialize()”的方法中,尝试插入一个名为“ Punteggio”的Label。问题是,如果添加其他对象,例如矩形,圆形,则会显示它们,但不会显示标签,按钮。 我哪里出问题了?

     /Main.java
    public class Main extends Application {
@Override
public void start(Stage primaryStage) {
    try {
        GamePanel g = new GamePanel();

        BorderPane root = new BorderPane();
        root.setBackground(Background.EMPTY);
        //Scene scene = new Scene(root,400,400, Color.BLACK);
        Scene scene = new Scene(root,400,400);
        scene.setFill(Color.BLACK);


        primaryStage.setScene(scene);
        primaryStage.setTitle("SnakeFX");

        primaryStage.show();


        root.getChildren().add(g);

        g.requestFocus();

    } catch(Exception e) {
        e.printStackTrace();
    }

}

public static void main(String[] args) {
    launch(args);
}
 }



      /GamePanel.java
     public class GamePanel extends BorderPane {
public final int WIDTH = 400, HEIGHT = 400;
ArrayList<BodyPart> Snake;
boolean right = true, left = false, up = false, down = false;
ArrayList<Fruit> Fruits;
BorderPane gamepanel;

AnimationTimer game = new AnimationTimer() {
    private long time = 0;

    @Override
    public void handle(long now) {
        //it is not important
};

GamePanel(){

gamepanel = new BorderPane();

initialize();

game.start();

}



void initialize() {     

    Label Punteggio = new Label();
    Punteggio.setText("Testo di prova");
    Punteggio.setTextFill(Color.RED);
    Punteggio.relocate(200.0, -100.0); //I tried to change this coordinates 
                                       //too 

    Punteggio.setVisible(true);
    super.getChildren().add(Punteggio);


    for(int i=0;i<WIDTH;i=i+WIDTH/40) {
        Line line = new Line();
        line.setStartX((double)i);
        line.setStartY(0.0);
        line.setEndX((double)i);
        line.setEndY((double)HEIGHT);
        line.setVisible(true);
        super.getChildren().add(line);
    }

    for(int j=0;j<HEIGHT;j=j+HEIGHT/40) {
        Line line = new Line();
        line.setStartY((double)j);
        line.setStartX(0.0);
        line.setEndY((double)j);
        line.setEndX((double)WIDTH);
        line.setVisible(true);
        super.getChildren().add(line);
    }

    Snake = new ArrayList<BodyPart>();
    for(int i=0;i<70;i=i+10) {
        BodyPart b = new BodyPart();
        b.setX((double)i+20);
        b.setY((double)20);
        Snake.add(b);
        super.getChildren().add(b);
        Snake.get(i/10).right = true;
    }

    Fruits = new ArrayList<Fruit>();
    double rand_x, rand_y;
    rand_x = Math.random();
    rand_y = Math.random();
    Fruit f = new Fruit();
    f.setX( (int) (rand_x*WIDTH) - (int) (rand_x*WIDTH)%10 );
    f.setY( (int) (rand_y*HEIGHT) - (int) (rand_y*HEIGHT)%10 );
    Fruits.add(f);
    f.setVisible(true);
    super.getChildren().add(f);

     }

enter image description here

0 个答案:

没有答案