【发布时间】:2016-12-18 02:08:39
【问题描述】:
大家好,今天又遇到一个问题。 这是我的代码的一部分。我有 2 个功能要分配给一个按钮(登录),我该怎么做?
@FXML
private void fireLogIn()
{
LogInButton.setOnKeyPressed(event -> {
if(event.getCode() == KeyCode.ENTER){
LogIn(event); // <--- there is an error of wrong type of data
}
});
}
@FXML
private void LogIn(ActionEvent event) throws IOException {
if(LoginField.getText().equals("MKARK")&&PasswdField.getText().equals("KACZOR1"))
{
Parent parent = FXMLLoader.load(getClass().getResource("/fxmlFiles/MainScreen.fxml"));
Scene MainScene = new Scene(parent);
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
stage.setScene(MainScene);
stage.show();
}
else
{
IncorrectDataLink.setVisible(true);
IncorrectDataLink.setOnAction(e-> openWebpage(uri));
}
}
我想同时提供这两种功能,用鼠标按下按钮,或者当它专注于它时按下“ENTER”按钮。
【问题讨论】:
-
你为什么要把
event给Login作为参数?! -
因为对我要求很高,我猜是“private void LogIn(ActionEvent event)”
-
通常一个动作事件也是用回车键触发的,那么为什么还要创建2个事件处理程序呢?