gpt4 book ai didi

java - 调整 AnchorPane 中标签的大小

转载 作者:行者123 更新时间:2023-11-30 06:05:04 24 4
gpt4 key购买 nike

我在 AnchorPane 内有一个 JavaFX Label,它包含在 VBox 中。这个位于 ScrollPane 中。我希望文本自动换行并自动适应窗口的宽度。

这是我的代码的简化示例:

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仍然很大。

我必须更改什么,文本最初位于正确的位置(顶部)而无需调整窗口大小以及如何使垂直框不大于必要的大小?

请注意:AnchorPane 的存在是由于某些需要很长时间才能解释的原因,所以我无法删除它。另外,我无法将 Label 更改为 Text,因为它必须至少保留从 Region

派生的任何内容

最佳答案

您可能添加VBox作为ScrollPane中的根节点。我这么说是因为 SceneBuilder 不会让我这么做。在此示例中,我将根节点设为 AnchorPane。然后我添加 VBox 及其子项。我将 Label's 宽度绑定(bind)到 Stage

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();
}
}

关于java - 调整 AnchorPane 中标签的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51514036/

24 4 0