所以我有一个登录页面。如果您单击“不是用户?立即注册!”它应该在同一窗口中打开一个新页面。现在,我要打开第二页,但它在单独的窗口中。在打开注册页面时还会创建一条错误消息。这是代码:
主要
public class Main extends Application {
private ConnectToDB DB;
@FXML
GridPane mainPane;
@FXML
Hyperlink RegisterHyperLink;
@Override
public void start(final Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 400, 375));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
控制器
public class Controller {
private Stage stage = new Stage();
private GridPane pane = new GridPane();
private boolean passwordsCheck = false;
@FXML TextField password;
@FXML TextField passwordConfirm;
@FXML GridPane mainPane;
@FXML GridPane registerPage;
public void handleSubmitButtonAction(ActionEvent actionEvent) {
}
public void goToRegisterNewUserPage(ActionEvent e) throws Exception{
RegisterPage page = new RegisterPage(stage);
page.getScene().getWindow().hide();
// this is where the error gets thrown up
}
注册页面
public class RegisterPage extends GridPane {
@FXML GridPane mainPane;
@FXML GridPane RegisterUsers;
public RegisterPage(Stage stage) throws Exception{
FXMLLoader loader = new FXMLLoader();
GridPane RegisterUsers = loader.load(getClass().getClassLoader().getResource("RegisterUsers.fxml"));
stage.setScene(new Scene(RegisterUsers, 500, 475));
stage.show();
}
}
用于登录页面的sample.fxml以及相关的超链接
<GridPane fx:id="mainPanes"
fx:controller="sample.Controller"
xmlns:fx="http://javafx.com/fxml"
alignment="center" hgap="10" vgap="10">
<Hyperlink text="Forgot password?"
onAction="#goToForgotPasswordPage"/>
RegisterUsers.fxml用于注册用户页面。我不确定是否需要此功能,但其fx:id = registerPage。因此,基本上,单击超链接应该在同一窗口而不是第二个窗口中打开“注册用户”。如果自动调整大小会很棒,但如果没有自动调整大小,那我以后可以做。我怀疑新窗口与page.getScene().getWindow().hide();
有关。我在控制器中尝试了很多不同的方法。我之前的尝试是从sample.fxml文件中调用GridPane mainPane,然后是mainPane.getChildren().setAll(page);
,但这也没有用。感谢您的关注!
答案 0 :(得分:2)
可能有很多方法可以完成此操作,但是我已经完成了一个简单的示例。本质上,您将有一个MainLayout
,其中仅包含一个VBox
。这是我们用于主窗口的contentPane
。
在VBox
内,我们将加载LoginLayout.fxml
。单击Register
按钮后,我们将加载RegisterLayout.fxml
并替换contentPane
的子代。
下面是一个完整的示例,您可以复制该示例以查看实际效果:
Main.java:
package loginExample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/loginExample/MainLayout.fxml"));
loader.setController(new MainController());
primaryStage.setTitle("Login Example");
primaryStage.setWidth(300);
primaryStage.setHeight(200);
primaryStage.setScene(new Scene(loader.load()));
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
MainController.java:
package loginExample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import java.io.IOException;
public class MainController {
@FXML
private VBox contentPane;
@FXML
private void initialize() {
// Initially start with the login layout
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/loginExample/LoginLayout.fxml"));
// Set the LoginController and pass a reference to the MainController. This allows the LoginController
// to access our contentPane.
loader.setController(new LoginController(this));
// Now, load the login layout into our contentPane
GridPane gridPane = loader.load();
contentPane.getChildren().add(gridPane);
} catch (IOException e) {
e.printStackTrace();
}
}
public VBox getContentPane() {
return contentPane;
}
}
MainLayout.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.VBox?>
<VBox fx:id="contentPane" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
</padding>
</VBox>
LoginController.java
package loginExample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import java.io.IOException;
public class LoginController {
@FXML
private Button btnRegister;
// Reference to our main controller so we can access its content
private MainController mainController;
public LoginController(MainController mainController) {
this.mainController = mainController;
}
@FXML
private void initialize() {
// Set our Register button to change the content of the main pane
btnRegister.setOnAction(event -> {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/loginExample/RegisterLayout.fxml"));
loader.setController(new RegisterController(mainController));
// Set our RegisterLayout as the new content for our MainLayout window
mainController.getContentPane().getChildren().clear();
mainController.getContentPane().getChildren().add(loader.load());
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
LoginLayout.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<GridPane hgap="10.0" vgap="5.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1">
<columnConstraints>
<ColumnConstraints halignment="RIGHT" hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
<ColumnConstraints hgrow="SOMETIMES" minWidth="-Infinity"/>
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="-Infinity" vgrow="NEVER"/>
<RowConstraints minHeight="-Infinity" vgrow="SOMETIMES"/>
<RowConstraints minHeight="-Infinity" vgrow="SOMETIMES"/>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
</rowConstraints>
<children>
<HBox alignment="CENTER" GridPane.columnSpan="2">
<children>
<Label style="-fx-font-size: 150%; -fx-font-weight: bold;" text="Please login below:"/>
</children>
</HBox>
<Label text="Username:" GridPane.rowIndex="1"/>
<Label text="Password:" GridPane.rowIndex="2"/>
<TextField GridPane.columnIndex="1" GridPane.rowIndex="1"/>
<HBox alignment="CENTER_RIGHT" spacing="10.0" GridPane.columnSpan="2" GridPane.rowIndex="3">
<children>
<Button fx:id="btnRegister" mnemonicParsing="false" text="Register"/>
<Button defaultButton="true" mnemonicParsing="false" text="Login"/>
</children>
</HBox>
<PasswordField GridPane.columnIndex="1" GridPane.rowIndex="2"/>
</children>
</GridPane>
RegisterController.java:
package loginExample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import java.io.IOException;
public class RegisterController {
@FXML
private Button btnCancel;
private MainController mainController;
public RegisterController(MainController mainController) {
this.mainController = mainController;
}
@FXML
private void initialize() {
btnCancel.setOnAction(event -> {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/loginExample/LoginLayout.fxml"));
loader.setController(new LoginController(mainController));
// Set our RegisterLayout as the new content for our MainLayout window
mainController.getContentPane().getChildren().clear();
mainController.getContentPane().getChildren().add(loader.load());
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
RegisterLayout.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<GridPane hgap="10.0" vgap="5.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/9.0.1">
<columnConstraints>
<ColumnConstraints halignment="RIGHT" hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
<ColumnConstraints hgrow="SOMETIMES" minWidth="-Infinity"/>
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="-Infinity" vgrow="NEVER"/>
<RowConstraints minHeight="-Infinity" vgrow="SOMETIMES"/>
<RowConstraints minHeight="-Infinity" vgrow="SOMETIMES"/>
<RowConstraints minHeight="-Infinity" vgrow="SOMETIMES"/>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
</rowConstraints>
<children>
<HBox alignment="CENTER" GridPane.columnSpan="2">
<children>
<Label style="-fx-font-size: 150%; -fx-font-weight: bold;" text="Fill Form to Register:"/>
</children>
</HBox>
<Label text="Username:" GridPane.rowIndex="1"/>
<Label text="Password:" GridPane.rowIndex="2"/>
<Label text="Re-enter Password:" GridPane.rowIndex="3"/>
<TextField GridPane.columnIndex="1" GridPane.rowIndex="1"/>
<HBox alignment="CENTER_RIGHT" spacing="10.0" GridPane.columnSpan="2" GridPane.rowIndex="4">
<children>
<Button fx:id="btnCancel" cancelButton="true" mnemonicParsing="false" text="Cancel"/>
<Button defaultButton="true" mnemonicParsing="false" text="Register"/>
</children>
</HBox>
<PasswordField GridPane.columnIndex="1" GridPane.rowIndex="2"/>
<PasswordField GridPane.columnIndex="1" GridPane.rowIndex="3"/>
</children>
</GridPane>
结果:
这是一个非常简单的示例,旨在演示该概念;您可能希望以不同的方式构建现实世界项目。