简体   繁体   English

JavaFX - 只显示一个标签

[英]JavaFX - Only one label is showing

EDIT: SOLVED, ACCIDENTALLY FLIPPED X AND Y VALUES FOR LABEL'S 1 & 2 编辑:为标签1和2解决,意外撕开X和Y值

I'm creating an overlay using JavaFX, but for some reason two of my labels are missing while one is perfectly fine. 我正在使用JavaFX创建一个叠加层,但由于某种原因,我的两个标签丢失而一个完全正常。 It's weird because they all have the same y-value (setLayoutY) and only one label is successfully shown. 这很奇怪,因为它们都具有相同的y值(setLayoutY),并且只成功显示了一个标签。 When changing label 1's y-value from 536 to 500, it then gets show, but cut out. 将标签1的y值从536更改为500时,它会显示,但会被删除。

When label 1's setLayoutY(536): http://i.imgur.com/M5NxQoa.png When label 2's setLayoutY(500): http://i.imgur.com/heJDopx.png 当标签1的setLayoutY(536)时: http//i.imgur.com/M5NxQoa.png当标签2的setLayoutY(500)时: http//i.imgur.com/heJDopx.png

It's weird because it's well within both the pane and stages size (by the way, which one takes precedence ). 这很奇怪,因为它在窗格和阶段大小内都很好(顺便说一下,哪一个优先 )。 On the other hand, label 2 has the same y-value of 536 and is displayed, but the rest aren't. 另一方面,标签2具有相同的y值536并显示,但其余的则不显示。

I really hope this isn't a simple mistake as I'm not seeing what I'm doing wrong. 我真的希望这不是一个简单的错误,因为我没有看到我做错了什么。 Why is this happening and how can I fix it? 为什么会发生这种情况,我该如何解决? Thank you! 谢谢!

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.scene.paint.Color;

public class Overlay extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Pane root = new Pane();
        root.setPrefSize(765,596);

        Label label1 = new Label("why does");
        label1.setLayoutX(97);
        label1.setLayoutY(536);
        label1.setPrefWidth(57);
        label1.setPrefHeight(184);
        label1.setTextFill(Color.BLACK);

        Label label2 = new Label("this happen");
        label2.setLayoutX(481);
        label2.setLayoutY(536);
        label2.setPrefWidth(184);
        label2.setPrefHeight(57);
        label2.setTextFill(Color.BLACK);

        Label label3 = new Label("-1");
        label3.setLayoutX(289);
        label3.setLayoutY(536);
        label3.setPrefWidth(57);
        label3.setPrefHeight(184);
        label3.setTextFill(Color.BLACK);


        root.getChildren().addAll(label1, label2, label3);

        Scene scene = new Scene(root, 765, 596, Color.TRANSPARENT);
        scene.setFill(Color.TRANSPARENT);
        primaryStage.setScene(scene);
        primaryStage.initStyle(StageStyle.TRANSPARENT);
        primaryStage.setAlwaysOnTop(true);
        primaryStage.show();
    }

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

}

Set same PrefHeight in all label 在所有标签中设置相同的PrefHeight

label1.setPrefHeight(57);
label2.setPrefHeight(57);
label3.setPrefHeight(57);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM