我是 FXML 的新手,我正在尝试制作可以在使用 FXML SceneBuilder 之间切换的场景,因为它更方便。问题在于
Parent root1 = FXMLLoader.load(getClass().getResource("/test/FXMLDocument2.fxml"));
line,它适用于舞台设置的第一个场景,但是当我尝试使用相同的代码行(不同的路径和变量名称 ofc)创建第二个场景时,它编译失败。我已经尝试将路径设置为:“test/FXMLDocument2.fxml”、“FXMLDocument2.fxml”和“/test/FXMLDocument2.fxml”,但似乎没有任何效果。所有文件都在“测试”包中,因此我不明白代码有什么问题。我的猜测是这是一个 IDE 配置错误,但我没有在我读过的任何线程中发现任何有用的信息。
这是我所有的文件: 测试.java
package test;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Test extends Application {
static Stage primStage = new Stage();
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); //why does this work here, but not in "Class"?
Scene scene = new Scene(root);
primaryStage = primStage;
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
FXMLDocument.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test.FXMLDocumentController">
<children>
<Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
<Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
</children>
</AnchorPane>
FXMLDocumentController.java
package test;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
public class FXMLDocumentController implements Initializable {
@FXML
private Label label;
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
Test.primStage.setScene(new Class().method()); //this is the functionality I'd like to implement
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
类.java
package test;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
public class Class {
public Scene method() {
Parent root1 = FXMLLoader.load(getClass().getResource("/test/FXMLDocument2.fxml")); //this is the line that fails to compile
Scene scene2 = new Scene(root1, 700, 500);
scene2.setFill(Color.TRANSPARENT);
return scene2;
}
}
FXMLDocument2.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Text?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.171" fx:controller="test.FXMLDocument2Controller">
<children>
<Text layoutX="279.0" layoutY="196.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Scene 2" />
</children>
</AnchorPane>
FXMLDocument2Controller.java
package test;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.text.Text;
public class FXMLDocument2Controller implements Initializable {
@FXML
private Text text;
@Override
public void initialize(URL url, ResourceBundle rb) {
}
}