我正在构建一个Javafx应用程序,其中有一个用于更改密码的按钮,为此,我需要加载新的Change-password FXML。它很简单,我知道但是我的问题是在每个按钮上单击一个新的Change-password窗口打开。还是有一种方法可以在更改密码窗口失去焦点时关闭它,即,如果我单击其他任何地方。
@FXML
private void CPassBtn(ActionEvent event) {
try
{
Parent part = FXMLLoader.load(getClass().getResource("/EmpSalary/ui/Mainwin/forChangePass.fxml"));
Stage stage = new Stage();
stage.setResizable(false);
stage.getIcons().add(new Image("/resources/letter.png"));
stage.setScene(new Scene(part));
stage.setTitle("Password change");
stage.show();
}
catch (IOException ex) {
Logger.getLogger(LoginuiController.class.getName()).log(Level.SEVERE, null, ex);
}
}
答案 0 :(得分:1)
并非您所描述的功能完全正确,但可能使用了模态(它可以阻止输入事件根据模态在所有者或应用程序上处理):
stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner(((Node) event.getSouce()).getScene().getWindow());
stage.show();
答案 1 :(得分:0)
您可以使用布尔值阻止多个窗口检查是否已加载
private boolean stageLoaded = false ;
@FXML private void CPassBtn(ActionEvent event) {
try
{
if(stageLoaded){ return;}
Parent part = FXMLLoader.load(getClass().getResource("/EmpSalary/ui/Mainwin/forChangePass.fxml"));
Stage stage = new Stage();
stage.setResizable(false);
stage.getIcons().add(new Image("/resources/letter.png"));
stage.setScene(new Scene(part));
stage.setTitle("Password change");
stage.show();
stageLoaded = true ;
}
catch (IOException ex) {
Logger.getLogger(LoginuiController.class.getName()).log(Level.SEVERE, null, ex);
}
}
但是如果您需要再次设置stageLoaded = false。您需要添加一个事件stage.setOnCloseRequest
private boolean stageLoaded = false ;
@FXML private void CPassBtn(ActionEvent event) {
try
{
if(stageLoaded){ return;}
Parent part =FXMLLoader.load(getClass().getResource("/EmpSalary/ui/Mainwin/forChangePa
ss.fxml"));
Stage stage = new Stage();
stage.setResizable(false);
stage.getIcons().add(new Image("/resources/letter.png"));
stage.setScene(new Scene(part));
stage.setTitle("Password change");
stage.show();
stageLoaded = true ;
stage.setOnCloseRequest((WindowEvent even) -> {
stageLoaded = false;
}
catch (IOException ex) {
Logger.getLogger(LoginuiController.class.getName()).log(Level.SEVERE,
null, ex);
}
}
答案 2 :(得分:0)
我认为这正是您想要的
stage.focusedProperty().addListener(new ChangeListener<Boolean>()
{
@Override
public void changed(ObservableValue<? extends Boolean> b, Boolean oldValue, Boolean newValue)
{
if(newValue == false)
stage.close();
}
});
答案 3 :(得分:0)
该解决方案检查焦点何时改变,如果平台失去焦点则关闭
public class Main extends Application {
public static void main(String[] args) { launch(args); }
@Override
public void start(Stage primaryStage) {
Button button = new Button("Password Window");
Scene scene = new Scene(button);
primaryStage.setScene(scene);
primaryStage.show();
button.setOnAction(event -> System.out.println(openPasswordWindow()));
}
private String openPasswordWindow(){
PasswordField passwordField = new PasswordField();
Stage stage = new Stage(StageStyle.UTILITY);
stage.setScene(new Scene(passwordField));
stage.show();
//You would need from here
stage.focusedProperty().addListener((ov, onHidden, onShown) -> {
if(!stage.isFocused())
Platform.runLater(() -> stage.close());
});
//Down to here
return passwordField.getText();
}
}
在您的代码中,它看起来像这样
@FXML
private void CPassBtn(ActionEvent event) {
try
{
Parent part = FXMLLoader.load(getClass().getResource("/EmpSalary/ui/Mainwin/forChangePass.fxml"));
Stage stage = new Stage();
stage.setResizable(false);
stage.getIcons().add(new Image("/resources/letter.png"));
stage.setScene(new Scene(part));
stage.setTitle("Password change");
stage.show();
stage.focusedProperty().addListener((ov, onHidden, onShown) -> {
if(!stage.isFocused())
Platform.runLater(() -> stage.close());
});
}
catch (IOException ex) {
Logger.getLogger(LoginuiController.class.getName()).log(Level.SEVERE, null, ex);
}
}