检索JTextField文本值 [英] Retrieve JTextField text value

查看:80
本文介绍了检索JTextField文本值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从JTextField检索文本值,但首先我需要将组件对象(java.awt.Component)强制转换为JTextFiel ...

I'm trying to retrieve the text value from a JTextField but first I need to cast a component object (java.awt.Component) to a 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)中,但是它们是Components,所以这就是我将演员表转换为JTextField的原因.

I'm doing this because I know all the controls of mi page are in "x" (JLabels and JTextField) but they are Components and that's why i'm making the cast to JTextField.

我真的在这里迷路了,我不知道这是否是正确的方法. 感谢您的宝贵时间!

I'm really lost here and i don't know if this is the right way to do it. Thanks for your time!

推荐答案

((JTextComponent) x[i]).getText();应该可以工作.

(只是因为x[i]JTextComponent的实例,但这并不意味着它必然是JTextField.)但是JTextComponent具有.getText(),因此强制转换为JTextComponent应该没问题.

(Just because x[i] is an instance of a JTextComponent, doesn't mean it's neccesarily a JTextField though.) But JTextComponent has a .getText() so casting to JTextComponent should be ok.

这篇关于检索JTextField文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