gpt4 book ai didi

JavaFX 未启动

转载 作者:行者123 更新时间:2023-12-04 08:24:47 27 4
gpt4 key购买 nike

我正尝试在 JavaFX 中启动一个项目,但我对自己做错了什么很迷茫。

主要内容

package Software1C482;

/*
* This program is explicitly for the use of Western Governers University's
* Software 1 submission by Andrew Whited.
*/


import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import ViewController.HomeWindowController;

/**
*
* @author Andrew Whited
*/
public class InventorySystem extends Application {
private AnchorPane MainScreenView;
private FXMLLoader viewLoader;

public void initMainScreen(Stage window) throws IOException{

viewLoader = new FXMLLoader();
viewLoader.setLocation(InventorySystem.class.getResource("/ViewController/HomeWindow.fxml"));
MainScreenView = viewLoader.load();

Scene scene = new Scene(MainScreenView);

window.setScene(scene);
window.show();
}

public void showMainScreen() throws IOException{
HomeWindowController controller = viewLoader.getController();
controller.setMainApp(this);
}

@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Inventory Mangement System");
initMainScreen(primaryStage);
showMainScreen();

}

public static void main(String[] args) {
launch(args);
}
}

相关文件如下。

主窗口 Controller :

package ViewController;

import Model.Inventory;
import static Model.Inventory.getPartInventory;
import static Model.Inventory.getProductInventory;
import static Model.Inventory.removePart;
import static Model.Inventory.removeProduct;
import static Model.Inventory.validatePartDelete;
import Model.Part;
import Model.Product;
import Software1C482.InventorySystem;
import java.io.IOException;
import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;

import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.stage.Modality;
import javafx.stage.Stage;
/**
* FXML Controller class
*
* @author Andrew
*/

//It's the home window.
public class HomeWindowController implements Initializable {

//Declaring the FXML parts.
@FXML
private TableView<Product> TableProduct;
@FXML
private TableColumn<Product, Integer> ProductID, ProductInventoryLevel;
@FXML
private TableColumn<Product, String> ProductName;
@FXML
private TableColumn<Product, Double> ProductPPU;
@FXML
private Button ButtonProductAdd;
@FXML
private Button ButtonProductModify;
@FXML
private Button ButtonProductDelete;
@FXML
private Button ButtonPartSearch;
@FXML
private TableView<Part> TablePart;
@FXML
private TableColumn<Part, Integer> PartID, PartInventoryLevel;
@FXML
private TableColumn<Part, String> PartName;
@FXML
private TableColumn<Part, Double> PartPPU;
@FXML
private TextField SearchFieldPart;
@FXML
private TextField SearchFieldProduct;

private static Part modifyPart;
private static int modifyPartIndex;
private static Product modifyProduct;
private static int modifyProductIndex;

public static int partToModifyIndex() {
return modifyPartIndex;
}

public static int productToModifyIndex() {
return modifyProductIndex;
}

public HomeWindowController() {
}

@FXML
void HomeExitClick(ActionEvent event) {

Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.initModality(Modality.NONE);
alert.setTitle("Confirmation");
alert.setHeaderText("Confirm Exit");
alert.setContentText("Are you sure you want to exit?");
Optional<ButtonType> result = alert.showAndWait();

if (result.get() == ButtonType.OK) {
System.exit(0);
} else {
System.out.println("Please resume completing form.");
}
}

@FXML
void HomeAddPartsClick(ActionEvent event) throws IOException {

Parent addParts = FXMLLoader.load(getClass().getResource("AddParts.fxml"));
Scene scene = new Scene(addParts);
Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
window.setScene(scene);
window.show();
}

@FXML
void HomeAddProductsClick(ActionEvent event) throws IOException {

Parent addProducts = FXMLLoader.load(getClass().getResource("AddProducts.fxml"));
Scene scene = new Scene(addProducts);
Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
window.setScene(scene);
window.show();
}

@FXML
void HomeModifyPartsClick(ActionEvent event) throws IOException {

modifyPart = TablePart.getSelectionModel().getSelectedItem();
modifyPartIndex = getPartInventory().indexOf(modifyPart);
Parent modifyParts = FXMLLoader.load(getClass().getResource("ModifyParts.fxml"));
Scene scene = new Scene(modifyParts);
Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
window.setScene(scene);
window.show();
}

@FXML
void HomeModifyProductsClick(ActionEvent event) throws IOException {

modifyProduct = TableProduct.getSelectionModel().getSelectedItem();
modifyProductIndex = getProductInventory().indexOf(modifyProduct);
Parent modifyProducts = FXMLLoader.load(getClass().getResource("ModifyProducts.fxml"));
Scene scene = new Scene(modifyProducts);
Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
window.setScene(scene);
window.show();
}

@FXML
void HomeSearchProductsBtn(ActionEvent event) throws IOException {
String searchProd = SearchFieldProduct.getText();
int prodIndex = -1;
if (Inventory.lookupProduct(searchProd) == -1) {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Search Error");
alert.setHeaderText("Product not found.");
alert.setContentText("The text entered does not match any Product.");
alert.showAndWait();
} else {
prodIndex = Inventory.lookupProduct(searchProd);
Product tempProd = Inventory.getProductInventory().get(prodIndex);
ObservableList<Product> tempProdList = FXCollections.observableArrayList();
tempProdList.add(tempProd);
TableProduct.setItems(tempProdList);
}
}

@FXML
void HomeDeleteProductsClick(ActionEvent event) throws IOException {
Product product = TableProduct.getSelectionModel().getSelectedItem();
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.initModality(Modality.NONE);
alert.setTitle("Confirm Delete");
alert.setHeaderText("Confirm?");
alert.setContentText("Are you sure you want to delete " + product.getProductName() + "?");
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK) {
removeProduct(product);
updateProductTableView();
System.out.println("Product " + product.getProductName() + " was removed.");
} else {
System.out.println("Product " + product.getProductName() + " was not removed.");
}
}

