JavaFX按钮不是先响应第二次点击 [英] JavaFX button reacting on second click not first
问题描述
我正在尝试在JavaFX中创建我的第一个应用程序,我遇到了一个调用方法的Button问题(例如打开另一个窗口) - 我总是要点击它两次才能触发操作。
I am trying to create my very first application in JavaFX and I have a problem with Button that calls a method (for example to open another window) - I always have to click it twice in order to trigger action.
这是来自Controller的代码:
Here's my code from the Controller:
import javafx.fxml.FXML;
import javafx.scene.control.Button;
public class ControllerSignIn {
@FXML
private Button forgot;
@FXML
private Button back;
@FXML
private Button signin;
public void forgetPasswordClicked() {
forgot.setOnAction(e -> ForgotPassword.setUpWindow()); //works on 2nd click
}
public void backClicked() {
back.setOnAction(e -> ForgotPassword.closeWindow()); //works on 2nd click
}
public void signInClicked() {
System.out.println("Sign In CLICKED"); //works on first click
}
}
我的方法在这里实现:
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Modality;
import javafx.stage.Stage;
import java.io.IOException;
public class ForgotPassword {
static Stage window;
static Scene scene;
static Parent root;
private static void loadFXML() {
try {
root = FXMLLoader.load(ForgotPassword.class.getResource("ForgotPassword.fxml"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void setUpWindow() {
loadFXML();
scene = new Scene(root);
scene.getStylesheets().add("signin/SignIn.css");
window = new Stage();
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle("Forgot Password?");
window.setScene(scene);
window.showAndWait();
}
public static void closeWindow() {
window.close();
}
}
推荐答案
最可能你的FXML中有以下内容:
Most likely you have the following in your FXML:
<Button fx:id="forgot" onAction="#forgetPasswordClicked" />
这使您的按钮忘记
调用您的方法 forgetPasswordClicked()
。但是,当您单击按钮时,不是定义要执行的逻辑,而是第一次说:单击此按钮时,在我的按钮上放置一个动作事件
call setUpWindow()
This makes your button forgot
call your method forgetPasswordClicked()
. But instead of defining your logic to be executed when your button is clicked, the first time you say: "When this button is clicked, place an action event
on my button which will call setUpWindow()
"
forgot.setOnAction(e -> ForgotPassword.setUpWindow());
因此,首次点击设置按钮的逻辑。第二次点击,实际执行它。要解决此问题,请立即使用您的逻辑:
Therefore, your first click "sets up" the logic of your button. The second click, actually executes it. To solve this, either immediately use your logic as such:
public void forgetPasswordClicked() {
ForgotPassword.setUpWindow();
}
或者不定义fxml中要调用的方法,并移动按钮的初始化(设置动作监听器)如下所示:
or don't define the method to be called in your fxml, and move the initialization of your button (setting the action listener) to your initialization as following:
public class ControllerSignIn implements Initializable {
@FXML
private Button forgot;
@FXML
private Button back;
@Override
public void initialize(URL location, ResourceBundle resources) {
forgot.setOnAction(e -> ForgotPassword.setUpWindow());
back.setOnAction(e -> ForgotPassword.closeWindow());
}
}
这也是你的原因signInClicked()
方法从第一次单击开始工作,因为它实际上执行逻辑而不是首先设置处理程序。
This is also why your signInClicked()
method works from the first click, because it actually executes the logic instead of setting up the handler first.
这篇关于JavaFX按钮不是先响应第二次点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!