当前位置: 首页 > 知识库问答 >
问题:

JavaFX、FXML第二场景未打开

高和通
2023-03-14

我需要这段java代码的帮助。我有三个场景登录场景,管理场景,和玩家场景。当我运行程序并输入用户和密码时,第二个应该打开。现在的问题是,第二个场景没有打开。它甚至连接到数据库,但第二个场景没有打开。我已经检查了代码,我看不出有什么问题。能不能请一些帮帮我,发生了什么事。

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

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.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
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 {

                          admindStage(GiantsLogin.getPrimaryStage());

                    }
                    catch (Exception e) {

                    }
                }
            case "Player": 
                if (connectCheck()) {
                    try {
                        playerStage(GiantsLogin.getPrimaryStage());

                    }
                    catch (Exception e) {

                    }
                }
        }
    }
}

public void admindStage(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();
}

public void playerStage(Stage stage) throws IOException {
    GiantsAdminController controller = new GiantsAdminController("Hello World!");
    FXMLLoader loader = new FXMLLoader(getClass().getResource("GiantsPlayer.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 admindStage(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();
}

public void playerStage(Stage stage) throws IOException {
    GiantsAdminController controller = new GiantsAdminController("Hello World!");
    FXMLLoader loader = new FXMLLoader(getClass().getResource("GiantsPlayer.fxml"));
    loader.setController(controller);
    stage.hide();
    stage.setScene(new Scene((Pane) loader.load()));
    stage.show();
}
 public void loginBClick (Event event) {
    if (isAllFieldFillup()) {

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

                          admindStage(GiantsLogin.getPrimaryStage());

                    }
                    catch (Exception e) {

                    }
                }
            case "Player": 
                if (connectCheck()) {
                    try {
                        playerStage(GiantsLogin.getPrimaryStage());

                    }
                    catch (Exception e) {

                    }
                }
        }
    }
}
import java.io.IOException;
import java.net.URL;
import java.sql.Statement;
import java.util.ResourceBundle;
import javafx.collections.*;
import javafx.event.Event;
import javafx.fxml.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.stage.Stage;

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

private Statement stmt;

@FXML
private TextField aRank;
@FXML
private TextField aName;
@FXML
private TextField aPosition;
@FXML
private TextField aSchool;
@FXML
private TextField aAge;
@FXML
private TextField aWar;
@FXML
private Button clearB;
@FXML
private Button addB;
@FXML
private TableColumn<?, ?> rank;
@FXML
private TableColumn<?, ?> name;
@FXML
private TableColumn<?, ?> position;
@FXML
private TableColumn<?, ?> school;
@FXML
private TableColumn<?, ?> age;
@FXML
private TableColumn<?, ?> war;
@FXML
private TextField qSearch;
@FXML
private Button search;
@FXML
private Button singout;
@FXML
private Button delete;
@FXML
private ComboBox<String> serverType;
@FXML
private TextField dbName;
@FXML
private TextField serverName;
@FXML
private TextField sPassword;

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();
}

@Override
public void initialize(URL location, ResourceBundle resources) {//To change body of generated methods, choose Tools | Templates.
}

共有1个答案

佘单鹗
2023-03-14

以下只是一个命题!。使用AdmindStage()方法,执行以下修改

 public void admindStage(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();
}

或使用多级:

public void admindStage() throws IOException {
    GiantsAdminController controller = new GiantsAdminController("Hello World!");
    FXMLLoader loader = new FXMLLoader (getClass().getResource("GiantsAdmin.fxml"));
    loader.setController(controller);
    Stage stage = new Stage();
    stage.setScene(new Scene((Pane) loader.load()));
    stage.show();
}
 类似资料:
  • 我写信是为了获取有关JavaFX的一些信息。我使用SceneBuilder设计了一个应用程序,它有一个主要场景,由几个文本字段和用于浏览文件的按钮组成。程序运行后还有一个用于输出的文本区域。我在控制器的初始化方法中添加了更改侦听器,以便在更新输出文本区域时修改日志文件。 最初,我使用输出文本区域来显示日志文件,对其进行了测试。一旦我从主文件菜单中选择了“查看日志”菜单项,我就能够在这个输出文本区域

  • 当我运行该程序时,它加载第一个和第二个fxml,但继续显示第一个fxml(当它应该显示第二个时)。我可以使用相同代码的输入(即按钮)从第一个屏幕到第二个屏幕。 我尝试使用不同的加载器,每次设置场景时都使用。show()方法,但这些(或它们的许多变体)都不起作用。 我在调用方法时进行打印--这就是我确定它们被加载的方式(printline): null 相反,输出(在调试中,如果stage.show

  • 请用简单的语言回答以上问题。如果可能的话,为我提供一些如何使用JavaFX的好教程。

  • 有人知道如何将JavaFX场景保存到可以由JavaFX FXMLLoader加载的FXML文件中吗? SceneBuilder允许这样做,但如果我手动构建场景,我如何将其保存到FXML文件中?

  • 我正在设计一款跳棋游戏,一旦游戏结束,一个新窗口就会打开,你可以开始一个新游戏,也可以退出。我对JavaFX了解不多,我一直在努力让我的代码正常工作,但我没有成功。。。 我有一个困难,我的电路板没有显示为fxml文件,但它是用Java代码创建的,并放在我的边框窗格的中心。边框窗格的顶部是fxml文件。然后,游戏结束后的另一个窗口也是另一个fxml文件: 这个fxml文件的Controller类如下

  • 问题内容: 我有一个使用javafx Scene来渲染某些东西的应用程序,并且我想将该渲染结果放入我在Javafx中创建的某些GUI中。我该怎么做? 基本上,有一些容器可以放入场景,然后将其放入GUI。 抱歉,如果是新手问题,我是JavaFX的新手 问题答案: 该场景只有一个顶级父节点作为根。您可以获取它并放入另一个场景。