JavaFX标签内容未显示在gridpane中

时间:2013-08-29 09:25:32

标签: grid label javafx-2 javafx

为什么myLabel内容没有显示在我的gridpane中,很奇怪,其他一切都在显示..

我尽我所能。任何帮助,请

这里是我的代码的片段和结果的附加照片

enter image description here

@Override public void start(Stage stage) {

        Group root = new Group();
        Scene scene = new Scene(root, 1180, 650);
        stage.setScene(scene);
        stage.setTitle("Prayer Time Display");
        scene.getStylesheets().addAll(this.getClass().getResource("style.css").toExternalForm());

        GridPane Mainpane = new GridPane();
        scene.setRoot(Mainpane);
        Mainpane.setGridLinesVisible(true);
        Mainpane.setId("Mainpane");

        GridPane prayertime_pane = new GridPane();
        prayertime_pane.setId("prayertime_pane");
        prayertime_pane.setPadding(new Insets(20, 20, 20, 20));
        prayertime_pane.setAlignment(Pos.BASELINE_CENTER);
        prayertime_pane.setVgap(7);
        prayertime_pane.setHgap(35);

        GridPane moon_pane = new GridPane();
        moon_pane.setGridLinesVisible(true);
        moon_pane.setPadding(new Insets(10, 10, 10, 10));
        moon_pane.setAlignment(Pos.BASELINE_CENTER);
        moon_pane.setVgap(10);
        moon_pane.setHgap(10);

        myLabel.setText("hello");
        moon_pane.setConstraints(myLabel, 0, 0);
        moon_pane.getChildren().add(myLabel);

        ImageView Moon_img = new ImageView(new Image(getClass().getResourceAsStream("/Images/Full_Moon.png")));    
        Moon_img.setFitWidth(100);
        Moon_img.setFitHeight(100);
        moon_pane.add(Moon_img, 1, 0);

        Text next_moon_text = new Text("13/02");
        next_moon_text.setId("prayer-text-english");
        moon_pane.setHalignment(next_moon_text,HPos.LEFT);
        moon_pane.setValignment(next_moon_text,VPos.CENTER);
        moon_pane.setConstraints(next_moon_text, 2, 0);
        moon_pane.getChildren().add(next_moon_text);

        Mainpane.add(moon_pane, 8, 1,3,2);

1 个答案:

答案 0 :(得分:1)

您的标签确实出现在场景中,但它已经缩小到只渲染文本溢出字符的程度 - “......”

为确保标签始终以适合所有文字的固定宽度呈现,您可以强制最小宽度和最大宽度与首选宽度相同:

  myLabel.setMinWidth(Region.USE_PREF_SIZE);
  myLabel.setMaxWidth(Region.USE_PREF_SIZE);

如果要保留动态大小调整,可以始终使用“风景视图”(http://fxexperience.com/scenic-view/)分析场景图并确定哪些节点或容器占用所有空间或具有大小限制