我正在使用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 文件中声明的文本字段检索字符串。任何帮助是极大的赞赏。谢谢!
最佳答案
我认为你应该使用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
还有。看起来该库不再开发了。这是一个危险信号。
关于java - CookSwing - 从文本字段检索字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8032360/