简体   繁体   English

JavaFX TextField使用等宽字体调整大小

[英]JavaFX TextField resize with monospace font

I want to make a TextField that automatically resizes to the size of its content. 我想创建一个自动调整大小到其内容大小的TextField。 If I use textField.prefColumnCountProperty().bind(textField.textProperty().length()); 如果我使用textField.prefColumnCountProperty().bind(textField.textProperty().length()); it works. 有用。 But as soon as I use a monospaced font the TextField resizes but the text is invisible until I select it. 但是一旦我使用等宽字体,TextField会调整大小,但文本在我选择之前是不可见的。 How can I use the resize property with a monospaced font? 如何使用等宽字体的resize属性?

Before selecting the text: 在选择文本之前:

在选择文本之前

After selecting the text: 选择文字后:

选择文本后

Main.java Main.java

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


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

Controller.java Controller.java

package sample;

import javafx.fxml.FXML;
import javafx.scene.control.TextField;


public class Controller {

    @FXML
    private TextField textField;

    @FXML
    public void initialize() {
        textField.setStyle("-fx-font-family: 'monospaced';");
        textField.prefColumnCountProperty().bind(textField.textProperty().length());
    }

}

sample.fxml sample.fxml

<?import javafx.scene.layout.GridPane?>

<?import javafx.scene.control.TextField?>
<GridPane fx:controller="sample.Controller"
          xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
        <TextField fx:id="textField"></TextField>
</GridPane>

Short: 短:

The problem only occurs when getAlignment() is set to left ( BASELINE_LEFT , BOTTOM_LEFT , CENTER_LEFT or TOP_LEFT ) so just use textField.setAlignment(Pos.CENTER); 仅当getAlignment()设置为left( BASELINE_LEFTBOTTOM_LEFTCENTER_LEFTTOP_LEFT )时才会出现此问题,因此只需使用textField.setAlignment(Pos.CENTER); .


Long answer since this is still a strange behavior: 很长的答案,因为这仍然是一个奇怪的行为:

The text is positioned incorrectly, all the text is moved to the left out of the visible area. 文本定位不正确,所有文本都移动到可见区域的左侧。 It seems like the cause of this behavior is that the width of the textfield is to small. 看起来这种行为的原因是文本字段的宽度很小。

在此输入图像描述

This is a small example where the text "test" is typed and then moves the cursor to the left. 这是一个小例子,其中键入文本“test”,然后将光标移动到左侧。


Well i guess it's a problem how TextFieldSkin.computePrefWidth works, since it doesn't calculate the size of the cursor. 嗯,我想这是TextFieldSkin.computePrefWidth如何工作的问题,因为它不计算光标的大小。

You could create your own TextFieldSkin which computePrefWidth returns a bigger value: (This code-sample uses com.sun.javafx.scene.control.skin.TextFieldSkin.TextFieldSkin for Java-8) 您可以创建自己的TextFieldSkin,computePrefWidth返回一个更大的值:(此代码示例使用com.sun.javafx.scene.control.skin.TextFieldSkin.TextFieldSkin for Java-8)

textField.setSkin(new TextFieldSkin(textField){
    @Override
    protected double computePrefWidth(double height, double topInset, double rightInset, double bottomInset, double leftInset) {
        return 1 + super.computePrefWidth(height, topInset, rightInset, bottomInset, leftInset);
    }
});

You could also one to the prefColumnCount 您也可以使用prefColumnCount

textField.prefColumnCountProperty().bind(textField.textProperty().length().add(1));

Also see: What is the prefColumnCount property for a TextField? 另请参阅: TextField的prefColumnCount属性是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM