如何在Java Gnome / GTK程序中使用Glade UI(.glade文件)? [英] How to use a Glade UI (.glade file) in a Java Gnome/GTK program?

查看:113
本文介绍了如何在Java Gnome / GTK程序中使用Glade UI(.glade文件)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上寻找教程/文档来研究如何在Java Gnome项目中使用Glade中设计的UI,但没有运气。我已经知道如何使用Java Gnome / GTK从代码创建UI。无论如何,我想使用我在Java Gnome / Gtk项目中创建的Glade UI,但我不知道从哪里开始。请告诉我:


  • 我需要安装哪些软件包;


  • 如何将我用Glade(.glade文件)创建的UI与我的Java Gnome / Gtk项目(特别是在Eclipse中)集成;




  • 在此先感谢。



    这就是我的Glade UI的外观(只需点击图片即可看到它):





    这是上面UI的XML代码:

     <?xml version =1.0encoding =UTF-8?> 
    < interface>
    <! - interface-requires gtk + 3.0 - >
    < object class =GtkWindowid =window1>
    < property name =can_focus> False< / property>
    < property name =window_position>中心< / property>
    < child>
    < object class =GtkFixedid =fixed1>
    < property name =visible> True< / property>
    < property name =can_focus> False< / property>
    < child>
    < object class =GtkEntryid =entry1>
    < property name =width_request> 162< / property>
    < property name =height_request> 25< / property>
    < property name =visible> True< / property>
    < property name =can_focus> True< / property>
    < property name =halign> start< / property>
    < property name =valign> end< / property>
    < property name =invisible_char>•< / property>
    < / object>
    <包装>
    < property name =x> 11< / property>
    < property name =y> 49< / property>
    < / packing>
    < / child>
    < child>
    < object class =GtkEntryid =entry2>
    < property name =width_request> 162< / property>
    < property name =height_request> 25< / property>
    < property name =visible> True< / property>
    < property name =can_focus> True< / property>
    < property name =halign> start< / property>
    < property name =valign> end< / property>
    < property name =invisible_char>•< / property>
    < property name =invisible_char_set> True< / property>
    < / object>
    <包装>
    < property name =x> 271< / property>
    < property name =y> 49< / property>
    < / packing>
    < / child>
    < child>
    < object class =GtkLabelid =label>
    < property name =height_request> 25< / property>
    < property name =visible> True< / property>
    < property name =can_focus> False< / property>
    < / object>
    <包装>
    < property name =x> 216< / property>
    < property name =y> 49< / property>
    < / packing>
    < / child>
    < child>
    < object class =GtkButtonid =button>
    < property name =labeltranslatable =yes>计算!!!< / property>
    < property name =use_action_appearance> False< / property>
    < property name =width_request> 84< / property>
    < property name =height_request> 27< / property>
    < property name =visible> True< / property>
    < property name =can_focus> True< / property>
    < property name =receiving_default> True< / property>
    < property name =use_action_appearance> False< / property>
    < / object>
    <包装>
    < property name =x> 181< / property>
    < property name =y> 93< / property>
    < / packing>
    < / child>
    < / object>
    < / child>
    < / object>
    < / interface>


    解决方案

    好的,您需要:




    • 初始GTK库
    • 创建一个Builder对象
    • 检索窗口小部件

    • 显示窗口小部件



    我假设您已经安装了java-gnome库你的Linux系统并将jar导入到你的eclipse项目中(项目属性-java build path - 添加外部jar - /usr/share/java/gtk.jar)。

    示例代码:

      Gtk.init(args); //初始化库
    Builder b = new Builder(); //创建构建器
    b.addFromFile(filename.glade); //从文件
    加载布局窗口w =(Window)b.getObject(myWindowName); //检索一个对象
    w.showAll(); // Show window
    Gtk.main();

    这是它的工作方式。如果你想在一个对象上设置一个监听器,例如一个按钮,你可以按照以前看到的名称检索该对象,然后:

      button.connect(new Clicked() {
    $ b $ @覆盖
    public void onClicked(Button arg0){
    //做你想要的
    }
    });

    它是否适合您?那么,这不适合我。
    问题是我的代码在调用Builder.addFromFile(filename)时抛出一个异常


    $ b


    java.text.ParseException:Invalid对象类型`GtkLabel'

    该错误与树中的第一个元素有关。
    经过深入的研究后,我发现,由于一个未知的bug,你需要在使用它之前预先定义每个小部件:在这种情况下,只需调用

      new Label(); 

    创建生成器之前。



    显然, not 意图以这种方式工作,并且将来会被修复。



    有关此问题的更多信息:



    在java-gnome-developer邮件列表上的线程



    Java-gnome-hackers邮件列表上的线程



    希望这可以帮助...

    I've did a research on the Internet looking for tutorials/documentations to explain me how to use a UI designed in Glade in a Java Gnome project, but no luck. I already know how to create a UI from the code using the Java Gnome/GTK. Anyway, I'd like to use a Glade UI that I've created in a Java Gnome/Gtk project, but I have no idea from where to start. Please tell me:

    • which packages I need to install;

    • how to integrate the UI I've create with Glade (the .glade file) with my Java Gnome/Gtk project (specifically in Eclipse);

    • and give an example.

    Thanks in advance.

    That's how my Glade UI looks (Just click the image to see it bigger):

    This is the XML code of the UI above:

    <?xml version="1.0" encoding="UTF-8"?>
    <interface>
      <!-- interface-requires gtk+ 3.0 -->
      <object class="GtkWindow" id="window1">
        <property name="can_focus">False</property>
        <property name="window_position">center</property>
        <child>
          <object class="GtkFixed" id="fixed1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <object class="GtkEntry" id="entry1">
                <property name="width_request">162</property>
                <property name="height_request">25</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="halign">start</property>
                <property name="valign">end</property>
                <property name="invisible_char">•</property>
              </object>
              <packing>
                <property name="x">11</property>
                <property name="y">49</property>
              </packing>
            </child>
            <child>
              <object class="GtkEntry" id="entry2">
                <property name="width_request">162</property>
                <property name="height_request">25</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="halign">start</property>
                <property name="valign">end</property>
                <property name="invisible_char">•</property>
                <property name="invisible_char_set">True</property>
              </object>
              <packing>
                <property name="x">271</property>
                <property name="y">49</property>
              </packing>
            </child>
            <child>
              <object class="GtkLabel" id="label">
                <property name="height_request">25</property>
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">*</property>
              </object>
              <packing>
                <property name="x">216</property>
                <property name="y">49</property>
              </packing>
            </child>
            <child>
              <object class="GtkButton" id="button">
                <property name="label" translatable="yes">Calculate!!!</property>
                <property name="use_action_appearance">False</property>
                <property name="width_request">84</property>
                <property name="height_request">27</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <property name="use_action_appearance">False</property>
              </object>
              <packing>
                <property name="x">181</property>
                <property name="y">93</property>
              </packing>
            </child>
          </object>
        </child>
      </object>
    </interface>
    

    解决方案

    Ok, you need to:

    • Init GTK library
    • Create a Builder object
    • Retrieve the window widget
    • Display the window widget

    I assume you have already installed the java-gnome library in your Linux system and imported the jar in your eclipse project (project properties - java build path - add external jars - /usr/share/java/gtk.jar).

    A sample code:

    Gtk.init(args); //Init library
    Builder b = new Builder();  //Create builder
    b.addFromFile("filename.glade");  //Load layout from file
    Window w = (Window) b.getObject("myWindowName");  //Retrieve an object
    w.showAll(); //Show window
    Gtk.main();
    

    This is the way it should work. If you want to set a listener on an object, e.g. a button, you retrieve the oject by name as seen before with the window and then:

    button.connect(new Clicked() {
    
        @Override
        public void onClicked(Button arg0) {
            // Do what you want
        }
    });
    

    Is it working for you? Well, it's not for me. The problem was my code was throwing an exception when calling Builder.addFromFile(filename)

    java.text.ParseException: Invalid object type `GtkLabel'

    The error is about the first element in the tree. After a deep research I found that, due to a known bug never fixed, you need to pre-define every widget before using it: in this case just call

     new Label();
    

    before creating the builder.

    This is, obviously, not intended to work in this way, and will be fixed in the future.

    More info on this issue:

    Thread on java-gnome-developer mailing list

    Thread on Java-gnome-hackers mailing list

    Hope this can help...

    这篇关于如何在Java Gnome / GTK程序中使用Glade UI(.glade文件)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