Java 如何更改单选按钮标签的位置?
默认情况下,单选按钮的文本标签位于按钮右侧。我希望标签显示在按钮下方。我找到了一个解决方案,但解决方案不是很好,或者根本不起作用 我可以创建一个带有无文本单选按钮和单独文本标签的自定义组件,并将它们定位在VBox中。但是,只有按钮本身响应用户事件,而不是整个事件Java 如何更改单选按钮标签的位置?,java,css,javafx,radio-button,javafx-8,Java,Css,Javafx,Radio Button,Javafx 8,默认情况下,单选按钮的文本标签位于按钮右侧。我希望标签显示在按钮下方。我找到了一个解决方案,但解决方案不是很好,或者根本不起作用 我可以创建一个带有无文本单选按钮和单独文本标签的自定义组件,并将它们定位在VBox中。但是,只有按钮本身响应用户事件,而不是整个事件 没有简单的方法来重新定位标签吗?没有简单的方法来执行此操作简单的方法是设置单个属性或类似的内容 作为一种解决方法,您可以使用VBox执行您提到的操作,但使用标签:您可以将RadioButton设置为标签的图形,并将RadioButton
没有简单的方法来重新定位标签吗?没有简单的方法来执行此操作简单的方法是设置单个属性或类似的内容 作为一种解决方法,您可以使用VBox执行您提到的操作,但使用标签:您可以将RadioButton设置为标签的图形,并将RadioButton设置为顶部,RadioButton放置在标签顶部。然后,您可以在标签上添加事件处理程序,以在单击时选择RadioButton 此方法的一个示例 以及生产的单选按钮: 或者,如果要使RadioButton的文本围绕点旋转,可以使用CSS旋转,属性为-fx rotate: 第一个选择器将旋转整个单选按钮,因此文本将倒置放置在点的左侧。第二个选择器将文本旋转回法线方向 范例 此示例显示一个单选按钮,其文本可以放置在组合框选择指定的点的任意一侧
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
Scene scene = new Scene(root, 400, 400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
RadioButton rb = new RadioButton("In all directions);
ComboBox<PseudoClass> combo = new ComboBox<>();
combo.getItems().addAll(PseudoClass.getPseudoClass("left"),
PseudoClass.getPseudoClass("top"),
PseudoClass.getPseudoClass("right"),
PseudoClass.getPseudoClass("bottom"));
combo.valueProperty().addListener((obs, oldVal, newVal) -> {
if (oldVal != null)
rb.pseudoClassStateChanged(oldVal, false);
rb.pseudoClassStateChanged(newVal, true);
});
root.setTop(combo);
root.setCenter(rb);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
和显示的单选按钮:
没有简单的方法可以做到这一点,简单的方法是设置单个属性或类似的内容 作为一种解决方法,您可以使用VBox执行您提到的操作,但使用标签:您可以将RadioButton设置为标签的图形,并将RadioButton设置为顶部,RadioButton放置在标签顶部。然后,您可以在标签上添加事件处理程序,以在单击时选择RadioButton 此方法的一个示例 以及生产的单选按钮: 或者,如果要使RadioButton的文本围绕点旋转,可以使用CSS旋转,属性为-fx rotate: 第一个选择器将旋转整个单选按钮,因此文本将倒置放置在点的左侧。第二个选择器将文本旋转回法线方向 范例 此示例显示一个单选按钮,其文本可以放置在组合框选择指定的点的任意一侧
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
Scene scene = new Scene(root, 400, 400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
RadioButton rb = new RadioButton("In all directions);
ComboBox<PseudoClass> combo = new ComboBox<>();
combo.getItems().addAll(PseudoClass.getPseudoClass("left"),
PseudoClass.getPseudoClass("top"),
PseudoClass.getPseudoClass("right"),
PseudoClass.getPseudoClass("bottom"));
combo.valueProperty().addListener((obs, oldVal, newVal) -> {
if (oldVal != null)
rb.pseudoClassStateChanged(oldVal, false);
rb.pseudoClassStateChanged(newVal, true);
});
root.setTop(combo);
root.setCenter(rb);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
和显示的单选按钮:
非常完整的答案和工程。非常感谢。为了支持切换组,我最终根据您提供的代码创建了一个类,因为标签不支持切换组,它需要将其委托给它的单选按钮。除非您有更好的方法来支持切换组,因为这个答案非常完整,而且有效。非常感谢。为了支持切换组,我根据您提供的代码创建了一个类,因为标签不支持切换组,它需要将其委托给它的单选按钮
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
Scene scene = new Scene(root, 400, 400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
RadioButton rb = new RadioButton("In all directions);
ComboBox<PseudoClass> combo = new ComboBox<>();
combo.getItems().addAll(PseudoClass.getPseudoClass("left"),
PseudoClass.getPseudoClass("top"),
PseudoClass.getPseudoClass("right"),
PseudoClass.getPseudoClass("bottom"));
combo.valueProperty().addListener((obs, oldVal, newVal) -> {
if (oldVal != null)
rb.pseudoClassStateChanged(oldVal, false);
rb.pseudoClassStateChanged(newVal, true);
});
root.setTop(combo);
root.setCenter(rb);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
.radio-button:left > .text { -fx-rotate: 180; }
.radio-button:left { -fx-rotate:180; }
.radio-button:right > .text { -fx-rotate: 0; }
.radio-button:right { -fx-rotate:0; }
.radio-button:top > .text { -fx-rotate: 0; }
.radio-button:top { -fx-rotate:-90; }
.radio-button:bottom > .text { -fx-rotate: 0; }
.radio-button:bottom { -fx-rotate:90; }