微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

限制 TextField 的文本区域边距/填充

如何解决限制 TextField 的文本区域边距/填充

我在 JavaFX 中使用了一些 TextField,我想限制它们显示文本的区域。我已经限制了它们的最大字符数,但在某些情况下,键入的最大字符数大于文本字段的宽度。如您所见,文本与我添加自定义擦除文本按钮重叠。

我想知道是否可以将文本的右边距向左移动一点(不更改 TextField 的属性 - 大小、坐标),这样按钮和文本就不再重叠了。

我已经尝试过边距、填充,但它们不能满足我的需求。

Example

如何限制 TextField 的最大长度(来自 stackoverflow):

public class LimitedJFXTextField extends JFXTextField {

    private final IntegerProperty maxLength;

    public LimitedJFXTextField() {
        super();
        this.maxLength = new SimpleIntegerProperty(-1);
    }

    public IntegerProperty maxLengthproperty() {
        return this.maxLength;
    }

    public final Integer getMaxLength() {
        return this.maxLength.getValue();
    }

    public final void setMaxLength(Integer maxLength) {
        Objects.requireNonNull(maxLength,"Max length cannot be null,-1 for no limit");
        this.maxLength.setValue(maxLength);
    }

    @Override
    public void replaceText(int start,int end,String insertedText) {
        if (this.getMaxLength() <= 0) {
            // Default behavior,in case of no max length
            super.replaceText(start,end,insertedText);
        } else {
            // Get the text in the textfield,before the user enters something
            String currentText = this.getText() == null ? "" : this.getText();

            // Compute the text that should normally be in the textfield Now
            String finalText = currentText
                    .substring(0,start) + insertedText + currentText
                    .substring(end);

            // If the max length is not excedeed
            int numberOfexceedingCharacters = finalText.length() - this
                    .getMaxLength();
            if (numberOfexceedingCharacters <= 0) {
                // normal behavior
                super.replaceText(start,insertedText);
            } else {
                // Otherwise,cut the the text that was going to be inserted
                String cutInsertedText = insertedText.substring(
                        0,insertedText.length() - numberOfexceedingCharacters
                );

                // And replace this text
                super.replaceText(start,cutInsertedText);
            }
        }
    }
}

解决方法

这是使用 ControlsFX 的解决方案。

import java.io.IOException;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import org.controlsfx.control.textfield.CustomTextField;

/**
 * JavaFX App
 */
public class App extends Application
{

    private static Scene scene;

    @Override
    public void start(Stage stage) throws IOException
    {
        CustomTextField customTextField = new CustomTextField();
        customTextField.setText("Hello World!");

        Label labelX = new Label("x");
        labelX.setTextFill(Color.RED);
        customTextField.setRight(labelX);
        customTextField.setMaxWidth(200);

        scene = new Scene(new StackPane(customTextField),500,500);//loadFXML("primary"),640,480);
        stage.setScene(scene);
        stage.show();
    }
}

enter image description here

,

我从未使用过 JFXTextField,所以请谨慎对待这个答案。

但是,从 JFXTextField extends TextField 开始,您应该能够更改 TextField 的属性。其中之一称为padding

所以只使用“普通”TextField,你可以使用这个:

public class ExampleApp extends Application {

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

    @Override
    public void start(Stage stage) {
        var textField = new TextField();
        textField.setPadding(new Insets(0,50,0)); // This adds 50px right padding
        textField.setText("Hello World!");

        var label = new Label("x");

        var pane = new StackPane(textField,label);
        StackPane.setAlignment(label,Pos.CENTER_RIGHT);

        var scene = new Scene(pane,500);
        stage.setScene(scene);
        stage.show();
    }
}

或者,如果您更喜欢 FXML,请使用:

<TextField>
    <padding>
        <Insets right="50"/>
    </padding>
</TextField>

StackPane 和 Label 只是为了展示一些重叠的项目,与 Sedricks 的回答相同。实际上限制文本字段内文本的重要部分是填充。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。