【问题标题】:JavaFX, FXMLLoader.setController(controller) doesn't load sceneJavaFX,FXMLLoader.setController(controller) 不加载场景
【发布时间】:2016-11-25 22:05:45
【问题描述】:

在 setController 上时保持冻结状态。输入用户和密码时不加载场景

import java.io.IOException;
import java.sql.*;
import javafx.collections.*;
import javafx.event.Event;
import javafx.fxml.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class GiantsLoginController {

public String dataName, serverName, password;
public int num;

private Connection connect = null;
private Statement stmt = null;

private boolean userPass, connected;

private Connections connection;


@FXML
private ComboBox<String> sType;
@FXML
public TextField dbName;
@FXML
private TextField sName;
@FXML
private Button loginB;
@FXML
private PasswordField sPassword;
@FXML
private Pane paneL;
@FXML
private GridPane gPane;
@FXML
private ComboBox<String> uType;


ObservableList<String> sLists = FXCollections.observableArrayList("MySQL LOCAL", 
        "MYSQL REMOTE", "SQL SERVER LOCAL", "SQL SERVER");
ObservableList<String> uList = FXCollections.observableArrayList("Player", 
        "Admin");



@FXML
public void initialize() {
    sType.setItems(sLists);
    uType.setItems(uList);
}

@FXML
public void loginBClick (Event event) {
    if (isAllFieldFillup()) {

        switch(uType.getValue().trim()) {
            case "Admin":
                if (connectCheck()) {
                    try {

                          changeStage(GiantsLogin.getPrimaryStage());

                    }
                    catch (Exception e) {

                    }
                }
            case "Player": 
                if (connectCheck()) {
                    try {

                    }
                    catch (Exception e) {

                    }
                }
        }
    }
}

public void changeStage(Stage stage) throws IOException {
    GiantsAdminController controller = new GiantsAdminController("Hello World!");
    FXMLLoader loader = FXMLLoader.load(getClass().getResource("GiantsAdmin.fxml"));
    loader.setController(controller);
    stage.hide();
    stage.setScene(new Scene((Pane) loader.load()));
    stage.show();
}

public void closeConnection () {

    if (connect != null) {
        try {
            stmt.close();
            connect.close();
        }
        catch (SQLException e) {

        }
    }
}

public boolean connectCheck() {
    connected = false;

    dataName = dbName.getText();
    serverName = sName.getText();
    password = sPassword.getText();


    switch (sType.getValue()) {
        case "MySQL LOCAL":
            num = 1;
            break;
        case "MYSQL REMOTE":
            num = 2;
            break;
        case "SQL SERVER LOCAL":
            num = 3;
            break;
        case "SQL SERVER":
            num = 4;
            break;
        default:

    }

    if (connect == null) {
        connect = Connections.getconnect(num, dataName, serverName, password);
    }

    if (connect == null ) {
        System.out.println("Still no connection");
    }

    if (stmt == null) {
        try {
            stmt = connect.createStatement();
            connected = true;
        } catch (SQLException e) {
            Alert notify = new Alert(Alert.AlertType.INFORMATION);
            notify.setTitle("Blank filed");
            notify.setHeaderText(null);
            notify.setContentText("Incorrect login.");
            notify.showAndWait();

            connected = false;
        }


    }
    return connected;
}

private boolean isAllFieldFillup() {
    boolean allInfo;
    if (sType.getValue().equals("server type") && dbName.getText().isEmpty()
            && sName.getText().isEmpty() && sPassword.getText().isEmpty()) {
        Alert notify = new Alert(Alert.AlertType.INFORMATION);
        notify.setTitle("Blank filed");
        notify.setHeaderText(null);
        notify.setContentText("You are missing some information.");
        notify.showAndWait();

        allInfo = false;
    }
    else {
        allInfo = true;
    }
    return allInfo;
}

}

这是我打开一个新场景并设置我的控制器的地方,这样我就可以 能够在新场景中访问它。但是当我放的时候它就冻结了 用户名和密码,然后单击登录。

