我正在使用JAVAFX进行聊天应用。消息显示在textArea中,但textArea总是以相同的大小显示。如何使textArea完全适合文本量? TNX
答案 0 :(得分:1)
以下代码完全符合您的要求
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);
}