gpt4 book ai didi

java - 在同一窗口中打开另一个 GridPane

转载 作者:行者123 更新时间:2023-12-02 10:07:10 25 4
gpt4 key购买 nike

所以我有一个登录页面。如果您点击“还不是用户?立即注册!”它应该在同一窗口中打开一个新页面。现在我正在打开第二页,但它位于单独的窗口中。它还会在打开注册页面时创建错误消息。这是代码:

主要

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

Controller

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(); 相关。我在 Controller 中尝试了很多不同的东西。我之前尝试从sample.fxml 文件调用GridPane mainPane,然后调用mainPane.getChildren().setAll(page); ,但这也不起作用。感谢您的浏览!

最佳答案

可能有很多方法可以实现这一点,但我举了一个简单的例子。本质上,您将拥有一个仅包含一个 VBoxMainLayout。这是主窗口的 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>
<小时/>

结果:

screenshot 1 screenshot2

这是一个非常简单的例子,旨在演示这个概念;您可能希望以稍微不同的方式构建您的现实世界项目。

关于java - 在同一窗口中打开另一个 GridPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55272371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com