JAVAFX使动态textArea大小 [英] JAVAFX Make dynamic textArea size

查看:1281
本文介绍了JAVAFX使动态textArea大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JAVAFX进行聊天应用。消息显示在textArea中,但textArea总是以相同的大小显示。如何使textArea完全适合文本量? TNX

I'm making chat application using JAVAFX. The messages displayed in textArea, but the textArea always in the same size. How can I make the textArea to fit exactly to the amount of text? TNX

推荐答案

以下代码完全符合您的要求

The following code does exactly what You wants

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {

        AnchorPane root = new AnchorPane();
        Scene scene = new Scene(root,400,400);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());


        TextArea ta=new TextArea(); 
        ta.addEventHandler(KeyEvent.KEY_RELEASED, new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                 // your algorithm to change height
                 ta.setPrefHeight(ta.getPrefHeight()+10); 
            }
        });
        root.getChildren().add(ta);

        primaryStage.setScene(scene);
        primaryStage.show();

    } catch(Exception e) {
        e.printStackTrace();
    }
}

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

这篇关于JAVAFX使动态textArea大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