在 javafx 中使用单选按钮切换滑块 on/off
toggle sliders on/off with radio button in javafx
我想在按下一个单选按钮时关闭滑块,并在按下另一个按钮时打开它们:
无论如何,当我点击关闭按钮时,我希望标签、滑块和文本字段无法被 select 编辑。当您点击切换时,您可以再次 select 滑块等。
我知道我需要使用 ToggleGroup,但不确定我将如何关闭滑块。
我可能会为此 UI 使用单个 CheckBox 而不是多个单选按钮,然后您只需将滑块窗格的禁用 属性 绑定到选定的 属性 CheckBox,不过我这里只针对你显示的UI给出一个答案。
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ToggleSetup extends Application {
@Override
public void start(Stage stage) throws Exception {
RadioButton on = new RadioButton("on");
RadioButton off = new RadioButton("off");
ToggleGroup toggleState = new ToggleGroup();
on.setToggleGroup(toggleState);
off.setToggleGroup(toggleState);
toggleState.selectToggle(on);
VBox sliderPane = new VBox(
10,
new Slider(),
new Slider(),
new Slider()
);
sliderPane.disableProperty().bind(
Bindings.equal(off, toggleState.selectedToggleProperty())
);
VBox layout = new VBox(10, on, off, sliderPane);
layout.setPadding(new Insets(10));
stage.setScene(new Scene(layout));
stage.show();
}
public static void main(String[] args) { launch(args); }
}
我想在按下一个单选按钮时关闭滑块,并在按下另一个按钮时打开它们:
无论如何,当我点击关闭按钮时,我希望标签、滑块和文本字段无法被 select 编辑。当您点击切换时,您可以再次 select 滑块等。
我知道我需要使用 ToggleGroup,但不确定我将如何关闭滑块。
我可能会为此 UI 使用单个 CheckBox 而不是多个单选按钮,然后您只需将滑块窗格的禁用 属性 绑定到选定的 属性 CheckBox,不过我这里只针对你显示的UI给出一个答案。
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ToggleSetup extends Application {
@Override
public void start(Stage stage) throws Exception {
RadioButton on = new RadioButton("on");
RadioButton off = new RadioButton("off");
ToggleGroup toggleState = new ToggleGroup();
on.setToggleGroup(toggleState);
off.setToggleGroup(toggleState);
toggleState.selectToggle(on);
VBox sliderPane = new VBox(
10,
new Slider(),
new Slider(),
new Slider()
);
sliderPane.disableProperty().bind(
Bindings.equal(off, toggleState.selectedToggleProperty())
);
VBox layout = new VBox(10, on, off, sliderPane);
layout.setPadding(new Insets(10));
stage.setScene(new Scene(layout));
stage.show();
}
public static void main(String[] args) { launch(args); }
}