【问题标题】:Is there any way of hiding the text field of a spinner on JavaFX?有什么方法可以隐藏 JavaFX 上微调器的文本字段吗?
【发布时间】:2021-11-19 05:04:34
【问题描述】:

我正在开发一个 java/javaFX 应用程序,我需要使用一个像微调器这样的组件来为用户提供将某个属性增加/减少 1 个单位的可能性。它已经实现并按我的需要工作,但是理想的情况是隐藏文本字段,因为它根本没有帮助。

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

谢谢

【问题讨论】:

  • 您可能想忘记使用 Spinner,而只需使用 new Button("\u23f6")new Button("\u23f7"),它们应该分别创建带有“⏶”和“⏷”的按钮。

标签: java javafx spinner


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    • 2020-10-26
    • 2021-04-30
    • 1970-01-01
    • 2020-02-22
    相关资源
    最近更新 更多