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) 我需要一些布局元素,允许在一个页面上使用多个标签(或者更确切地说是多个标签/锚烷包)