public void changeStage(Stage stage) throws IOException {
    GiantsAdminController controller = new GiantsAdminController("Hello World!");
    FXMLLoader loader = FXMLLoader.load(getClass().getResource("GiantsAdmin.fxml"));
    loader.setController(controller);
    stage.hide();
    stage.setScene(new Scene((Pane) loader.load()));
    stage.show();
}

这是我调用changeStage方法的地方

public void loginBClick (Event event) {
    if (isAllFieldFillup()) {

        switch(uType.getValue().trim()) {
            case "Admin":
                if (connectCheck()) {
                    try {

                          changeStage(GiantsLogin.getPrimaryStage());

                    }
                    catch (Exception e) {

                    }
                }
            case "Player": 
                if (connectCheck()) {
                    try {

                    }
                    catch (Exception e) {

                    }
                }
        }
    }
}

这是我的主要课程

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.*;

public class GiantsLogin extends Application {

private static Stage stage;

@Override
public void start(Stage stage) throws IOException {
    setPrimaryStage(stage);
    Parent root = FXMLLoader.load(getClass().getResource("GiantsLogin.fxml"));
    Scene scene = new Scene(root);
    scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
    stage.setScene(scene);
    stage.setTitle("Giants Login");
    stage.show();
}

public static void setPrimaryStage(Stage primaryStage) {
    stage = primaryStage;
}

public static Stage getPrimaryStage() {
    return stage;
}
public static void main(String[] args) {
    launch(args);
}

}

这是我的第二个窗口:这是应该打开的窗口 当用户名和密码正确时。

import java.io.IOException;
import java.sql.Statement;
import javafx.collections.*;
import javafx.event.Event;
import javafx.fxml.*;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;

public class GiantsAdminController {
@FXML
private Button connect = null;
private boolean connected;

private Statement stmt;

@FXML
private TextField aRank;

public GiantsAdminController(String message) {
    System.out.println("You said: " + message);
}

public GiantsAdminController() {
}

ObservableList<String> sLists = FXCollections.observableArrayList("MySQL LOCAL", 
        "MYSQL REMOTE", "SQL SERVER LOCAL", "SQL SERVER");
@FXML
public void initialize() {
    serverType.setItems(sLists);
}

@FXML
public void clearBClick (Event event) {
    aRank.clear();
    aName.clear();
    aPosition.clear();
    aSchool.clear();
    aAge.clear();
    aWar.clear();
}


@FXML
public void SingOutClick(Event event) throws IOException {

    Parent giantsLogin = FXMLLoader.load(getClass().getResource("/giants/GiantsLogin.fxml"));

    Scene gLScene = new Scene(giantsLogin);
    gLScene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
    stage.setScene(gLScene); 
    stage.show();
}
 }

我已经尽一切努力解决这个问题,但没有运气。

【问题讨论】:

    标签: javafx fxml netbeans-8


    【解决方案1】:

    我认为问题在于在 FXMLLoader 上使用了静态加载功能。 试试这个修改(未测试)。

    public void changeStage(Stage stage) throws IOException {
        GiantsAdminController controller = new GiantsAdminController("Hello World!");
        FXMLLoader loader = new FXMLLoader(getClass().getResource("GiantsAdmin.fxml"));
        loader.setController(controller);
        stage.hide();
        stage.setScene(new Scene((Pane) loader.load()));
        stage.show();
    }
    

    【讨论】:

    • 我就是这么做的。现在它不再冻结了。但是第二个场景没有打开
    • 顺便说一句,我注意到当用户是 palyer 时你没有调用 changeStage() 方法
    • 没错。我只想确保它首先适用于管理员。胸围仍然没有得到第二个场景。请你再看一遍,看看为什么烤饼场景没有打开。请
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 2015-05-10
    • 2014-04-05
    • 1970-01-01
    相关资源
    最近更新 更多