Ich brauche Hilfe mit diesem Java-Code. Ich habe drei Szenen Login-Szene, Admin-Szene und Player-Szene. Wenn ich das Programm ausführe und diesen Benutzer und das Passwort eingabe, sollte die zweite öffnen. Das Problem ist jetzt, dass sich die zweite Szene nicht öffnet. Es verbindet sich sogar mit der Datenbank, aber diese zweite Szene öffnet sich nicht. Ich habe den Code überprüft, ich sehe kein Problem damit. Kann mir bitte helfen, was passiert.JavaFX, FXML Zweite Szene öffnet nicht
Hier ist mein Hauptcode.
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);
}
}
Hier ist der Controller für mein Haupt:
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;
}
}
Dies ist, wo ich meine Bühne für meine Admin und Spielerszene.
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(); }
Dies ist, wo ich diese zwei Bühne in der GiantsLoginController nennen.
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) { } } } } }
Dies ist der Controller für meine Admin-Szene
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.
}
Bitte lernen, wie man einen Debugger verwenden. Ohne Ihren vollständigen Code/Setup zur Verfügung zu haben, ist es ziemlich schwer, Ihnen zu helfen. Sie könnten andererseits einfach durch den Code gehen, um das Problem zu identifizieren, was nicht eine Minute dauern würde. Die Verwendung von leeren Catch-Blöcken ist nie eine gute Idee, es sei denn, Ihr Code wird auch dann ordnungsgemäß funktionieren, wenn eine Ausnahme auftritt. Auch dann ist es in den meisten Fällen eine gute Idee, Informationen irgendwo zu protokollieren. Besonders in Ihrem Fall suchen Sie Fehler eine Ausnahme könnte wertvolle Informationen liefern.BTW: Versuchen Sie absichtlich, die Player- und Admin-Szene zu verwenden, wenn "Admin" ausgewählt ist? – fabian