对于一个项目,我需要使用JavaFX创建一个GUI。因此,在我密切关注的Scene Builder / JavaFX教程中,我创建了一个非常基本的GUI。但是加载似乎无法正常工作,并引发LoadException。我在网上似乎遇到了一些类似的问题,但是他们的答案似乎都无法解决我的问题。我已经尽力尝试类似的指南和教程,做了很多googleFoo,但是它不会加载。
这是主要的:
package stacksim;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class stacksim extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
try {
// Read file fxml and draw interface.
Parent root = FXMLLoader.load(getClass()
.getResource("stacksim.fxml"));
primaryStage.setTitle("Stack Simulation");
primaryStage.setScene(new Scene(root));
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
}
控制器:
package stacksim;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.transform.Translate;
/**
* FXML Controller class
*
* @author Dell
*/
public class stacksimcontroller implements Initializable {
@FXML
private Button btnPush;
@FXML
private Button btnPop;
@FXML
private Button btnExit;
@FXML
private GridPane gridBoxBall;
@FXML
private GridPane gridBoxNum;
@FXML
private Label description;
private int curColumn = 0;
private int count = 0;
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
public void actionPush(ActionEvent event) {
System.out.println("Button Push Clicked!");
if (curColumn >= 10) {
description.setText("Stack is fully, you can't push more !");
} else {
addCircle(curColumn, 0);
addLabel(curColumn, 0);
System.out.println(' ');
count++;
description.setText("Push successfully!");
curColumn++;
System.out.println(curColumn);
}
}
public void actionPop(ActionEvent event) {
System.out.println("Button Pop Clicked!");
if (curColumn == 0) {
description.setText("Stack Empty! Nothing to pop!");
System.out.println(curColumn);
} else if (curColumn == 1) {
curColumn--;
gridBoxBall.getChildren().remove(curColumn);
gridBoxNum.getChildren().remove(curColumn);
description.setText("Pop successfully!");
System.out.println(curColumn);
} else {
curColumn--;
gridBoxBall.getChildren().remove(curColumn);
gridBoxNum.getChildren().remove(curColumn);
System.out.println(curColumn);
for (int i = 0; i < curColumn; i++) {
Translate translate = new Translate();
translate.setZ(0);
translate.setY(0);
translate.setX(0);
gridBoxBall.getChildren().get(i).getTransforms().addAll(translate);
gridBoxNum.getChildren().get(i).getTransforms().addAll(translate);
}
description.setText("Pop successfully!");
}
}
public void actionExit(ActionEvent event) {
System.exit(0);
}
public void addCircle(int col, int row){
Circle circle = new Circle(20);
circle.setFill(Color.FIREBRICK);
gridBoxBall.add(circle, col, row);
}
public void addLabel(int col, int row){
Label label = new Label(count + "");
label.setPrefSize(40, 40);
label.setAlignment(Pos.CENTER);
gridBoxNum.add(label, col, row);
}
}
fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.shape.*?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="queuedemon.QueueDemonController">
<children>
<Label alignment="CENTER" contentDisplay="CENTER" layoutX="-4.0" layoutY="-3.0" prefHeight="65.0" prefWidth="606.0" text="STACK HAVING 10 SLOTS" textFill="FIREBRICK">
<font>
<Font name="Arial Bold" size="26.0" />
</font>
</Label>
<HBox alignment="CENTER" layoutX="1.0" layoutY="335.0" prefHeight="51.0" prefWidth="600.0" spacing="100.0">
<children>
<Button fx:id="btnPush" alignment="CENTER" mnemonicParsing="false" onAction="#actionPush" text="PUSH" textFill="FIREBRICK" />
<Button fx:id="btnPop" alignment="CENTER" mnemonicParsing="false" onAction="#actionPop" text="POP" textFill="FIREBRICK" />
</children>
<opaqueInsets>
<Insets />
</opaqueInsets>
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
</HBox>
<Label fx:id="description" alignment="CENTER" layoutX="2.0" layoutY="281.0" prefHeight="17.0" prefWidth="600.0" textFill="FIREBRICK">
<font>
<Font size="14.0" />
</font>
</Label>
<GridPane fx:id="gridBoxBall" hgap="10.0" layoutX="62.0" layoutY="180.0" prefHeight="40.0" prefWidth="500.0" AnchorPane.bottomAnchor="180.0" AnchorPane.leftAnchor="62.0" AnchorPane.rightAnchor="38.0" AnchorPane.topAnchor="180.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<padding>
<Insets left="10.0" right="10.0" />
</padding>
</GridPane>
<GridPane fx:id="gridBoxNum" hgap="10.0" layoutX="62.0" layoutY="241.0" prefHeight="40.0" prefWidth="500.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<padding>
<Insets left="10.0" right="10.0" />
</padding>
</GridPane>
<Button fx:id="btnExit" alignment="CENTER" layoutX="537.0" layoutY="14.0" mnemonicParsing="false" onAction="#actionExit" text="EXIT" textFill="FIREBRICK" />
</children>
</AnchorPane>
错误:
javafx.fxml.LoadException:
/E:/Study/OOP/Java/JavaMidterm/TestQueueMidterm/bin/stacksim/stacksim.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2848)
at javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2692)
at javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2661)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2517)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at stacksim.stacksim.start(stacksim.java:31)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException
at javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2899)
at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2846)
... 20 more