如何使RadioButton看起来像JavaFX中的常规Button

我正在实现一个类似工具箱的窗格,因此用户一次只能选择一个工具,并且我从Button切换到RadioButton的行为。

但是我发现RadioButton使用自己带有点的皮肤,但是我仍然希望它像普通的Button一样显示。 我是JavaFX和FXML的初学者,所以任何人都知道如何实现这一目标?

首先创建一个单选按钮,删除radio-button样式,然后添加toggle-button样式

 RadioButton radioButton=new RadioButton("Radio"); radioButton.getStyleClass().remove("radio-button"); radioButton.getStyleClass().add("toggle-button"); 

希望能解决你的问题

最后我走了另一条路。 您可以扩展ToggleButton ,使其行为类似于RadioButton 。 这没有消耗鼠标释放的奇怪的点击效果。

 public class RadioToggleButton extends ToggleButton { // As in RadioButton. /** * Toggles the state of the radio button if and only if the RadioButton * has not already selected or is not part of a {@link ToggleGroup}. */ @Override public void fire() { // we don't toggle from selected to not selected if part of a group if (getToggleGroup() == null || !isSelected()) { super.fire(); } } } 

然后在FXML中:

      

它完成了。

这是你可以用来摆脱点的风格

 .radio-button > .radio { -fx-background-color: transparent; -fx-background-insets: 0; -fx-background-radius: 0px; -fx-padding: 0.0em; /* 4 8 4 8 */ } .radio-button:selected > .radio > .dot { -fx-background-color: transparent; } 

在这里记录; 万一有人发现它有用。

您可以使用ToggleButtonToggleGroup ,但必须创建ToggleGroup的扩展名以保持选定的一个。 这是一个例子 。