gpt4 book ai didi

java - 将文本区域自动调整为 javaFX 中的选项卡 Pane

转载 作者:太空宇宙 更新时间:2023-11-03 20:15:06 24 4
gpt4 key购买 nike

我在 TabPane 中有 3 个选项卡,每个选项卡都有一个包含不同文本和不同长度的文本区域。我想根据每个选项卡中的长度自动调整文本区域的大小。我不明白我该怎么办?使用场景生成器? CSS?javaFX 方法?提前致谢...

最佳答案

我想你是在问文本区域是根据其中显示的文本来扩大还是缩小?

如果是这样,看看这段代码是否有帮助:

import java.util.concurrent.Callable;

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class AutosizingTextArea extends Application {

@Override
public void start(Stage primaryStage) {
TextArea textArea = new TextArea();
textArea.setMinHeight(24);
textArea.setWrapText(true);
VBox root = new VBox(textArea);
Scene scene = new Scene(root, 600, 400);
primaryStage.setScene(scene);
primaryStage.show();

// This code can only be executed after the window is shown:

// Perform a lookup for an element with a css class of "text"
// This will give the Node that actually renders the text inside the
// TextArea
Node text = textArea.lookup(".text");
// Bind the preferred height of the text area to the actual height of the text
// This will make the text area the height of the text, plus some padding
// of 20 pixels, as long as that height is between the text area's minHeight
// and maxHeight. The minHeight we set to 24 pixels, the max height will be
// the height of its parent (usually).
textArea.prefHeightProperty().bind(Bindings.createDoubleBinding(new Callable<Double>(){
@Override
public Double call() throws Exception {
return text.getBoundsInLocal().getHeight();
}
}, text.boundsInLocalProperty()).add(20));

}

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

如果你想让它可重用,那么你可以考虑子类化 TextArea。 (一般来说,我不喜欢子类控制类。)这里棘手的部分是执行使 TextArea 在添加到实时场景图中后展开的代码(这对于查找是必需的)上类)。执行此操作的一种方法(恕我直言,这有点 hack)是使用 AnimationTimer 进行查找,一旦查找成功就可以停止。我 mock 了这个here .

关于java - 将文本区域自动调整为 javaFX 中的选项卡 Pane ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22732766/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com