JavaFX加载FXML窗口为空:运行代码仅打开空白窗口
Hey there! Let's figure out why your Mastermind window is showing up empty instead of loading your FXML content—this is a super common JavaFX pitfall, so we'll get it sorted quickly.
1. Your UC1Controller isn't a valid JavaFX Node (and isn't attaching FXML content)
The biggest issue here is that you're passing an instance of UC1Controller directly to new Scene(root). For this to work, UC1Controller must extend a JavaFX layout container (like AnchorPane, VBox, or Pane) so it's a valid Node that can render content in the scene.
If your controller is just a plain Java class with no Node inheritance, the scene has nothing to display—hence the blank window.
Fix: Make your controller a Node and load FXML into it
Modify your UC1Controller to extend a layout class, then load your FXML file into it in the constructor:
import javafx.fxml.FXMLLoader; import javafx.scene.layout.AnchorPane; import java.io.IOException; public class UC1Controller extends AnchorPane { private final DomeinController dc; public UC1Controller(DomeinController dc) { this.dc = dc; // Adjust the FXML path to match your project's resource structure FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/yourpackage/UC1View.fxml")); loader.setController(this); // Bind this class to the FXML's controller loader.setRoot(this); // Attach FXML content directly to this controller instance try { loader.load(); } catch (IOException e) { // Don't suppress exceptions—this will help you debug path issues throw new RuntimeException("Failed to load FXML file", e); } } }
2. Check Your FXML File for Misconfigurations
Even if your controller is set up correctly, your FXML might be causing the blank window:
- Incorrect
fx:controller: Double-check that thefx:controllerattribute in your FXML points to the fully qualified name ofUC1Controller(e.g.,com.yourpackage.UC1Controller). - Empty FXML content: Make sure your FXML actually has visible UI elements (buttons, labels, grids, etc.) defined inside the root layout. An empty FXML will result in an empty window.
- Wrong FXML path: If
getResource()can't find your FXML file, it may fail silently (or throw an exception if you catch it). Ensure the path matches where your FXML is stored (e.g., if it's insrc/main/resources, use the path relative to that folder).
3. Alternative Approach: Load FXML Directly in StartUpGui
If you prefer not to have your controller extend a Node, you can load the FXML directly in your start() method and pass the DomeinController to your controller manually:
@Override public void start(Stage primaryStage) throws Exception { DomeinController dc = new DomeinController(); FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/yourpackage/UC1View.fxml")); // Set the controller manually (skip the fx:controller attribute in FXML) UC1Controller controller = new UC1Controller(dc); loader.setController(controller); // Load the FXML root node and create the scene AnchorPane root = loader.load(); Scene scene = new Scene(root); primaryStage.setTitle("Mastermind"); primaryStage.setScene(scene); primaryStage.show(); }
This approach keeps your controller decoupled from UI layout classes, which is often cleaner for larger projects.
内容的提问来源于stack exchange,提问作者guust ysebie

