我正在尝试从 JTextField 检索文本值,但首先我需要将组件对象 (java.awt.Component) 转换为 JTextFiel...
mi代码是这样的
Component[] x = this.getComponents();
for(int i = 0; i < x.length; i++)
{
if (x[i] instanceof JTextComponent)
{
//retrieve text...something like
//(JTextField)x[i].getText();
}
}
我这样做是因为我知道 mi 页面的所有控件都在“x”中(JLabels 和 JTextField),但它们是组件,这就是我将其转换为 JTextField 的原因。
我真的迷失了方向,我不知道这是否是正确的方法。 感谢您的宝贵时间!
最佳答案
I'm really lost here and i don't know if this is the right way to do it. Thanks for your time!
您永远不会被迫将所有代码写在一行上。因此,为了简化您的问题,请简化代码。像这样的东西:
Component component = x[i];
JTextField textField = (JTextField)component;
String text = textField.getText();
这样,如果出现编译错误或其他错误,编译器会指出确切的行。
关于java - 检索 JTextField 文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3885577/