【问题标题】:JavaFx: Sizing, when a Pane contains textJavaFx:调整大小,当窗格包含文本时
【发布时间】:2017-12-04 22:53:09
【问题描述】:

在 JavaFX 中,我在容器窗格内的 AnchorPane(或 StackPane)内有一个标签(或 TextFlow)。

容器窗格的高度是无限的,但它的宽度固定为我想要的任何值。

我需要:

1) AnchorPane 的宽度填充所有可用空间,但不大于标签在一行中的宽度。

2) 标签为多行 (wrap=true) 并填充所有可用空间。

3) AnchorPane 的高度要等于 Label 的高度。

代码设置:

Pane rootPane = new Pane();
rootPane.setBackground(new Background(new BackgroundFill(Color.FIREBRICK, CornerRadii.EMPTY, Insets.EMPTY)));
AnchorPane anchorPane = new AnchorPane();
anchorPane.setBackground(new Background(new BackgroundFill(Color.FORESTGREEN, CornerRadii.EMPTY, Insets.EMPTY)));
Label text = new Label("text text text text text text text text text text text");
anchorPane.getChildren().add(text);
rootPane.getChildren().add(anchorPane);

或者,我可以使用以下标签:

TextFlow text = new TextFlow(new Text("text text text text text text text text text text text"));

我在这个问题上浪费了几个小时。这在 HTML 中是如此简单……我怎样才能做到这一点?

【问题讨论】:

  • 发布您的FXML 的简化版本。
  • @SedrickJefferson 我没有 FXML。

标签: java javafx


【解决方案1】:

代码

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;


public class Main extends Application { 

    @Override
    public void start(Stage primaryStage) throws Exception{
        Pane rootPane = new Pane();

        Label text = new Label();        
        text.setWrapText(true);
        text.setText("text text text text text text text text text text text");

        AnchorPane anchorPane = new AnchorPane();
        anchorPane.getChildren().add(text);
        anchorPane.setBackground(new Background(new BackgroundFill(Color.FORESTGREEN, CornerRadii.EMPTY, Insets.EMPTY)));

        rootPane.getChildren().add(anchorPane);
        rootPane.setBackground(new Background(new BackgroundFill(Color.FIREBRICK, CornerRadii.EMPTY, Insets.EMPTY)));

        primaryStage.setScene(new Scene(rootPane, 300, 275));
        primaryStage.show();

        rootPane.widthProperty().addListener(new ChangeListener<Number>() {

            private double textWidth;

            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {

                if(text.getWidth() > 0 && textWidth == 0) {
                    textWidth = text.getWidth();
                }

                if(textWidth > 0 && textWidth > rootPane.getWidth())
                {
                    text.setPrefWidth(rootPane.getWidth());
                }else
                {
                    text.setPrefWidth(Label.USE_COMPUTED_SIZE);
                }
            }
        });
    }


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

结果

【讨论】:

  • 这不使用标签,但是好的,我可以使用文本。但是问题是它闪烁。要查看它,请尝试更长的文本。每次调整它的大小都会出错。
  • 确实如此。在我的 linux 机器上没有看到它,因为我的窗口管理器在调整大小时没有更新。标签/文本处理包装的方式似乎存在一些问题。今天我会再看一遍并更新我的答案。
  • 如何通过屏幕截图制作这些动画 gif?
  • 你需要声明textWidth字段对吧?:private double textWidth;
  • 我已添加声明。谢谢。
猜你喜欢
  • 1970-01-01
  • 2015-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-15
  • 1970-01-01
  • 2014-12-24
  • 1970-01-01
相关资源
最近更新 更多