【问题标题】:How to close current stage when clicking a button to display new stage单击按钮以显示新阶段时如何关闭当前阶段
【发布时间】:2014-11-12 00:37:53
【问题描述】:

所以我正在开发一个密码管理应用程序,这是我第一次尝试使用 Java 制作 GUI。一旦用户单击登录按钮,应用程序将关闭登录窗口并仅显示主应用程序窗口。每当我尝试这样做时,我的 setPrevStage 方法都会存储一个 NULL 值,而 prevStage.close() 方法会导致程序因 NullPointerException 而崩溃。不知道我做错了什么,所以非常感谢任何建议。这是我用于此操作的 .java 文件。

    package pwmanager;

    import java.io.IOException;
    import java.net.URL;
    import java.util.ResourceBundle;
    import javafx.event.ActionEvent;
    import javafx.event.EventType;
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.fxml.Initializable;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.AnchorPane;
    import javafx.scene.layout.GridPane;
    import javafx.stage.Stage;


    /**
     *
     * @author 176878
     */
    public class FXMLDocumentController implements Initializable {

    @FXML
    private Button loginButton;


    @FXML
    Stage prevStage;
    Stage currentStage;
    public void setPrevStage(Stage stage){
        prevStage = stage;
        if (stage == null){
            System.out.println("Stage NULL");
        }

       }

    @FXML
    public void getPrevStage(Stage stage){
        currentStage = prevStage;

    }

    @FXML
    public void loginButtonAction(ActionEvent event) throws IOException {
       System.out.println("You clicked me, logging in!");
        setPrevStage(prevStage);

        Stage stage = new Stage();


        FXMLLoader loader = new FXMLLoader(getClass().getResource("MainScreen.fxml"));

        Parent mainScreen = (Parent)loader.load();
        Scene scene = new Scene(mainScreen);

        stage.setScene(scene);
        stage.setTitle("Password Manager");
        prevStage.close();
        stage.show();
    }    

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

}

【问题讨论】:

    标签: javafx stage scene


    【解决方案1】:

    我没有得到你的问题。但我给出了我的解决方案,这里的代码可能对你有帮助

    Stage st = (Stage) loginbutton.getScene().getWindow();
                st.hide();
                try 
                    {
                      showFxml(filex);
                    } 
                catch (Exception exp)
                    {
                      System.out.println("file loading problem "+exp);
                    }
    

    这里是 showFxml()

    public void showFxml(String filen)throws Exception
     {
       FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(""+filen+""));
       Parent root1 = (Parent) fxmlLoader.load();
       Stage stage = new Stage();
       stage.initModality(Modality.APPLICATION_MODAL);
       stage.setTitle("Password Manager");
       stage.setScene(new Scene(root1)); 
       stage.show();
     }
    

    通过使用它,您可以隐藏您之前的阶段并将当前阶段带到前面。

    【讨论】:

    • prevStage.close();我的程序行导致它因 NullPointerException 错误而崩溃,因此我的 getPrevStage() 方法由于某种原因没有将对象存储在 prevStage 变量中,因此当 prevStage.close();被调用,程序崩溃,因为没有要关闭的对象/阶段。我阅读了您的代码,但它并没有真正帮助我解决我的具体问题。
    【解决方案2】:

    如果在 prevStage 中有“旧”阶段,那么你试试看:

    @FXML
    public void loginButtonAction(ActionEvent event) throws IOException {
       System.out.println("You clicked me, logging in!");
    
        FXMLLoader loader = new FXMLLoader(getClass().getResource("MainScreen.fxml"));
    
        Parent mainScreen = (Parent)loader.load();
        Scene scene = new Scene(mainScreen);
    
        prevStage.setScene(scene);
        prevStage.setTitle("Password Manager");
        prevStage.show();
    }  
    

    如果不是...在您的 Start.class 中定义静态 Stage 阶段并在此设置您的 primarystage 阅读此示例:

    public class Start extends Application {
    
        static Stage stage;
    
        @Override
        public void start(Stage primarystage) {
            stage=primarystage;
    
            ...
    
             stage.show();
        }
    
    }
    

    现在的代码是这样的:

    @FXML
    public void loginButtonAction(ActionEvent event) throws IOException {
       System.out.println("You clicked me, logging in!");
    
        prevStage=Start.stage;   // Start.class
    
        FXMLLoader loader = new FXMLLoader(getClass().getResource("MainScreen.fxml"));
    
        Parent mainScreen = (Parent)loader.load();
        Scene scene = new Scene(mainScreen);
    
        prevStage.setScene(scene);
        prevStage.setTitle("Password Manager");
        prevStage.show();
    }    
    

    【讨论】:

      猜你喜欢
      • 2018-02-04
      • 2012-08-25
      • 1970-01-01
      • 1970-01-01
      • 2014-08-20
      • 2015-08-03
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      相关资源
      最近更新 更多