简体   繁体   English

JavaFX:window 为黑色且不显示 SceneBuilder 更新

[英]JavaFX: window is black and does not show SceneBuilder updates

I'm learning JavaFX and I'm trying to make a graphical app with SceneBuilder, but the window is blank with nothing on it.我正在学习 JavaFX 并且我正在尝试使用 SceneBuilder 制作图形应用程序,但是 window 是空白的,上面什么都没有。 I added some buttons to the window on SceneBuilder, but I can't see them when I run the app.我在 SceneBuilder 上的 window 中添加了一些按钮,但在运行应用程序时看不到它们。 The app is based on MVC:该应用程序基于 MVC:

Main:主要的:

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;



public class Main extends Application {
    @Override
    public void start(Stage stage) {
        try {
            FXMLLoader loader = new FXMLLoader(Main.class.getResource("Test.fxml"));
            AnchorPane root = (AnchorPane) loader.load();
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            stage.setTitle("Test");
            stage.setScene(scene);
            stage.show();
            
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

Controller: Controller:

package application;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.layout.AnchorPane;

public class Controller {
    @FXML
    private AnchorPane base;
    
    @FXML
    public void setBackgroundRed(ActionEvent event) {
        base.setStyle("-fx-background-color: red;");
    }
    @FXML
    public void setBackgroundGreen(ActionEvent event)   {
        base.setStyle("-fx-background-color: green;");
    }
}

That's what I made on SceneBuilder, and what the GUI looks like: SceneBuilder GUI这就是我在 SceneBuilder 上所做的,以及 GUI 的样子: SceneBuilder GUI

That's what I get instead, when I run the application: blank window这就是我在运行应用程序时得到的结果:空白 window

Edit: if I close and reopen SceneBuilder the GUI is ok.编辑:如果我关闭并重新打开 SceneBuilder,GUI 就可以了。 image图片

You're using Eclipse IDE right?您正在使用 Eclipse IDE 对吗?

I think the issue is Eclipse.我认为问题是 Eclipse。 In fact, Eclipse by default doesn't refersh project resources automatically.事实上,Eclipse 默认不会自动引用项目资源。 Therefore, if you update and save your.fxml file from SceneBuilder, it goes out-of-sync inside the IDE, until you refresh the resources ( right click on Project folder > Refresh, or F5).因此,如果您从 SceneBuilder 更新并保存您的 .fxml 文件,它会在 IDE 中不同步,直到您刷新资源右键单击项目文件夹 > 刷新或 F5)。

You can make Eclipse refresh resources automatically by selecting the option Refresh using native hooks or polling: Window > Preferences > General > Workspace > "refresh resources automatically".您可以通过选择使用本机挂钩或轮询刷新选项使 Eclipse自动刷新资源:Window > 首选项 > 常规 > 工作区 > “自动刷新资源”。

That issue had been widely discussed in this thread .该问题已在此线程中广泛讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM