繁体   English   中英

如何使按钮在JavaFX中仅被按下一次

[英]How to make a button be pressed only once in javafx

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

这是一个简单的答案。

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

然后我用

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

而且有效。

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

您可以使用Controls FX的“分段”按钮,并在其上实现便宜的想法。

我认为这很简单。 实际上,您有:

    @FXML
    private Button btn;

然后,在您希望事件发生并且按钮冻结的地方 ,您放置:

btn.setDisable(true);

事件一旦发生,按钮就会冻结

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

在类中声明您的变量:

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();
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM