[英]How to add to JavaFX Scene stylesheet from another module in Java 9?
I have two JPMS modules: 我有两个JPMS模块:
In module-a I have something like: 在模块a中,我有类似以下内容:
public class MyAppplication extends Application {
....
public static void addCss(String path) {
stage.getScene().getStylesheets().add(path);
}
}
In module-b I have CSS file which I want to add to MyApplication
. 在模块b中,我有要添加到MyApplication
CSS文件。 How to do it in code in module-b? 如何在模块b中的代码中执行此操作? I can't understand how to pass path from another module. 我不明白如何从另一个模块传递路径。
I mean in module-b: 我的意思是在模块b中:
...
MyApplication.addCss(???);
...
EDIT 编辑
In OSGi I used the following solution in bundle-b
(assuming, that module-a was bundle-a, and module-b was bundle-b): 在OSGi中,我在bundle-b
使用了以下解决方案(假设模块a是bundle-a,而模块b是bundle-b):
String pathInBundleB = "com/foo/package-in-bundle-b/file.css"
Bundle bundleB = FrameworkUtil.getBundle(this.getClass()).getBundleContext().getBundle();
URL cssFileUrl = bundleB.getEntry(pathInBundleB);
MyApplication.addCss(cssFileUrl.toString());
I found the solution with the help of @AlanBateman. 我在@AlanBateman的帮助下找到了解决方案。
Assuming, that css file is in com/foo/some-package/file.css
I use the following code in module-b: 假设,css文件位于com/foo/some-package/file.css
我在module-b中使用以下代码:
package com.foo.some-package;
public class SomeClass {
public void init() {
MyApplication.addCss(this.getClass().getResource("base.css").toString());
}
}
Besides, in module-info of module-b I have: 此外,在模块b的module-info中,我有:
opens com.foo.some-package to module-a;
package org.apis.style.css;
public class CommonCss {
public static String getCommonCssStyle(){
return CommonCss.class.getClassLoader().getResource("common.css").toExternalForm();
}
}
Export this package to all. 将此包导出到所有。
In other module I add this 在其他模块中,我添加了这个
getStylesheets().add(CommonCss.getCommonCssStyle());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.