2011-11-07 53 views
1

我使用CookSwing(http://cookxml.yuanheng.org/cookswing/)构建Java Swing UI,但该网站没有很多信息。我被困在试图让我的Java类从表单中的文本字段中检索字符串(用XML声明)。这应该很容易,但我没有尝试过。有没有人有这样的经验?下面是Java类:CookSwing - 从文本字段检索字符串

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

我想你应该使用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 

也。看起来库不再被开发。那是一面红旗。

+0

是的,我可以在XML文件中给文本字段添加一个ID或一个名称,但是如何使用它来引用来自Java类的文本字段? – Mike

+0

@Mike,用你的例子编辑我的答案。 –

+0

完美运作。谢谢,戒指! – Mike