[英]Is there any way of hiding the text field of a spinner on JavaFX?
我正在开发一个 java/javaFX 应用程序,我需要使用像微调器这样的组件来为用户提供将某个属性增加/减少 1 个单位的可能性。 它已经按照我的需要实施和工作。 但是,理想的情况是隐藏文本字段,因为它根本没有帮助。
有谁知道隐藏它的方法或可以类似工作的替代组件?
谢谢
此代码似乎可以完成您想要的。
.spinner .text-field {
visibility: hidden;
-fx-pref-width: 2em;
-fx-pref-height: 2.5em;
}
这是一个使用 CSS 的黑客攻击。 更好的解决方案可能是创建自定义皮肤或自定义控件,但我不会在这里尝试。 也许黑客足以满足您的目的。
关于 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.