JavaFX – CSS无法在Linux中加载

我正在使用JavaFX来开发一个多平台的应用程序,我使用Netbeans 7.2.1与JavaFX 2.2和Java 1.7u21(以前使用1.7u9和工作很好)的Win7。 正如我所说的,Win7运行得很好,我的应用程序。 当我尝试在Ubuntu 12.04上运行它时,它无法加载我的CSS样式。 每个fxml文件都添加了其控件的样式表类。 然后每个控件都增加了样式。 例如:在一个空白button上,我正在应用这种风格:

buttonStyle.css:

.button { -fx-text-fill: white; -fx-alignment: CENTER; -fx-border-radius: 5; -fx-font-size: 13pt; -fx-font-family: "Verdana"; -fx-font-weight: bold; -fx-background-color: linear-gradient(#328BDB 0%, #207BCF 25%, #1973C9 75%, #0A65BF 100%); } .button:hover { -fx-background-color: linear-gradient(#2C8AE7 0%, #0C6AC7 25%, #0A58A5 75%, #093B6D 100%); } .button:pressed { -fx-background-color: linear-gradient(#328BDB 0%, #207BCF 25%, #1973C9 75%, #0A65BF 100%); } 

在Win7上工作正常,但它在Ubuntu上产生以下exception:

java.lang.ClassNotFoundException: AUTHOR Exception in Application start method java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at com.javafx.main.Main.launchApp(Main.java:642) at com.javafx.main.Main.main(Main.java:805) Caused by: java.lang.RuntimeException: Exception in Application start method at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403) at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47) at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115) at java.lang.Thread.run(Thread.java:722) Caused by: java.lang.ArrayIndexOutOfBoundsException: 512 at com.sun.javafx.css.StyleConverter.readBinary(StyleConverter.java:99) at com.sun.javafx.css.ParsedValue.readBinary(ParsedValue.java:600) at com.sun.javafx.css.Declaration.readBinary(Declaration.java:142) at com.sun.javafx.css.Rule.readBinary(Rule.java:189) at com.sun.javafx.css.Stylesheet.readBinary(Stylesheet.java:185) at com.sun.javafx.css.Stylesheet.loadBinary(Stylesheet.java:215) at com.sun.javafx.css.StyleManager.loadStylesheetUnPrivileged(StyleManager.java:572) at com.sun.javafx.css.StyleManager.loadStylesheet(StyleManager.java:411) at com.sun.javafx.css.StyleManager.access$1900(StyleManager.java:72) at com.sun.javafx.css.StyleManager$StylesheetContainer.gatherParentStylesheets(StyleManager.java:1140) at com.sun.javafx.css.StyleManager$StylesheetContainer.getStyleHelper(StyleManager.java:1225) at com.sun.javafx.css.StyleManager$StylesheetContainer.access$1300(StyleManager.java:1031) at com.sun.javafx.css.StyleManager.getStyleHelper(StyleManager.java:968) at javafx.scene.Node.impl_createStyleHelper(Node.java:7433) at javafx.scene.Node.impl_processCSS(Node.java:7403) at javafx.scene.Parent.impl_processCSS(Parent.java:1146) at javafx.scene.Parent.impl_processCSS(Parent.java:1153) at javafx.scene.Parent.impl_processCSS(Parent.java:1153) at javafx.scene.Node.processCSS(Node.java:7383) at javafx.scene.Scene.doCSSPass(Scene.java:443) at javafx.scene.Scene.preferredSize(Scene.java:1445) at javafx.scene.Scene.impl_preferredSize(Scene.java:1512) at javafx.stage.Window$10.invalidated(Window.java:719) at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:127) at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:161) at javafx.stage.Window.setShowing(Window.java:782) at javafx.stage.Window.show(Window.java:797) at javafx.stage.Stage.show(Stage.java:229) at com.test.MainInterface.start(MainInterface.java:62) at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319) at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:206) at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76) at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method) at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82) ... 1 more

删除buttonStyle.css并添加comboStyle.css,会引发以下exception:

