如何使用javafx中的on_off按钮禁用所有按钮?

时间:2016-10-05 04:17:18

标签: javafx scenebuilder

enter image description here

这是一个使用javaFx制作的简单计算器。我的问题是我想使用on_off按钮获得电源并使计算器无效。怎么办???提前谢谢。

2 个答案:

答案 0 :(得分:2)

单独使用SceneBuilder无法做到这一点,但可以通过自己编辑fxml来完成。只需使用ToggleButton开启/关闭按钮,然后将disable属性绑定到selected的{​​{1}}属性,或者在ToggleButton方法中执行此操作控制器(要求所有initialize通过Button注入控制器。

绑定在fxml

fx:id
控制器中的

绑定

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.*?>

<VBox xmlns:fx="http://javafx.com/fxml/1" spacing="10">
    <padding>
        <Insets left="10" right="10" bottom="10" top="10" />
    </padding>
    <children>
        <fx:define>
            <!-- create ToggleButton to be used with the disable properties -->
            <ToggleButton fx:id="on" text="On"/>
        </fx:define>
        <!-- create buttons and bind the disable property to the negated 
             selected property of the on button -->
        <Button text="Button 1" disable="${!on.selected}" />
        <Button text="Button 2" disable="${!on.selected}" />
        <Button text="Button 3" disable="${!on.selected}" />
        <Button text="Button 4" disable="${!on.selected}" />
        <Button text="Button 5" disable="${!on.selected}" />
        <!-- add on button to scene -->
        <fx:reference source="on"/>
    </children>
</VBox>

答案 1 :(得分:0)

为了快速做到这一点,我建议你添加所有按钮,除了打开/关闭一个容器,如hbox v盒或你想要的容器,然后禁用包含你的按钮的容器(父),例如

vBoxMain.getChildren().addAll(/*every button except on/off*/);
//or generate dynamically the buttons and add them to the vBoxMain in a for cycle
buttonOnOff.setOnAction((ActionEvent e) -> {

    if(vBoxMain.isDidable()){
        vBoxMain.setDisable(false);
    }else{
        vBoxMain.setDisable(true);
    }

});

这不是针对您的具体布局,而是为了给您一个想法,我希望它会对您有所帮助。