我正在尝试从JRuby脚本加载一个FXML表单。我可以通过Jython脚本成功地做到这一点。这是基本的Jython代码:
from javafx.application import Application
from javafx.fxml import FXMLLoader
from javafx.scene import Scene
class Main(Application):
def start(self, stage):
main_form = self.getClass().getResource('/main.fxml')
self.loader = FXMLLoader(main_form)
root = self.loader.load()
scene = Scene(root)
stage.setScene(scene)
stage.show()
args = []
print Main().class
print args
Application.launch(Main().class, args)脚本执行得很好,表单会显示出来。脚本的输出是:
<type 'org.python.proxies.__main__$Main$0'>
[]因此,'org.python.proxies.main$Main$0‘类型为Main.class()
这是我创建等效JRuby脚本的尝试:
java_import javafx.application.Application
java_import javafx.fxml.FXMLLoader
java_import javafx.scene.Scene
class Main < Application
attr_accessor :loader, :fxml_form, :scene
def start(stage)
@fxml_form = self.java_class.getResource('/main.fxml')
@loader = FXMLLoader.new(@fxml_form)
root = @loader.load()
@scene = Scene.new(root)
stage.set_scene(@scene)
stage.show()
end
end
args = Array.new()
puts Main.new().java_class
puts args
Application.launch(Main.new().java_class, args)此脚本的输出如下:
javafx.application.Application
NameError: no method 'launch' for arguments (org.jruby.javasupport.JavaClass,org.jruby.RubyArray) on Java::JavafxApplication::Application
available overloads:
(java.lang.String[])
(java.lang.Class,java.lang.String[])
(root) at /home/uros/NetBeansProjects/JRuby-JavaXF/lib/main.rb:31所以Main.new().java_class是javafx.application.Application,表单永远不会加载。在这两种情况下,我的FXML都是一个基本的'hello world‘形式:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="264.0" prefWidth="315.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button mnemonicParsing="false" prefHeight="34.0" prefWidth="89.0" text="Button" />
</children>
</StackPane>我在Linux 17上,我的Jython是2.7b3,JRuby是1.7.16.1。
编辑
其他信息:
当尝试使用jrubyfx宝石时,如下所示:
require 'jrubyfx'
class Main < JRubyFX::Application
def start(stage)
with(stage, title: "Hello World!", width: 800, height: 600) do
fxml "main.fxml"
show
end
end
end
Main.launch我得到以下例外:
NameError: cannot initialize Java class javafx.scene.control.ListView
(root) at /home/uros/.rvm/rubies/jruby-1.7.16.1/lib/ruby/gems/shared/gems/jrubyfx-1.1.0-java/lib/jrubyfx/core_ext/precompiled.rb:320
require at org/jruby/RubyKernel.java:1065
(root) at /home/uros/.rvm/rubies/jruby-1.7.16.1/lib/ruby/shared/rubygems/core_ext/kernel_require.rb:1
require at /home/uros/.rvm/rubies/jruby-1.7.16.1/lib/ruby/shared/rubygems/core_ext/kernel_require.rb:55
glob at org/jruby/RubyDir.java:242
load_dsl at /home/uros/.rvm/rubies/jruby-1.7.16.1/lib/ruby/gems/shared/gems/jrubyfx-1.1.0-java/lib/jrubyfx/dsl.rb:313
load_dsl at /home/uros/.rvm/rubies/jruby-1.7.16.1/lib/ruby/gems/shared/gems/jrubyfx-1.1.0-java/lib/jrubyfx/dsl.rb:312
require at org/jruby/RubyKernel.java:1065
(root) at /home/uros/.rvm/rubies/jruby-1.7.16.1/lib/ruby/gems/shared/gems/jrubyfx-1.1.0-java/lib/jrubyfx.rb:37
(root) at /home/uros/NetBeansProjects/RubyApplication4/lib/main.rb:1我的Java版本:
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) Server VM (build 25.25-b02, mixed mode)发布于 2014-12-26 20:00:59
NameError: cannot initialize Java class javafx.scene.control.ListView是由
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)在1.1.1中修正了JRubyFX错误 (它增加了java 8支持)。1.1.0只支持Java7(在u6之后)。现在,它应该可以使用您在7u6+和8中提供的代码:
require 'jrubyfx'
class Main < JRubyFX::Application
def start(stage)
with(stage, title: "Hello World!", width: 800, height: 600) do
fxml "main.fxml"
show
end
end
end
Main.launchhttps://stackoverflow.com/questions/26785689
复制相似问题