我正在设计一个使用BorderPane作为Rootlayout的Javafx应用程序,该应用程序在边框窗格的左侧有一个按钮。在单击时,我需要将borderpane(rootlayout)中心设置为其他addprodct.fxml
。我想在BorderPane中心内单击“插入”按钮上添加一个vbox。
我检查了我的fxml是否正确加载
System.out.println("view folder: " + RootController.class.getResource("/Views/AddProduct.fxml"));
打印到fxml的确切路径。我尝试使用新的fxml设置舞台,以查看它是否可以正确加载并正常工作。
FXMLLoader loader = new FXMLLoader();
System.out.println("view folder: " + RootController.class.getResource("/Views/AddProduct.fxml"));
loader.setLocation(RootController.class.getResource( "/Views/AddProduct.fxml"));
tryvbox = loader.load();
main.getPrimaryStage().setScene(new Scene(tryvbox));
是的,我知道已经有人问过同样的问题,但由于没有解决我的问题,我实施了解决方案。 Load new fxml in borderpane center
JavaFX: How to update the center view of a borderpane with new values
Main.java
package sample;
import Controllers.RootController;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
@FXML
private BorderPane rootLayout;
private Stage primaryStage;
@Override
public void start(Stage primaryStage) throws Exception{
this.primaryStage = primaryStage;
mainwindow();
}
public void mainwindow() throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/Views/Rootlayout.fxml"));
rootLayout = loader.load();
RootController rootController = loader.getController();
rootController.setMain(this);
Scene scene= new Scene(rootLayout);
primaryStage.setTitle("Inventory Manager");
primaryStage.setScene(scene);
primaryStage.show();
}
public BorderPane getRootLayout() {
return rootLayout;
}
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
RootController.java
package Controllers;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import sample.Main;
import java.io.IOException;
public class RootController {
@FXML
private Button rootBtnInsrt;
@FXML
private Button rootBtnUpdate;
@FXML
private BorderPane rootLayout;
private Main main;
public void setMain(Main main) {
this.main = main;
}
@FXML
void btnInsrtClick(MouseEvent event) {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(RootController.class.getResource( "/Views/AddProduct.fxml"));
try {
VBox addProduct1 = loader.load();
} catch (IOException e) {
e.printStackTrace();
}
AddProductController addProductController= loader.getController();
addProductController.setMain(main);
main.getRootLayout().setCenter(rootLayout);
}
}
rootlayout.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<BorderPane fx:id="rootLayout" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controllers.RootController">
<top>
<MenuBar prefHeight="21.0" prefWidth="600.0" BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
<bottom>
<AnchorPane prefHeight="29.0" prefWidth="600.0" style="-fx-background-color: Black;" BorderPane.alignment="CENTER" />
</bottom>
<left>
<VBox alignment="CENTER" prefHeight="345.0" prefWidth="164.0" spacing="20.0" BorderPane.alignment="CENTER">
<children>
<Button fx:id="rootBtnInsrt" mnemonicParsing="false" onMouseClicked="#btnInsrtClick" prefHeight="50.0" prefWidth="132.0" text="Insert " />
<Button fx:id="rootBtnUpdate" mnemonicParsing="false" onMouseClicked="#btnUpdateClick" prefHeight="50.0" prefWidth="135.0" text="Update" />
<Button mnemonicParsing="false" prefHeight="50.0" prefWidth="158.0" text="View" />
</children>
<padding>
<Insets left="10.0" right="10.0" />
</padding>
</VBox>
</left>
</BorderPane>
Addproduct.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<VBox fx:id="addproduct" alignment="CENTER" prefHeight="249.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label alignment="CENTER" text="Add Product" VBox.vgrow="ALWAYS" />
<GridPane VBox.vgrow="ALWAYS">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Product Code" />
<Label text="Product Name :" GridPane.rowIndex="1" />
<Label text="Product Price :" GridPane.rowIndex="2" />
<Label text="Product Quantity :" GridPane.rowIndex="3" />
<TextField GridPane.columnIndex="1" />
<TextField GridPane.columnIndex="1" GridPane.rowIndex="1" />
<TextField GridPane.columnIndex="1" GridPane.rowIndex="2" />
<TextField GridPane.columnIndex="1" GridPane.rowIndex="3" />
</children>
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
</GridPane>
<HBox alignment="CENTER" spacing="30.0">
<children>
<Button alignment="CENTER" maxHeight="40.0" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="ADD" HBox.hgrow="ALWAYS" />
<Button alignment="CENTER" maxHeight="40.0" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Reset" HBox.hgrow="ALWAYS" />
<Button maxHeight="40.0" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Back" HBox.hgrow="ALWAYS" />
</children>
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" />
</padding>
</HBox>
</children>
</VBox>
尝试运行以下代码时出现错误。
全部在同一错误窗格中 这是我的目录结构。 Directory Structure
答案 0 :(得分:0)
因此,基本上,在所有工作和帮助之后,由于Try / Catch块而出现了问题。
FXMLLoader loader = new FXMLLoader();
loader.setLocation(RootController.class.getResource( "/Views/AddProduct.fxml"));
try {
VBox addProduct1 = loader.load();
}
catch (IOException e) {
e.printStackTrace();
}
我删除了try / catch并为方法添加了例外,即void btnInsrtClick(MouseEvent event) throws IOException