如何强制插入符号位于后缀之前

发布于 2025-01-20 04:22:53 字数 2741 浏览 3 评论 0原文

在 JavaFx 中,我在 TextField 上应用 TextFormatter,以强制插入符号位于后缀之前(我想创建一个度数字段,因此后缀为 °)。

当我启动应用程序并单击文本字段时,出现以下内容:

在此处输入图像描述

我希望插入符永远不会在 ° 之后,如下所示:

在此处输入图像描述

这是我的代码: HelloAppplication.java:

package com.example.demo;

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

import java.io.IOException;

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 320, 240);
        stage.setTitle("Hello!");
        stage.setScene(scene);
        stage.show();
    }

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

HelloController.java:

package com.example.demo;

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

public class HelloController {
    @FXML
    private TextField input;

    @FXML
    private void initialize() {
        input.setText("°");
        input.setTextFormatter(getSuffix("°"));
    }

    public TextFormatter<String> getSuffix(String suffix) {
        return new TextFormatter<>(pChange -> {
            int maxPos = pChange.getControlText().length() - suffix.length();
            if (pChange.isContentChange()) {
                pChange.setRange(Math.min(pChange.getRangeStart(), maxPos), Math.min(pChange.getRangeEnd(), maxPos));
            } else {
                pChange.setCaretPosition(Math.min(pChange.getCaretPosition(), maxPos));
                pChange.setAnchor(Math.min(pChange.getAnchor(), maxPos));
            }
            return pChange;
        });
    }
}

hello-view.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.VBox?>

<VBox alignment="CENTER" spacing="20.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/null" fx:controller="com.example.demo.HelloController">
    <padding>
        <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
    </padding>
   <children>
      <TextField fx:id="input" />
   </children>
</VBox>

你有解决方案吗?

In JavaFx, I apply a TextFormatter on a TextField to force the caret to be before a suffix (I want to create a degree field, so the suffix is °).

When I launch my application and click on the textfield, I have the following:

enter image description here

I want the carret to be never after °, like this:

enter image description here

Here is my code:
HelloAppplication.java:

package com.example.demo;

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

import java.io.IOException;

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 320, 240);
        stage.setTitle("Hello!");
        stage.setScene(scene);
        stage.show();
    }

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

HelloController.java:

package com.example.demo;

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

public class HelloController {
    @FXML
    private TextField input;

    @FXML
    private void initialize() {
        input.setText("°");
        input.setTextFormatter(getSuffix("°"));
    }

    public TextFormatter<String> getSuffix(String suffix) {
        return new TextFormatter<>(pChange -> {
            int maxPos = pChange.getControlText().length() - suffix.length();
            if (pChange.isContentChange()) {
                pChange.setRange(Math.min(pChange.getRangeStart(), maxPos), Math.min(pChange.getRangeEnd(), maxPos));
            } else {
                pChange.setCaretPosition(Math.min(pChange.getCaretPosition(), maxPos));
                pChange.setAnchor(Math.min(pChange.getAnchor(), maxPos));
            }
            return pChange;
        });
    }
}

hello-view.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.VBox?>

<VBox alignment="CENTER" spacing="20.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/null" fx:controller="com.example.demo.HelloController">
    <padding>
        <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
    </padding>
   <children>
      <TextField fx:id="input" />
   </children>
</VBox>

Have you got a solution ?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文