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),而用一个循环就可以搞定了。 我很着急呀。 各位高手帮帮忙了。
你可以用JTextField new 一个,然后都getClass后再对比。我没有试过,这是笨方法
呵呵....不给分,这么简单的问题都没有人回答啊. if (components[i] instanceof JTextField)
用JTextField new 一个:是生成JTextField的一个实例,我要判断的是JTextField这种类型,好像不行。
或者 if (components[i].getClass() == JTextField.class)
if (components[i] instanceof JTextField)可以判断了, 但是还有一个难题,就是实现(JTextField)components[i].setText(null)这个功能呢? 就是需要把这个组件转换成JTextField,好用它的setText方法,将它的值清空。
components[i].getClass()可以取出控件的类,即JTextField控件返回的是class javax.swing.JTextField,但是我现在怎么把返回的class与JTextField比较呢?用什么比较运算符呢?== 不行,我试了不行。
旭日东升:
instanceof具体怎么用呢?我是个java新手,能不能给出这段代码?怎么判断这个控件是JTextField,然后还有怎么把这个控件转化成JTextField,为了用它的setText(null)这个方法。
这种方法我在Delphi中用过,好使,但是不知道在java中怎么用?请各位高手指教。
附Delphi代码如下:
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),而用一个循环就可以搞定了。
我很着急呀。
各位高手帮帮忙了。
if (components[i] instanceof JTextField)
if (components[i].getClass() == JTextField.class)
但是还有一个难题,就是实现(JTextField)components[i].setText(null)这个功能呢?
就是需要把这个组件转换成JTextField,好用它的setText方法,将它的值清空。
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;
}
} }
}
我狂faint,
你这是胡来啊.try & catch不是这样用的.虚拟机都给你气死了.楼主:
(JTextField)components[i].setText(null)不能用要改成这样啊:
((JTextField)components[i]).setText(null)也就是说,和起来的代码是:
if (components[i] instanceof JTextField){
((JTextField)components[i]).setText(null)
}
楼主:
(JTextField)components[i].setText(null)不能用要改成这样啊:
((JTextField)components[i]).setText(null) maowu(猫呜) 的应该是对的.
至于那个try catch的写法.真是...不推荐用异常来进行流程转换吧
http://community.csdn.net/Expert/topic/3075/3075093.xml?temp=8.672732E-02
http://community.csdn.net/Expert/topic/3054/3054852.xml?temp=.6173212
http://community.csdn.net/Expert/topic/3054/3054852.xml?temp=.6173212
就是他们说的components[i] instanceof JTextField;就可以判断它是不是JTextField类的实例了