comboStyle.css:

 .combo-box { -fx-font: 13pt "Verdana"; -fx-alignment: baseline-left; -fx-background-color: linear-gradient(#328BDB 0%, #207BCF 25%, #1973C9 75%, #0A65BF 100%); } .combo-box .text { -fx-fill: white; } .combo-box .arrow-button { -fx-background-color: linear-gradient(#328BDB 0%, #207BCF 25%, #1973C9 75%, #0A65BF 100%); } .combo-box .arrow { -fx-background-color: white; } 

exception: java.io.EOFException at java.io.DataInputStream.readByte(DataInputStream.java:267) at com.sun.javafx.css.CompoundSelector.readBinary(CompoundSelector.java:351) at com.sun.javafx.css.Selector.readBinary(Selector.java:113) at com.sun.javafx.css.CompoundSelector.readBinary(CompoundSelector.java:344) at com.sun.javafx.css.Selector.readBinary(Selector.java:113) at com.sun.javafx.css.Rule.readBinary(Rule.java:182) at com.sun.javafx.css.Stylesheet.readBinary(Stylesheet.java:185) at com.sun.javafx.css.Stylesheet.loadBinary(Stylesheet.java:215) at com.sun.javafx.css.StyleManager.loadStylesheetUnPrivileged(StyleManager.java:572) at com.sun.javafx.css.StyleManager.loadStylesheet(StyleManager.java:411) at com.sun.javafx.css.StyleManager.access$1900(StyleManager.java:72) at com.sun.javafx.css.StyleManager$StylesheetContainer.gatherParentStylesheets(StyleManager.java:1140) at com.sun.javafx.css.StyleManager$StylesheetContainer.getStyleHelper(StyleManager.java:1225) at com.sun.javafx.css.StyleManager$StylesheetContainer.access$1300(StyleManager.java:1031) at com.sun.javafx.css.StyleManager.getStyleHelper(StyleManager.java:968) at javafx.scene.Node.impl_createStyleHelper(Node.java:7433) at javafx.scene.Node.impl_processCSS(Node.java:7403) at javafx.scene.Parent.impl_processCSS(Parent.java:1146) at javafx.scene.Parent.impl_processCSS(Parent.java:1153) at javafx.scene.Parent.impl_processCSS(Parent.java:1153) at javafx.scene.Node.processCSS(Node.java:7383) at javafx.scene.Scene.doCSSPass(Scene.java:443) at javafx.scene.Scene.preferredSize(Scene.java:1445) at javafx.scene.Scene.impl_preferredSize(Scene.java:1512) at javafx.stage.Window$10.invalidated(Window.java:719) at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:127) at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:161) at javafx.stage.Window.setShowing(Window.java:782) at javafx.stage.Window.show(Window.java:797) at javafx.stage.Stage.show(Stage.java:229) at com.test.MainInterface.start(MainInterface.java:62) at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319) at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:206) at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76) at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method) at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82) at java.lang.Thread.run(Thread.java:722) java.io.EOFException at java.io.DataInputStream.readByte(DataInputStream.java:267) at com.sun.javafx.css.CompoundSelector.readBinary(CompoundSelector.java:351) at com.sun.javafx.css.Selector.readBinary(Selector.java:113) at com.sun.javafx.css.CompoundSelector.readBinary(CompoundSelector.java:344) at com.sun.javafx.css.Selector.readBinary(Selector.java:113) at com.sun.javafx.css.Rule.readBinary(Rule.java:182) at com.sun.javafx.css.Stylesheet.readBinary(Stylesheet.java:185) at com.sun.javafx.css.Stylesheet.loadBinary(Stylesheet.java:215) at com.sun.javafx.css.StyleManager.loadStylesheetUnPrivileged(StyleManager.java:572) at com.sun.javafx.css.StyleManager.loadStylesheet(StyleManager.java:411) at com.sun.javafx.css.StyleManager.access$1900(StyleManager.java:72) at com.sun.javafx.css.StyleManager$StylesheetContainer.gatherParentStylesheets(StyleManager.java:1140) at com.sun.javafx.css.StyleManager$StylesheetContainer.getStyleHelper(StyleManager.java:1225) at com.sun.javafx.css.StyleManager$StylesheetContainer.access$1300(StyleManager.java:1031) at com.sun.javafx.css.StyleManager.getStyleHelper(StyleManager.java:968) at javafx.scene.Node.impl_createStyleHelper(Node.java:7433) at javafx.scene.Node.impl_processCSS(Node.java:7403) at javafx.scene.Parent.impl_processCSS(Parent.java:1146) at javafx.scene.Parent.impl_processCSS(Parent.java:1153) at javafx.scene.Parent.impl_processCSS(Parent.java:1153) at javafx.scene.Node.processCSS(Node.java:7383) at javafx.scene.Scene.doCSSPass(Scene.java:443) at javafx.scene.Scene.preferredSize(Scene.java:1445) at javafx.scene.Scene.impl_preferredSize(Scene.java:1512) at javafx.stage.Window$10.invalidated(Window.java:719) at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:127) at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:161) at javafx.stage.Window.setShowing(Window.java:782) at javafx.stage.Window.show(Window.java:797) at javafx.stage.Stage.show(Stage.java:229) at com.test.MainInterface.start(MainInterface.java:62) at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319) at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:206) at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76) at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method) at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82) at java.lang.Thread.run(Thread.java:722)

任何想法为什么这些样式不能在Linux中加载?

我只是找到了解决办法。 我在我的FXML文件上设置了所有的CSS引用。 另外,我正在阅读有关将CSS文件自动转换为二进制文件(.bss)的文章。 然后,我一直在研究一种方法来取消我的Maven项目中的转换,就像下面这样:

 <plugin> <groupId>com.zenjava</groupId> <artifactId>javafx-maven-plugin</artifactId> <version>1.5</version> <configuration> <mainClass>package.mainClass</mainClass> <css2bin>false</css2bin> <!-- THIS LINE --> ... </configuration> </plugin> 

其他类型的项目可以使用-Dbinary.css = false作为运行参数来避免这种转换。

最后,我只使用下面的代码将我所有的CSS加载到我的Controller类上:

 anchorPaneScene1.getStylesheets().add("/path/to/resources/css/button.css") button1.getStyleClass().add("button");