@FXML
void PartSearchOnAction(ActionEvent event) throws IOException {
String searchPart = SearchFieldPart.getText();
int partIndex = -1;
if (Inventory.lookupPart(searchPart) == -1) {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Search Error");
alert.setHeaderText("Part not found.");
alert.setContentText("The text entered does not match any Part.");
alert.showAndWait();
} else {
partIndex = Inventory.lookupPart(searchPart);
Part tempPart = Inventory.getPartInventory().get(partIndex);
ObservableList<Part> tempProdList = FXCollections.observableArrayList();
tempProdList.add(tempPart);
TablePart.setItems(tempProdList);
}
}

@FXML
void HomeDeletePartsClick(ActionEvent event) throws IOException {
Part part = TablePart.getSelectionModel().getSelectedItem();
if (validatePartDelete(part)) {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Part Delete Error.");
alert.setHeaderText("Part cannot be removed.");
alert.setContentText("This part is used in a product.");
alert.showAndWait();
} else {
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.initModality(Modality.NONE);
alert.setTitle("Product Delete");
alert.setHeaderText("Confirm?");
alert.setContentText("Are you sure you want to delete " + part.getPartName() + "?");
Optional<ButtonType> result = alert.showAndWait();

if (result.get() == ButtonType.OK) {
removePart(part);
updatePartTableView();
System.out.println("Part " + part.getPartName() + " was removed.");
} else {
System.out.println("Part " + part.getPartName() + " was not removed.");
}
}
}

@Override
public void initialize(URL url, ResourceBundle rb) {
PartID.setCellValueFactory(cellData -> cellData.getValue().partIDProperty().asObject());
PartName.setCellValueFactory(cellData -> cellData.getValue().partNameProperty());
PartInventoryLevel.setCellValueFactory(cellData -> cellData.getValue().partInvProperty().asObject());
PartPPU.setCellValueFactory(cellData -> cellData.getValue().partPriceProperty().asObject());
ProductID.setCellValueFactory(cellData -> cellData.getValue().productIDProperty().asObject());
ProductName.setCellValueFactory(cellData -> cellData.getValue().productNameProperty());
ProductInventoryLevel.setCellValueFactory(cellData -> cellData.getValue().productInvProperty().asObject());
ProductPPU.setCellValueFactory(cellData -> cellData.getValue().productPriceProperty().asObject());
updatePartTableView();
updateProductTableView();
}

public void updatePartTableView() {
TablePart.setItems(getPartInventory());
}

public void updateProductTableView() {
TableProduct.setItems(getProductInventory());
}

public void setMainApp(InventorySystem mainApp) {
updatePartTableView();
updateProductTableView();
}
}

最后主页窗口.FXML

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.Scene?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.Rectangle?>
<?import javafx.scene.text.Font?>
<?import javafx.stage.Stage?>

