【问题标题】:How to make a button be pressed only once in javafx如何在javafx中只按下一次按钮
【发布时间】:2015-04-15 14:46:54
【问题描述】:

所以我在主菜单中有四个按钮,我希望用户一次只能按一个按钮。原因是我的几个按钮转到另一个屏幕。如果他们多次按下该按钮,则屏幕将加载两次或第 n 次,我不希望这样。因此,一个廉价的修复方法是,一旦用户单击这些按钮中的任何一个,就将所有按钮设置为禁用,然后在它们返回屏幕后重新启用。我可以使用单选按钮,但屏幕的设计将被破坏。有什么想法吗?

【问题讨论】:

  • 可能只是使用属性?在你的控制器中创建一个新属性,当所有按钮都应该被​​禁用时,总是改变它的状态。然后将每个按钮的 disableProperty 绑定到您的属性的状态。因此,当您在单击一个按钮时更改属性状态时,所有按钮始终处于禁用状态。
  • 我试图通过将布尔属性绑定到按钮的禁用属性来禁用一个按钮,例如 isDisabled = new SimpleBooleanProperty(); optionButton.disableProperty().bindBidirectional(isDisabled);然后当他们单击此按钮时,我执行 isDisabled.set(false)。这不起作用,但为什么?
  • @NDY 谢谢你的建议

标签: java button javafx


【解决方案1】:

这是一个简单的答案。

isDisabled = new SimpleBooleanProperty();
backToMain.disableProperty().bind(isDisabled);

然后我用了

isDisabled.setValue(true); // in my eventhandler method

它成功了。

如果有人有更好的答案,请发布。

【讨论】:

    【解决方案2】:

    您可以使用Segmented button of Controls FX 并在其上实施您的廉价想法。

    【讨论】:

      【解决方案3】:

      在我看来,它更简单。事实上你有:

          @FXML
          private Button btn;
      

      然后,如果您希望某个事件发生并且按钮冻结本身,您可以输入:

      btn.setDisable(true);
      

      一旦事件发生,按钮冻结

      【讨论】:

        【解决方案4】:

        我不确定您是如何创建新窗口的,但我使用此方法只允许打开一个窗口实例。

        在类中声明你的变量:

        private Stage settingsStage = new Stage();
        

        设置你的方法:

        @FXML // this method opens the settings window
        void OpenSettings(ActionEvent event) throws IOException {
            // if the stage is already open, exit the method
            if (settingsStage.isShowing()) {
                return;
            }       
            // set up the source controller for the new window
            // FXMLLoader fxmlLoader = new
            // FXMLLoader(getClass().getResource("Settings.fxml"));
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Login.fxml"));
            // load the controls for the window
            Parent settings = (Parent) fxmlLoader.load();
            // make the window always on top of open windows
            settingsStage.setAlwaysOnTop(true);
            // set the title for the window
            settingsStage.setTitle("Login");
            // set the icon for the window
            settingsStage.getIcons().add(new Image("application/images/Lead.png"));
            // set the scene for the stage
            settingsStage.setScene(new Scene(settings));
            settings.getStylesheets().add(getClass().getResource("login.css").toExternalForm());
            // finally display the window to the user
            settingsStage.show();
        } 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-05-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-26
          • 2017-09-09
          相关资源
          最近更新 更多