简体   繁体   English

有什么方法可以隐藏 JavaFX 上微调器的文本字段?

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

I'm working on a java/javaFX application where I need to use a component like a spinner to give the user the possibility of increasing/decreasing by 1 unit a certain property.我正在开发一个 java/javaFX 应用程序,我需要使用像微调器这样的组件来为用户提供将某个属性增加/减少 1 个单位的可能性。 It's already implemented and working as I need.它已经按照我的需要实施和工作。 However, the ideal would be to hide the text field as it's not helpful at all.但是,理想的情况是隐藏文本字段,因为它根本没有帮助。

Does anyone know a way to hide it or an alternative component that could work similarly?有谁知道隐藏它的方法或可以类似工作的替代组件?

Thank you谢谢

This code appears to accomplish what you want.此代码似乎可以完成您想要的。

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

It is a hack using CSS though.这是一个使用 CSS 的黑客攻击。 A better solution might be to create a custom skin or a custom control, but I won't try that here.更好的解决方案可能是创建自定义皮肤或自定义控件,但我不会在这里尝试。 Perhaps the hack will suffice for your purposes.也许黑客足以满足您的目的。

The weird thing about the hack is that setting the pref width and height of the hidden text field will allow the arrows to be visible (if you just set pref width and height of the text field to zero, then the arrows aren't visible either).关于 hack 的奇怪之处在于,设置隐藏文本字段的首选项宽度和高度将允许箭头可见(如果您只是将文本字段的首选项宽度和高度设置为零,那么箭头也不可见)。

没有文字微调器

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();
    }
}

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

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