编程语言
首页 > 编程语言> > 如何在Java Gnome / GTK程序中使用Glade UI(.glade文件)?

如何在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的外观(只需点击图片即可看到更大):

enter image description here

这是上面UI的XML代码:

<?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>

解决方法:

好的,你需要:

> Init GTK库
>创建一个Builder对象
>检索窗口小部件
>显示窗口小部件

我假设您已经在Linux系统中安装了java-gnome库并在eclipse项目中导入了jar(项目属性 – java构建路径 – 添加外部jar – /usr/share/java/gtk.jar).

示例代码:

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();

这是它应该工作的方式.如果要在对象上设置侦听器,例如按钮,您可以按照之前的窗口检索对象,然后:

button.connect(new Clicked() {

    @Override
    public void onClicked(Button arg0) {
        // Do what you want
    }
});

它适合你吗?嗯,这不适合我.
问题是我的代码在调用Builder.addFromFile(filename)时抛出异常

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

错误是关于树中的第一个元素.
经过深入研究后,我发现由于已知的bug从未修复过,您需要在使用之前预先定义每个小部件:在这种情况下只需调用

 new Label();

在创建构建器之前.

显然,这不是以这种方式工作,并将在未来修复.

有关此问题的更多信息:

Thread on java-gnome-developer mailing list

Thread on Java-gnome-hackers mailing list

希望这可以帮助……

标签:ubuntu,java,user-interface,gtk,glade
来源: https://codeday.me/bug/20190529/1180040.html