<Stage xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ViewController.HomeWindowController">
<scene>
<Scene>
<AnchorPane prefHeight="450.0" prefWidth="941.0">
<children>
<Label layoutX="40.0" layoutY="53.0" text="Inventory Management System">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="497.0" layoutY="108.0" text="Products">
<font>
<Font size="18.0" />
</font>
</Label>
<Rectangle arcHeight="50.0" arcWidth="50.0" fill="#1f93ff00" height="238.0" layoutX="479.0" layoutY="101.0" stroke="BLACK" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" width="418.0" />
<TextField fx:id="SearchFieldProduct" layoutX="737.0" layoutY="121.0" onAction="#HomeSearchProductsBtn" />
<Button layoutX="682.0" layoutY="121.0" mnemonicParsing="false" onAction="#HomeSearchProductsBtn" text="Search" />
<TableView fx:id="TableProduct" layoutX="501.0" layoutY="157.0" prefHeight="126.0" prefWidth="374.0">
<columns>
<TableColumn fx:id="ProductID" prefWidth="77.0" text="Product ID" />
<TableColumn fx:id="ProductName" prefWidth="89.0" text="Product Name" />
<TableColumn fx:id="ProductInventoryLevel" prefWidth="94.0" text="Inventory Level" />
<TableColumn fx:id="ProductPPU" prefWidth="113.0" text="Price/Cost Per Unit" />
</columns>
</TableView>
<Button layoutX="624.0" layoutY="290.0" mnemonicParsing="false" onAction="#HomeAddProductsClick" prefHeight="36.0" prefWidth="70.0" text="Add" />
<Button layoutX="708.0" layoutY="290.0" mnemonicParsing="false" onAction="#HomeModifyProductsClick" prefHeight="36.0" prefWidth="70.0" text="Modify" />
<Button layoutX="789.0" layoutY="290.0" mnemonicParsing="false" onAction="#HomeDeleteProductsClick" prefHeight="36.0" prefWidth="70.0" text="Delete" />
<Label layoutX="67.0" layoutY="108.0" text="Parts">
<font>
<Font size="18.0" />
</font>
</Label>
<Rectangle arcHeight="50.0" arcWidth="50.0" fill="#1f93ff00" height="238.0" layoutX="45.0" layoutY="101.0" stroke="BLACK" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" width="418.0" />
<TextField fx:id="SearchFieldPart" layoutX="303.0" layoutY="121.0" />
<Button layoutX="248.0" layoutY="121.0" mnemonicParsing="false" onAction="#PartSearchOnAction" text="Search" />
<TableView fx:id="TablePart" layoutX="67.0" layoutY="157.0" prefHeight="126.0" prefWidth="374.0">
<columns>
<TableColumn fx:id="PartID" prefWidth="79.0" text="Part ID" />
<TableColumn fx:id="PartName" prefWidth="86.0" text="Part Name" />
<TableColumn fx:id="PartInventoryLevel" prefWidth="94.0" text="Inventory Level" />
<TableColumn fx:id="PartPPU" prefWidth="114.0" text="Price/Cost Per Unit" />
</columns>
</TableView>
<Button layoutX="190.0" layoutY="290.0" mnemonicParsing="false" onAction="#HomeAddPartsClick" prefHeight="36.0" prefWidth="70.0" text="Add" />
<Button layoutX="274.0" layoutY="290.0" mnemonicParsing="false" onAction="#HomeModifyPartsClick" prefHeight="36.0" prefWidth="70.0" text="Modify" />
<Button layoutX="355.0" layoutY="290.0" mnemonicParsing="false" onAction="#HomeDeletePartsClick" prefHeight="36.0" prefWidth="70.0" text="Delete" />
<Button layoutX="749.0" layoutY="381.0" mnemonicParsing="false" onAction="#HomeExitClick" prefHeight="48.0" prefWidth="149.0" text="Exit" />
</children></AnchorPane>
</Scene>
</scene>
</Stage>

我只能用这三个文件重现错误。报错如下。

ant -f C:\\Users\\Andrew\\Documents\\NetBeansProjects\\Reprex -Dnb.internal.action.name=run run
init:
Deleting: C:\Users\Andrew\Documents\NetBeansProjects\Reprex\build\built-jar.properties
deps-jar:
Updating property file: C:\Users\Andrew\Documents\NetBeansProjects\Reprex\build\built-jar.properties
compile:
run:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: java.lang.ClassCastException: class javafx.stage.Stage cannot be cast to class javafx.scene.layout.AnchorPane (javafx.stage.Stage and javafx.scene.layout.AnchorPane are in module javafx.graphics of loader 'app')
at InventorySystem/Software1C482.InventorySystem.initMainScreen(InventorySystem.java:32)
at InventorySystem/Software1C482.InventorySystem.start(InventorySystem.java:48)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
Exception running application Software1C482.InventorySystem
C:\Users\Andrew\Documents\NetBeansProjects\Reprex\nbproject\build-impl.xml:1330: The following error occurred while executing this line:
C:\Users\Andrew\Documents\NetBeansProjects\Reprex\nbproject\build-impl.xml:902: Java returned: 1
BUILD FAILED (total time: 3 seconds)

如前所述,已重写此问题以更好地解决新问题。我还是个新手,所以这对于 reprex 来说可能很差。

最佳答案

据我所知,您的 fxml 包含 Stage 作为根元素,但您正在将 FXML 中的对象作为 AnchorPane 加载。

InvocationTargetException 的存在意味着在 AnchorPane 上调用了某些构造函数或字段或方法,但对象上不存在该字段/方法。

