加载样式表,并在用户单击按钮时将其应用于场景
调用getScene()返回null。
函数所在的类是场景的控制器和根节点,我使用SceneBuilder 2.0,并将类设置为加载fxml的控制器,它是一个VBox
。
VBox guiRootNode = null; // inside this instance is where the `getScene() call is`
try {
FXMLLoader loader = new FXMLLoader(MainWindow.class.getResource("MainWindow.fxml"));
guiRootNode = (VBox) loader.load();
} catch (IOException e) {
e.printStackTrace();
}
if (guiRootNode == null) {
new Alert(Alert.AlertType.ERROR, "The GUI could not be loaded").showAndWait();
Platform.exit();
} else {
primaryStage.setScene(new Scene(guiRootNode));
}
问题代码是MainWindow
类中的一个成员函数,@FXML
标记是这样的,我可以通过MainWindow.fxml
设置按钮来调用它。
@FXML
private void onDefaultCssClicked()
{
// getScene() returns null
getScene().getStylesheets().remove(getClass().getResource("dark.css").toExternalForm());
getScene().getStylesheets().add(getClass().getResource("default.css").toExternalForm());
}
完整的代码可以在https://github.com/SebastianTroy/FactorioManufacturingPlanner然而,从长远来看,它并不代表一个最小的代码示例。。。
JavaFX-getScene()返回null此QA假设getScene()
调用是在初始化
函数中或在实例化过程中完成的。
JavaFX getScene()在本QA中控制器的initialize方法中返回null。该调用具体在initialise
方法中,因此在这里不适用。
fx: root
构造,检查SceneBuilder中的选项会导致错误javafx.fxml.LoadExctive: Root尚未设置。在加载之前使用setRoot()方法。
button.getScene()
工作正常,所以我有我的黑客,但我还是想了解这个问题。我不再试图让我的控制器成为根gui对象。
基本上,我已经分析过FXGUI的控制器是根gui节点,因此我让控制器扩展根gui节点的类型。当然不是这样,控制器是并且应该是一个单独的类,其中注入了一些gui变量。
<VBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.gui.MainWindow">
<children>
<Button onAction="#click" text="button" fx:id="button"/>
</children>
</VBox>
public class MainWindow extends VBox {
@FXML
private Button button;
@FXML
private void click() {
System.out.println("Controller scene: "+ getScene());
System.out.println("Button scene: "+ button.getScene());
}
}
Controller scene: null
Button scene: javafx.scene.Scene@4d6ed40a
为fxml根目录加载的VBox
与用作控制器根目录的实例不同。将加载的节点添加到场景中,但不将控制器添加到场景中,因此getScene()
返回null
。
loader.getRoot() == loader.getController()
产生false
。
要使用与控制器和根相同的实例,请使用
<fx:root type="application.gui.MainWindow" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button onAction="#click" text="button" fx:id="button"/>
</children>
</fx:root>
MainWindow mainWindow = new MainWindow();
FXMLLoader loader = new FXMLLoader(MainWindow.class.getResource("MainWindow.fxml"));
loader.setRoot(mainWindow);
loader.setController(mainWindow);
loader.load();
... new Scene(mainWindow) ...
从
main窗口的构造函数中执行此操作可能比较方便:
public MainWindow() {
FXMLLoader loader = new FXMLLoader(MainWindow.class.getResource("MainWindow.fxml"));
loader.setRoot(this);
loader.setController(this);
try {
loader.load();
} catch (IOException ex) {
throw new IllegalStateException("cannot load fxml", ex); // or use a different kind of exception / add throws IOException to the signature
}
}
这允许您使用
new MainWindow()
初始化加载MainWindow
。
问题内容: 我刚刚开始使用JavaFX Scene Builder来构建一个小型应用程序。 它由属于“ login.fxml”的控制器类“ Login.java”组成,其中通过称为“ registrationClicked(ActionEvent event)”的方法加载了FXML文件“ registrierung.fxml”: 现在,我想通过根节点vboxRoot引用控制器类“ Registri
我刚刚开始使用JavaFX Scene Builder来构建一个小型应用程序。 它由属于login.fxml的控制器类Login.java组成,其中FXML文件registrierung.fxml通过名为注册点击(动作事件)的方法加载: 现在我想参考一下“registrierung”的阶段。fxml“在控制器类”注册表中。java“通过根节点vboxRoot: 但是,“getScene()”总是导
问题内容: 我昨天在JavaFX中构建了一个小应用程序。我想在Controller类中获取应用程序的场景。每次尝试在控制器类中获取场景时,都会出错。我可以在Controller类的Button上设置OnKeyPressed方法,效果很好。但是,只有在选择Button后,它才能正常工作。我只能在Main类方法replaceSceneContent中获得场景。我已经读过这个问题,但是我在initial
我昨天在JavaFX中构建了一个小应用程序。我想在Controller类中获取应用程序的场景。每次我试图在控制器类中获取场景时,我都会出错。我可以在Controller类中的Button上设置OnKeyPested-method,工作正常。但是只有当按钮被选中时,它才工作正常...我可以只在Main类方法replace eSceneContent中获取场景。我已经读过这个问题了,但是我在初始化方法
我是开发android应用程序的新手 我正在开发一款音乐播放器,希望从音频文件的元数据中获取歌曲名称、艺术家、专辑名称、专辑艺术 我使用了,但当有800首歌曲时,速度会很慢,有些文件返回null,而其他音乐播放器可以检索这些信息。 然后我使用获取歌曲名称、艺术家、专辑名称和媒体商店。音频专辑。外部内容_URI获取专辑艺术。 是否有任何uri可同时获取(歌曲名称、艺术家、专辑名称、专辑艺术) 因为我
我有两个活动和两个布局。当我在第一个活动中显示列表时,一切都正常,并在单击时给出列表中项目的编号,但当我尝试在第二个活动中重复相同的内容时,它会告诉我RecycleServiceClickListener侦听器为空。 适配器: 第一项活动: 第二项活动: 错误: 我不明白为什么在第一种情况下,它正常处理单击,而在第二种情况下,它说RecyclerViewClickListener为null