我在点击“提交”按钮时尝试切换场景。
当我运行程序时,我收到此错误:
Caused by: java.lang.ClassNotFoundException: capitals.FXMLAnswerController
以下是上下文:
我有一个控制主窗口的FXML控制器FXMLController.java
,这里是代码:
package capitals;
// imports ommited
public class FXMLController implements Initializable {
@FXML
private Button submitButton;
@FXML
private Button exitButton;
@FXML
private void handleSubmit(ActionEvent event) throws IOException
{
Parent parent = FXMLLoader.load(getClass().getResource("answerPage.fxml")); // same package: capitals
Scene scene = new Scene(parent);
Stage appStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
appStage.setScene(scene);
appStage.show();
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle)
{
}
}
相应的FXML文档如下(FXMLDocument.fxml
):
<?xml version="1.0" encoding="UTF-8"?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="150.0" prefWidth="435.0" styleClass=".root" xmlns="http://javafx.com/javafx/8.0.102" xmlns:fx="http://javafx.com/fxml/1" fx:controller="capitals.FXMLController">
<children>
<TextField layoutX="83.0" layoutY="66.0" prefHeight="35.0" prefWidth="269.0" promptText="Type here..." style="-fx-background-radius: 6,6,6,6;" />
<Button id="exitButton" fx:id="exitButton" cancelButton="true" layoutX="306.0" layoutY="109.0" mnemonicParsing="false" onAction="#exit" style="-fx-font: 15 arial; -fx-background-radius: 7,7,7,7; -fx-base: #dbd8d6;" text="Exit" textAlignment="CENTER" textFill="RED" />
<Text layoutX="11.0" layoutY="49.0" strokeType="OUTSIDE" strokeWidth="0.0" text="What's the capital of The United Kingdom?" textAlignment="CENTER">
<font>
<Font name="Century Gothic Bold" size="22.0" />
</font>
</Text>
<Button id="exitButton" fx:id="submitButton" cancelButton="true" layoutX="83.0" layoutY="109.0" mnemonicParsing="false" onAction="#handleSubmit" style="-fx-font: 15 arial; -fx-background-radius: 7,7,7,7; -fx-base: #b6e7c9;" text="Submit" textAlignment="CENTER" textFill="#141414" />
</children>
</AnchorPane>
我接下来做的是在点击“提交”按钮时创建适当的场景,相应的FXML文档名为answerPage.fxml
:
<?xml version="1.0" encoding="UTF-8"?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="491.0" prefWidth="667.0" xmlns="http://javafx.com/javafx/8.0.102" xmlns:fx="http://javafx.com/fxml/1" fx:controller="capitals.FXMLAnswerController">
<children>
<VBox layoutX="287.0" layoutY="85.0" prefHeight="321.0" prefWidth="381.0" />
<Label fx:id="resultStatement" alignment="CENTER" layoutX="275.0" layoutY="14.0" text="LABEL!" textAlignment="CENTER">
<font>
<Font name="Century Gothic Bold" size="40.0" />
</font>
</Label>
<ImageView fx:id="flag" fitHeight="154.0" fitWidth="250.0" layoutX="47.0" layoutY="134.0" pickOnBounds="true" preserveRatio="true" />
<Button fx:id="playAgainButton" defaultButton="true" layoutX="164.0" layoutY="420.0" mnemonicParsing="false" prefHeight="38.0" prefWidth="97.0" style="-fx-background-radius: 7,7,7,7;" text="Play Again" textAlignment="CENTER">
<font>
<Font name="Century Gothic Bold Italic" size="15.0" />
</font>
</Button>
<Button fx:id="exitButton" cancelButton="true" layoutX="393.0" layoutY="420.0" mnemonicParsing="false" prefHeight="38.0" prefWidth="97.0" style="-fx-base: #dbd8d6; -fx-backround-radius: 7,7,7,7;" text="Exit" textAlignment="CENTER" textFill="RED">
<font>
<Font name="Century Gothic Bold Italic" size="15.0" />
</font>
</Button>
</children>
</AnchorPane>
答案控制器目前是空的:
package capitals;
public class FXMLAnswerController implements Initializable {
@FXML
private Button playAgainButton;
@FXML
private Button exitButton;
@Override
public void initialize(URL url, ResourceBundle resourceBundle)
{
}
}
请注意,所有这些文件都在同一个包中,它们位于src / capitals文件夹中。 FXML文件也在bin /中。 (出于长篇原因,有意省略进口)。
我提前感谢您的帮助。