CookSwing - 从文本字段中检索字符串

时间:2011-11-07 03:09:23

标签: java swing

我正在使用CookSwing(http://cookxml.yuanheng.org/cookswing/)来构建Java Swing UI,但该网站没有很多信息。我试图让我的Java类从表单中的文本字段(以XML格式声明)中检索字符串。这应该很容易做到,但我尝试过的任何工作都没有。有没有人有这方面的经验?这是Java类:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import cookxml.cookswing.CookSwing;

public final class CookSwingForm
{
  // Listener for the Quit button
  public ActionListener exitAction = new ActionListener()
  {
    public void actionPerformed (ActionEvent e)
    {
      System.exit(0);
    }
  };

  public CookSwingForm()
  {
    CookSwing cookSwing = new CookSwing(this);
    cookSwing.render("sampleform.xml").setVisible(true);
  }

  public static void main(String[] args)
  {
    new CookSwingForm();
  }
}

这是XML文件(“sampleform.xml”):

<frame title="Sample Form" size="300,70"
       defaultcloseoperation="EXIT_ON_CLOSE">
  <borderlayout>
    <constraint location="West">
       <textfield columns="20" />
    </constraint>
    <constraint location="East">
      <button text="Quit" actionlistener="exitAction" />
    </constraint>
  </borderlayout>
</frame>

我只需要让Java类从XML文件中声明的文本字段中检索字符串。任何帮助是极大的赞赏。谢谢!

1 个答案:

答案 0 :(得分:1)

我认为您应该使用id属性,然后将其用作文本字段的变量名称。

xml看起来像:<textfield id="box1" />

以下是您需要做的事情:

JTextField txtField = (JTextField) cookSwing.getId("box1").object;
//now,set some text 
txtField.setText("Blah!");
//or get some text as you may wish

另外。看起来不再开发库了。那是一个红旗。