有没有办法在 JavaFX 上隐藏微调器的文本字段?

Is there any way of hiding the text field of a spinner on JavaFX?

我正在开发一个 java/javaFX 应用程序,我需要使用像微调器这样的组件来为用户提供 increasing/decreasing 1 个单位 属性 的可能性。它已经按照我的需要实施和工作。但是,理想的做法是隐藏文本字段,因为它根本没有用。

有谁知道隐藏它的方法或可以类似工作的替代组件吗?

谢谢

此代码似乎可以完成您想要的。

.spinner .text-field {
    visibility: hidden;
    -fx-pref-width: 2em;
    -fx-pref-height: 2.5em;
}

虽然这是一个使用 CSS 的 hack。更好的解决方案可能是创建自定义皮肤或自定义控件,但我不会在这里尝试。也许 hack 足以满足您的目的。

这个 hack 的奇怪之处在于设置隐藏文本字段的 pref 宽度和高度将使箭头可见(如果您只是将文本字段的 pref 宽度和高度设置为零,那么箭头也不可见)。

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Spinner;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class HiddenTextSpinner extends Application {

    private static final String NO_TEXT_SPINNER_CSS = """
            data:text/css,
            .spinner .text-field {
                visibility: hidden;
                -fx-pref-width: 2em;
                -fx-pref-height: 2.5em;
            }
            """;

    @Override
    public void start(Stage stage) {
        Spinner<Integer> spinner = new Spinner<>(0, 10, 5);
        spinner.getStylesheets().add(NO_TEXT_SPINNER_CSS);
        spinner.setEditable(false);

        VBox layout = new VBox(10, spinner);
        layout.setPadding(new Insets(10));
        Scene scene = new Scene(new VBox(layout));
        stage.setScene(scene);
        stage.show();
    }

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