Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 锚烷标签的尺寸调整_Java_Javafx_Javafx 8 - Fatal编程技术网

Java 锚烷标签的尺寸调整

Java 锚烷标签的尺寸调整,java,javafx,javafx-8,Java,Javafx,Javafx 8,我在主播中有一个JavaFX标签,它包含在VBox中。这一个位于滚动窗格中。我希望文本自动换行并适应窗口的宽度 下面是我的代码的简化示例: public class MainApp extends Application { public static void main(String[] args) { Application.launch(MainApp.class); } @Override public void start(Stage

我在
主播
中有一个JavaFX
标签
,它包含在
VBox
中。这一个位于
滚动窗格中。我希望文本自动换行并适应窗口的宽度

下面是我的代码的简化示例:

public class MainApp extends Application {

    public static void main(String[] args) {
        Application.launch(MainApp.class);
    }

    @Override
    public void start(Stage primaryStage) {
        final ScrollPane root = new ScrollPane();
        root.setFitToWidth(true);

        VBox vbx = new VBox();
        vbx.setStyle("-fx-background-color: green");
        vbx.setSpacing(20);
        root.setContent(vbx);

        Label lbl = new Label(
                "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, " +
                "sed diam nonumy eirmod tempor invidunt ut labore et dolore " + 
                "magna aliquyam erat, sed diam voluptua. At vero eos et " +
                "accusam et justo duo dolores et ea rebum.");
        lbl.setStyle("-fx-background-color: yellow");
        lbl.setWrapText(true);

        AnchorPane ap = new AnchorPane();
        ap.setStyle("-fx-background-color: blue");
        ap.getChildren().add(lbl);
        vbx.getChildren().add(ap);

        lbl.maxWidthProperty().bind(ap.widthProperty());
        lbl.setTextAlignment(TextAlignment.JUSTIFY);

        AnchorPane.setLeftAnchor(lbl, 0.0);
        AnchorPane.setTopAnchor(lbl, 0.0);
        AnchorPane.setRightAnchor(lbl, 0.0);
        AnchorPane.setBottomAnchor(lbl, 0.0);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
问题是:当我启动程序时,
标签
远高于可用高度,文本垂直居中。当我调整窗口的宽度时,文本跳到顶部,其高度减小到正确的大小。但是
VBox
仍然很大

我必须改变什么,文本最初处于正确的位置(在顶部),而不必调整窗口的大小,以及如何使VBox不超过必要的大小


请注意:
AnchorPane
之所以存在,是因为某些原因需要很长时间才能解释,因此我无法删除它。此外,我无法将
标签更改为
文本,因为它必须至少保留从
区域派生的任何内容
您可能不应该
滚动窗格中添加
VBox
作为根节点。我这样说是因为
SceneBuilder
不会让我这么做。在本例中,我将根节点设置为
AnchorPane
。然后我添加
VBox
及其子项。我将
标签的
宽度绑定到
阶段

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

public class MainApp extends Application
{

    public static void main(String[] args)
    {
        Application.launch(MainApp.class);
    }

    @Override
    public void start(Stage primaryStage)
    {
        final ScrollPane root = new ScrollPane();
        root.setFitToWidth(true);

        Label lbl = new Label(
                "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, "
                + "sed diam nonumy eirmod tempor invidunt ut labore et dolore "
                + "magna aliquyam erat, sed diam voluptua. At vero eos et "
                + "accusam et justo duo dolores et ea rebum.");
        lbl.setStyle("-fx-background-color: yellow");
        lbl.setWrapText(true);

        AnchorPane ap = new AnchorPane();
        ap.setStyle("-fx-background-color: blue");
        ap.getChildren().add(lbl);

        lbl.setTextAlignment(TextAlignment.JUSTIFY);
        lbl.setMaxHeight(Double.MAX_VALUE);

        VBox vBox = new VBox(ap);
        AnchorPane rootAP = new AnchorPane(vBox);
        root.setContent(rootAP);
        Scene scene = new Scene(root, 300, 250);

        lbl.prefWidthProperty().bind(scene.widthProperty());//This should bind the Label width to the Scene width

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

worksforme(在fx10中,没有检查其他版本)我现在在fx10中进行了测试,但我仍然遇到同样的问题。
VBox
是否也存在,原因可能需要很长时间才能解释?不,可以更改、删除或替换
VBox
。我只需要它来布局多个标签。解决方案有两个问题:1)文本在窗口中垂直居中,我需要它与顶部对齐。2) 我需要一些布局元素,允许在一个页面上使用多个标签(或者更确切地说是多个标签/锚烷包)