2014-11-06 66 views
0

我想從JRuby腳本加載FXML表單。我可以通過Jython腳本成功完成此操作。這是基本的Jython代碼:使用JRuby加載FXML表格

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'> 
[] 

所以Main.class()類型爲「org.python.proxies。 主要 $主要$ 0

這是我試圖創建等效的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 Mint的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) 
+0

JRubyFX錯誤在1.1.1(其中添加了java 8支持)中得到修復。它現在應該工作。 – byteit101 2014-12-23 22:44:09

+0

您可以將評論移至答案,以便將其標記爲正確答案 – 2014-12-25 19:12:23

回答

2
NameError: cannot initialize Java class javafx.scene.control.ListView 

通過

Java(TM) SE Runtime Environment (build 1.8.0_25-b17) 

That JRubyFX error was fixed in 1.1.1(其添加的Java 8支持)而引起的。 1.1.0僅支持Java 7(在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.launch