Javafx:如何摆脱可扩展的Textarea中的怪异包装效果?

发布于 2025-01-24 03:56:12 字数 1494 浏览 2 评论 0原文

你好,我的文字塔里亚面临问题。我的目标是制作一个可扩展的文本方面,这只是一个普通的文本方面,除了它没有滚动条,它包装文本,如果用户想调整文本方面的宽度大小,则需要更新高度。

除了一件事,一切都很好。假设我已经在文本方面输入了一段段落,然后减少了它的宽度,以便由于包裹而将1或2个字母推入下一行中,那么由于某些奇怪的原因,高度没有更新。

beofre i降低宽度

​还是我的文本节点有问题?如果有人能帮助我,我会非常感谢,因为我已经坐了好几天了。

这是我的代码:

@Override
public void start(Stage stage) throws IOException {

    VBox box = new VBox();
    TextArea area = new TextArea();

    area.setWrapText(true);

    area.setMinHeight(27);
    area.setPrefHeight(27);
    area.setMaxHeight(27);

    box.getChildren().add(area);

    Scene scene = new Scene(box);
    scene.getStylesheets().add(this.getClass().getResource("/gui/dumps/test.css").toExternalForm());

    stage.setScene(scene);
    stage.show();

    area.textProperty().addListener((obs, old, niu) -> {
        setHeight(area);
    });

    area.layoutBoundsProperty().addListener((observable, oldValue, newValue) -> {
        setHeight(area);
    });
}

public void setHeight(TextArea area) {
    Text text = (Text) area.lookup(".text");

    double height = text.getLayoutBounds().getHeight() + 10;

    area.setMinHeight(height);
    area.setPrefHeight(height);
    area.setMaxHeight(height);
}

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

我的CSS样式表:

.text-area .scroll-pane {
-fx-hbar-policy: NEVER;
-fx-vbar-policy: NEVER;

}

hello iam facing a problem with my textarea. my goal is to make an expandable textarea which is just a normal textarea except it has no scrollbar, it wraps text and if the user wants to resize the width of the textarea the height needs to be updated.

everything works fine except one thing. lets say I already typed a paragraph into my textarea and then reduce the width of it just as much so that 1 or 2 letter are pushed in the next line because of wrapping then the height is not updating for some weird reason.

beofre i reduce width

after I reduced the width

iam not sure what iam doing wrong, do i use wrong listeners? or is there an problem with my text node? i would be very thankful if anyone can help me because iam sitting at this problem for days now.

here is my code:

@Override
public void start(Stage stage) throws IOException {

    VBox box = new VBox();
    TextArea area = new TextArea();

    area.setWrapText(true);

    area.setMinHeight(27);
    area.setPrefHeight(27);
    area.setMaxHeight(27);

    box.getChildren().add(area);

    Scene scene = new Scene(box);
    scene.getStylesheets().add(this.getClass().getResource("/gui/dumps/test.css").toExternalForm());

    stage.setScene(scene);
    stage.show();

    area.textProperty().addListener((obs, old, niu) -> {
        setHeight(area);
    });

    area.layoutBoundsProperty().addListener((observable, oldValue, newValue) -> {
        setHeight(area);
    });
}

public void setHeight(TextArea area) {
    Text text = (Text) area.lookup(".text");

    double height = text.getLayoutBounds().getHeight() + 10;

    area.setMinHeight(height);
    area.setPrefHeight(height);
    area.setMaxHeight(height);
}

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

and my css stylesheet:

.text-area .scroll-pane {
-fx-hbar-policy: NEVER;
-fx-vbar-policy: NEVER;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不美如何 2025-01-31 03:56:12

可能是您可以从以下解决方案开始,看看是否需要进一步的更改。

这个想法是要计算textarea的LayoutChildren()完成的高度,然后请求TextArea的布局以更新到计算高度。

您可能需要考虑其他条件,但我会留下来供您合作。

import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.IOException;

public class ResizableTextAreaDemo extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        VBox box = new VBox();
        TextArea area = new TextArea() {
            double insets;
            Node text;

            @Override
            protected void layoutChildren() {
                super.layoutChildren();
                // By this line, all the children layouts are computed.

                // Avoiding repetitive lookup calls.
                if (text == null) {
                    Region scrollPane = (Region) lookup(".scroll-pane");
                    Region content = (Region) lookup(".content");
                    double textAreaInsets = getInsets().getTop() + getInsets().getBottom();
                    double scrollInsets = scrollPane.getInsets().getTop() + scrollPane.getInsets().getBottom();
                    double contentInsets = content.getInsets().getTop() + content.getInsets().getBottom();
                    insets = textAreaInsets + scrollInsets + contentInsets;
                    text = lookup(".text");
                }

                // Compute the total height considering all the required insets.
                double totalHeight = insets + Math.ceil(text.getLayoutBounds().getHeight());
                setTextAreaHeight(this, totalHeight);

                // Finally, requesting layout of TextArea to resize to new value.
                requestLayout();
            }
        };
        area.setWrapText(true);
        box.getChildren().add(area);
        Scene scene = new Scene(box);
        stage.setScene(scene);
        stage.setTitle("TextArea Demo");
        stage.show();
    }

    public void setTextAreaHeight(TextArea area, double height) {
        area.setMinHeight(height);
        area.setPrefHeight(height);
        area.setMaxHeight(height);
    }

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

May be you can start with the below solution and see if you need any further changes.

The idea is to compute the height when the layoutChildren() of the TextArea is completed and then requesting the layout of TextArea to update to the computed height.

There may be other conditions you may need to consider, but I will leave that for you to work with.

enter image description here

import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.IOException;

public class ResizableTextAreaDemo extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        VBox box = new VBox();
        TextArea area = new TextArea() {
            double insets;
            Node text;

            @Override
            protected void layoutChildren() {
                super.layoutChildren();
                // By this line, all the children layouts are computed.

                // Avoiding repetitive lookup calls.
                if (text == null) {
                    Region scrollPane = (Region) lookup(".scroll-pane");
                    Region content = (Region) lookup(".content");
                    double textAreaInsets = getInsets().getTop() + getInsets().getBottom();
                    double scrollInsets = scrollPane.getInsets().getTop() + scrollPane.getInsets().getBottom();
                    double contentInsets = content.getInsets().getTop() + content.getInsets().getBottom();
                    insets = textAreaInsets + scrollInsets + contentInsets;
                    text = lookup(".text");
                }

                // Compute the total height considering all the required insets.
                double totalHeight = insets + Math.ceil(text.getLayoutBounds().getHeight());
                setTextAreaHeight(this, totalHeight);

                // Finally, requesting layout of TextArea to resize to new value.
                requestLayout();
            }
        };
        area.setWrapText(true);
        box.getChildren().add(area);
        Scene scene = new Scene(box);
        stage.setScene(scene);
        stage.setTitle("TextArea Demo");
        stage.show();
    }

    public void setTextAreaHeight(TextArea area, double height) {
        area.setMinHeight(height);
        area.setPrefHeight(height);
        area.setMaxHeight(height);
    }

    public static void main(String[] args) {
        launch(args);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文