简体   繁体   English

BXML的等效Java代码

[英]Equivalent Java code for BXML

I'm trying to understand how Apache Pivot builds a GUI from it's definition in a BXML file. 我试图了解Apache Pivot如何根据BXML文件中的定义来构建GUI。 I want to make sure that I understand which steps are involved and what is done automatically during these steps. 我想确保我了解涉及哪些步骤以及在这些步骤中自动完成的步骤。 For this, I tried translating a BXML definition for a very simple GUI to pure Java. 为此,我尝试将非常简单的GUI的BXML定义转换为纯Java。 But it seems that layouting is not done the same way (or at all) when following the steps that are described in the BXML Primer . 但是,当按照BXML Primer中描述的步骤进行布局时,似乎并没有以相同的方式(或根本没有)进行布局。

This is the BXML file and the accompanying class to load the file: 这是BXML文件以及用于加载文件的随附类:

example.bxml : example.bxml

<?xml version="1.0" encoding="UTF-8"?>

<Frame xmlns="org.apache.pivot.wtk">
    <TextArea text="Hello World" />
</Frame>

WithBxml.java : WithBxml.java

public final class WithBxml extends Application.Adapter {
    @Override
    public void startup(Display display, Map<String, String> properties) throws Exception {
        Frame frame = (Frame) new BXMLSerializer().readObject(WithBxml.class, "example.bxml");

        frame.open(display);
    }

    public static void main(String[] args) {
        DesktopApplicationContext.main(WithBxml.class, args);
    }
}

This creates the following GUI, which is expected: 这将创建以下预期的GUI:

在此处输入图片说明

I'm trying to use the following code to recreate the same GUI. 我正在尝试使用以下代码重新创建相同的GUI。 But the TextArea is not visible, as can be seen in the following screenshot. 但是TextArea不可见,如以下屏幕截图所示。

WithoutBxml.java : 没有Bxml.java

public final class WithoutBxml extends Application.Adapter {
    @Override
    public void startup(Display display, Map<String, String> properties) throws Exception {
        TextArea textArea = new TextArea();
        textArea.setText("Hello World");

        Frame frame = new Frame();
        frame.add(textArea);
        frame.open(display);
    }

    public static void main(String[] args) {
        DesktopApplicationContext.main(WithoutBxml.class, args);
    }
}

在此处输入图片说明

What do I need to change in the class WithoutBxml to get the same result as with the BXML file? 为了获得与BXML文件相同的结果,我需要在WithoutBxml类中进行WithoutBxml更改?

Instead of invoking frame.add(textArea); 而不是调用frame.add(textArea); , I had to use frame.setContent(textArea); ,我不得不使用frame.setContent(textArea); to add the TextArea to the Frame . TextArea添加到Frame This method is called when loading the BXML file because the Window class is annotated with @DefaultProperty("content") : 加载BXML文件时将调用此方法,因为Window类使用@DefaultProperty("content")注释:

@DefaultProperty("content")
public class Window extends Container {
    ...
}

Because of this, BXMLSerializer is calling setContent() for child elements of the <Window> element in the BXML file. 因此, BXMLSerializer为BXML文件中<Window>元素的子元素调用setContent()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM