[英]How make a button pressed when corresponding key on a keyboard is pressed? 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.