首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用JRuby加载FXML表单

使用JRuby加载FXML表单
EN

Stack Overflow用户
提问于 2014-11-06 17:30:25
回答 1查看 323关注 0票数 0

我正在尝试从JRuby脚本加载一个FXML表单。我可以通过Jython脚本成功地做到这一点。这是基本的Jython代码:

代码语言:javascript
复制
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)

脚本执行得很好,表单会显示出来。脚本的输出是:

代码语言:javascript
复制
<type 'org.python.proxies.__main__$Main$0'>
[]

因此,'org.python.proxies.main$Main$0‘类型为Main.class()

这是我创建等效JRuby脚本的尝试:

代码语言:javascript
复制
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)

此脚本的输出如下:

代码语言:javascript
复制
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‘形式:

代码语言:javascript
复制
<?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宝石时,如下所示:

代码语言:javascript
复制
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

我得到以下例外:

代码语言:javascript
复制
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版本:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-26 20:00:59

代码语言:javascript
复制
NameError: cannot initialize Java class javafx.scene.control.ListView

是由

代码语言:javascript
复制
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)

在1.1.1中修正了JRubyFX错误 (它增加了java 8支持)。1.1.0只支持Java7(在u6之后)。现在,它应该可以使用您在7u6+和8中提供的代码:

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26785689

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档