这是显而易见的,因为 Stage 不是 AnchorPane 的后代,所以即使是多态性也无济于事;当 FXMLLoader 尝试在 AnchorPane 上执行类似 setScene 的操作时,这将失败。


您有两种选择:

  1. 从 FXML 加载整个舞台
  2. 从 FXML 中删除阶段声明,只将根保留为节点。

我更喜欢第二种,也是比较流行的一种。第一个选项有点多余,因为 javafx 已经在 start 方法中为您提供了一个 Stage,所以我看不到创建新阶段的好处:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.Scene?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.Rectangle?>
<?import javafx.scene.text.Font?>
<?import javafx.stage.Stage?>

<AnchorPane prefHeight="450.0" prefWidth="941.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ViewController.HomeWindowController">
<children>
<Label layoutX="40.0" layoutY="53.0" text="Inventory Management System">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="497.0" layoutY="108.0" text="Products">
<font>
<Font size="18.0" />
</font>
</Label>
<Rectangle arcHeight="50.0" arcWidth="50.0" fill="#1f93ff00" height="238.0" layoutX="479.0" layoutY="101.0" stroke="BLACK" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" width="418.0" />
<TextField fx:id="SearchFieldProduct" layoutX="737.0" layoutY="121.0" />
<Button layoutX="682.0" layoutY="121.0" mnemonicParsing="false" text="Search" />
<TableView fx:id="TableProduct" layoutX="501.0" layoutY="157.0" prefHeight="126.0" prefWidth="374.0">
<columns>
<TableColumn fx:id="ProductID" prefWidth="77.0" text="Product ID" />
<TableColumn fx:id="ProductName" prefWidth="89.0" text="Product Name" />
<TableColumn fx:id="ProductInventoryLevel" prefWidth="94.0" text="Inventory Level" />
<TableColumn fx:id="ProductPPU" prefWidth="113.0" text="Price/Cost Per Unit" />
</columns>
</TableView>
<Button layoutX="624.0" layoutY="290.0" mnemonicParsing="false" prefHeight="36.0" prefWidth="70.0" text="Add" />
<Button layoutX="708.0" layoutY="290.0" mnemonicParsing="false" prefHeight="36.0" prefWidth="70.0" text="Modify" />
<Button layoutX="789.0" layoutY="290.0" mnemonicParsing="false" prefHeight="36.0" prefWidth="70.0" text="Delete" />
<Label layoutX="67.0" layoutY="108.0" text="Parts">
<font>
<Font size="18.0" />
</font>
</Label>
<Rectangle arcHeight="50.0" arcWidth="50.0" fill="#1f93ff00" height="238.0" layoutX="45.0" layoutY="101.0" stroke="BLACK" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" width="418.0" />
<TextField fx:id="SearchFieldPart" layoutX="303.0" layoutY="121.0" />
<Button layoutX="248.0" layoutY="121.0" mnemonicParsing="false" onAction="#PartSearchOnAction" text="Search" />
<TableView fx:id="TablePart" layoutX="67.0" layoutY="157.0" prefHeight="126.0" prefWidth="374.0">
<columns>
<TableColumn fx:id="PartID" prefWidth="79.0" text="Part ID" />
<TableColumn fx:id="PartName" prefWidth="86.0" text="Part Name" />
<TableColumn fx:id="PartInventoryLevel" prefWidth="94.0" text="Inventory Level" />
<TableColumn fx:id="PartPPU" prefWidth="114.0" text="Price/Cost Per Unit" />
</columns>
</TableView>
<Button layoutX="190.0" layoutY="290.0" mnemonicParsing="false" prefHeight="36.0" prefWidth="70.0" text="Add" />
<Button layoutX="274.0" layoutY="290.0" mnemonicParsing="false" prefHeight="36.0" prefWidth="70.0" text="Modify" />
<Button layoutX="355.0" layoutY="290.0" mnemonicParsing="false" prefHeight="36.0" prefWidth="70.0" text="Delete" />
<Button layoutX="749.0" layoutY="381.0" mnemonicParsing="false" prefHeight="48.0" prefWidth="149.0" text="Exit" />
</children>
</AnchorPane>

我也会将您的代码更改为:

public class InventorySystem extends Application {
private AnchorPane MainScreenView;
private FXMLLoader viewLoader;

public void initMainScreen(Stage window) throws IOException{

viewLoader = new FXMLLoader();
viewLoader.setLocation(InventorySystem.class.getResource("/ViewController/HomeWindow.fxml"));
MainScreenView = viewLoader.load();

Scene scene = new Scene(MainScreenView);

window.setScene(scene);
window.show();
}

public void showMainScreen() throws IOException{
HomeWindowController controller = viewLoader.getController();
controller.setMainApp(this);
}

@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Inventory Mangement System");
initMainScreen(primaryStage);
showMainScreen();

}

public static void main(String[] args) {
launch(args);
}
}

关于JavaFX 未启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60482108/

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