components[i].getClass()能不能判断?

解决方案 »

  1.   

    zms19740520(风逐流):
      components[i].getClass()可以取出控件的类,即JTextField控件返回的是class javax.swing.JTextField,但是我现在怎么把返回的class与JTextField比较呢?用什么比较运算符呢?== 不行,我试了不行。
    旭日东升:
      instanceof具体怎么用呢?我是个java新手,能不能给出这段代码?怎么判断这个控件是JTextField,然后还有怎么把这个控件转化成JTextField,为了用它的setText(null)这个方法。
      这种方法我在Delphi中用过,好使,但是不知道在java中怎么用?请各位高手指教。
    附Delphi代码如下:
      

  2.   

    for i:=0 to Form1.ComponentCount-1 do
      begin
        if Form1.Components[i] is TEdit
        then (Form1.Components[i] as TEdit).Text:=''
        else if Form1.Components[i] is TLabel
             then (Form1.Components[i] as TLabel).Caption:=''
             else if Form1.Components[i] is TMemo
                  then (Form1.Components[i] as TMemo).Lines.Clear;
      end;就是想取出一个panel中的所有组件,判断这个组件是不是JTextField,如果是,将这个组件转化成JTextField,并设它的setText(null),就这么简单。
    我这样做的目的,就是不用对panel上的所有JTextField都执行setText(null),而用一个循环就可以搞定了。
    我很着急呀。
    各位高手帮帮忙了。
      

  3.   

    你可以用JTextField new 一个,然后都getClass后再对比。我没有试过,这是笨方法
      

  4.   

    呵呵....不给分,这么简单的问题都没有人回答啊.
    if (components[i] instanceof JTextField)
      

  5.   

    用JTextField new 一个:是生成JTextField的一个实例,我要判断的是JTextField这种类型,好像不行。
      

  6.   

    或者
    if (components[i].getClass() == JTextField.class)
      

  7.   

    if (components[i] instanceof JTextField)可以判断了,
    但是还有一个难题,就是实现(JTextField)components[i].setText(null)这个功能呢?
    就是需要把这个组件转换成JTextField,好用它的setText方法,将它的值清空。
      

  8.   

    如果判断是就用(JTextField)components[i].setText("");啊
      

  9.   

    我倒有个办法,你不就是想检索出框架中所有的JTextField然后置空吗,将contentpane中所有组件取出,然后都转型,能转型成功则置其text值为"",不能转型即产生异常则捕捉,然后调到循环的下一次,代码如下,我是菜鸟,所以如果你觉得对就给我加分,我好得积分去问人问题,呵呵,想一起研究就加我msn:[email protected]
    public class Frame1 extends JFrame {
      private JTextField jTextField1 = new JTextField();
      private JTextField jTextField2 = new JTextField();
      private JLabel jLabel1 = new JLabel();
      private JButton jButton1 = new JButton();
      private JButton jButton2 = new JButton();
      private JLabel jLabel2 = new JLabel();
      private JToggleButton jToggleButton1 = new JToggleButton();
      private JRadioButton jRadioButton1 = new JRadioButton();  public Frame1() {
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      private void jbInit() throws Exception {
        jTextField1.setText("jTextField1");
        jTextField1.setBounds(new Rectangle(89, 38, 145, 29));
        this.getContentPane().setLayout(null);
        jTextField2.setText("jTextField2");
        jTextField2.setBounds(new Rectangle(87, 96, 144, 24));
        jLabel1.setText("jLabel1");
        jLabel1.setBounds(new Rectangle(87, 164, 160, 31));
        jButton1.setBounds(new Rectangle(287, 61, 92, 25));
        jButton1.setText("jButton1");
        jButton2.setBounds(new Rectangle(290, 108, 85, 24));
        jButton2.setText("jButton2");
        jLabel2.setText("jLabel2");
        jLabel2.setBounds(new Rectangle(81, 221, 97, 26));
        jToggleButton1.setText("jToggleButton1");
        jToggleButton1.setBounds(new Rectangle(282, 155, 93, 22));
        jRadioButton1.setText("jRadioButton1");
        jRadioButton1.setBounds(new Rectangle(267, 220, 103, 26));
        this.getContentPane().add(jTextField1, null);
        this.getContentPane().add(jTextField2, null);
        this.getContentPane().add(jLabel1, null);
        this.getContentPane().add(jButton1, null);
        this.getContentPane().add(jButton2, null);
        this.getContentPane().add(jLabel2, null);
        this.getContentPane().add(jToggleButton1, null);
        this.getContentPane().add(jRadioButton1, null);
        this.setTitle("test");
        this.setSize(400,300);
        this.setVisible(true);
      }
      public static void main(String[] args){
        Frame1 frame = new Frame1();
        Component[] components = frame.getContentPane().getComponents();
        //注意下面,是处理你想实现功能的地方
        label:for (int i = 0; i < components.length; i++) {
          try{
            ((JTextField)components[i]).setText("");
          }
        catch(Exception e){
           continue label;
        }
        }  }
    }
      

  10.   

    ((JTextField)components[i]).setText("");
      

  11.   

    liuliliujian(丹丹) ( ) :
    我狂faint,
    你这是胡来啊.try & catch不是这样用的.虚拟机都给你气死了.楼主:
    (JTextField)components[i].setText(null)不能用要改成这样啊:
    ((JTextField)components[i]).setText(null)也就是说,和起来的代码是:
    if (components[i] instanceof JTextField){
        ((JTextField)components[i]).setText(null)
    }
      

  12.   

    晕.类型转换的错误
    楼主:
    (JTextField)components[i].setText(null)不能用要改成这样啊:
    ((JTextField)components[i]).setText(null) maowu(猫呜) 的应该是对的.
    至于那个try catch的写法.真是...不推荐用异常来进行流程转换吧
      

  13.   

    各位兄弟姐妹,去这里领分,可惜只有10分,抱歉了,最后一点家产了。
    http://community.csdn.net/Expert/topic/3075/3075093.xml?temp=8.672732E-02
      

  14.   

    也可以到这里来,发表你的见解,来领分,要发表见解的哦。
    http://community.csdn.net/Expert/topic/3054/3054852.xml?temp=.6173212
      

  15.   

    来这里领分了,留下你的联系方式,QQ,MSN等,谢谢了!
    http://community.csdn.net/Expert/topic/3054/3054852.xml?temp=.6173212
      

  16.   

    嘿嘿,不好意思,昨天就是冲着能把他搞定的想法急着写的,能达到目的就ok了,不过上面的都说的对啦,最好还是不要用这种方法,虽然能实现,可以用个简单的判断语句啦
    就是他们说的components[i] instanceof JTextField;就可以判断它是不是JTextField类的实例了