【问题标题】:JavaFX opening new screens everytimeJavaFX 每次打开新屏幕
【发布时间】:2017-06-13 10:43:41
【问题描述】:

我正在尝试创建一个基本的登录应用程序,在输入凭据后,用户必须被带到一个新屏幕。

但是当我运行我的应用程序并输入凭据时,它会进入一个新屏幕,但我可以看到这是一个新窗口,而旧窗口仍在后台打开。

下面是我的代码:

package com.gsx.controller;

import java.io.IOException; 
import java.util.logging.Logger;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import application.Main;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class LoginController {

private Scene scene;

@FXML
private TextField databaseUserName;

@FXML
private TextField databasePassword;

@FXML
private AnchorPane mainPageAnchorPane;

private static SessionFactory sessionFactory = null;


//Controller method to handle the click of the Database Login button
//This method needs to try and setup a database connection based on the credentials provided
//and advance to the next stage if it is successfull in establishing the connection
//If the credentials entered are incorrect an error message must be displayed and the 
//control must remain in the same page.
@FXML
public void handleLoginButtonClick(ActionEvent actionEvent){



    Session session = null;

    try{

    Configuration configuration = new Configuration();
    //configuration.configure("applicationConfig.xml");
    configuration.configure("applicationConfig.xml");
    configuration.setProperty("hibernate.connection.username",databaseUserName.getText());
    configuration.setProperty("hibernate.connection.password",databasePassword.getText());
    System.out.println("Configuration was successful");
    System.out.println(configuration.getProperty("connection.username"));
    System.out.println(configuration.getProperty("hibernate.connection.password"));
    //Create the session factory
    sessionFactory = configuration.buildSessionFactory();

    //Get the session object
    session = sessionFactory.getCurrentSession();



        //Begin the transaction
        Transaction transaction = session.beginTransaction();

        System.out.println("Connection successful");
        //some problem here debug this tomorrow

        AnchorPane root = new AnchorPane();
        Scene scene = new Scene(root,650,800);
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/com/gsx/frontend/mainPage.fxml"));
        System.out.println(fxmlLoader);
        scene.setRoot((Parent)fxmlLoader.load());
        Main.stage.setScene(scene);
        Main.stage.show();
    }
    catch(Exception exception){

        exception.printStackTrace();
        Alert alert = new Alert(AlertType.ERROR, "The login information is incorrect",ButtonType.OK);
        alert.setTitle("Invalid Cred!!");
        alert.show();
        if(alert.getResult() == ButtonType.OK){

        }
    }
    finally{
        System.out.println("Finally Block to take care of closing the session and the session Factory");
        if(null!=session){
            System.out.println("Session is not Null. Closing it");
            session.close();
        }

    }



}



//Method to show the Login Screen
public void showLoginScreen(){
    try{
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("login.fxml"));
        scene.setRoot((Parent)fxmlLoader.load());
        }
    catch(IOException exception){
        Logger.getLogger(LoginController.class.getName());
    }
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}

}

你能帮我解决我做错了什么吗?

【问题讨论】:

  • 请创建一个minimal reproducible example 并将其发布在问题中,而不是链接到代码。如果链接因任何原因失效,则该问题对其他用户毫无用处。
  • @James_D - 我添加了评论,您可以删除否决票吗?
  • 不过,以后请发帖minimal reproducible example。您发布的内容不是最少的(它包含各种不相关的代码,例如与您所询问的内容无关的数据库连接)并且不完整(无法复制、编译和按原样运行)。
  • @James_D 当然还在学习。一定会记住这一点。谢谢:)

标签: javafx scene stage fxmlloader


【解决方案1】:

好吧,你做错了: 在您的登录控制器中,您创建一个完整的 Stage 类新实例。当您创建新阶段时,它会显示在新窗口中,因此您有两种选择。在加载新阶段时关闭旧阶段,或者您应该访问主阶段并设置场景。

要访问阶段,您可以使用以下命令,这将访问您的登录阶段,但您的主要阶段可以使用相同的逻辑:

databaseUserName.getScene.getWindow

现在你可以使用它来调用 .hide() 方法来关闭舞台,或者你可以在那个舞台上设置新的场景。

别忘了你总是可以施放这个

databaseUserName.getScene.getWindow

到(阶段),如果你需要,也不要尝试从初始化中访问它,因为它会返回 null。

最后,如果你打算制作包含大量场景的应用程序,我建议你使用一些框架,如 afterburnerfx 或任何其他框架。

最后,您可以将您的 primaryStage 设为静态,但我强烈建议您不要这样做

我是通过手机写的,如有错误,请见谅。如果您需要任何进一步的帮助,请随时询问。

【讨论】:

  • 是的,我决定在整个申请过程中使用我的初级阶段。
【解决方案2】:

你可以在java的LoginController中使用下面的代码来关闭之前的winodw。stage.show();之后使用下面的行

mainPageAnchorPane.getScene().getWindow().hide();

您之前的窗口将关闭。您必须使用要关闭的该窗口的锚窗格对象。

【讨论】:

    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 2022-10-22
    • 2023-01-06
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多