如何在Swing中动态设置现有组件名称

时间:2015-04-30 17:31:03

标签: java swing

我是java初学者...在这里我想要动态更改java swings中现有组件的名称

import javax.swing.*;    
class DemoForm extends JFrame


{
JPanel mainpane=new JPanel();     
JTextField txtfld=new JTextField(5);    
DemoForm()    
{    
// if first_name is string literal type
    //here i want to change txtfld to first_Name    
    //is there any mechanism toconvert txtfld to first_name
    mainpane.add(first_name);
    this.add(mainpane);    
    this.setVisible(true);    
    this.setSize(400,500);  
}   
public static void main(String args[])    
{   
    new DemoForm();
}   

}

2 个答案:

答案 0 :(得分:0)

import javax.swing.*;    
class DemoForm extends JFrame


{
JPanel mainpane=new JPanel();     
JTextField txtfld=new JTextField(5);    
DemoForm()    
{    
    //here i want to change txtfld to first_Name    
    //is there any mechanism toconvert txtfld to first_name
    JTextField first_name = txtfld ; 
    mainpane.add(first_name);
    this.add(mainpane);    
    this.setVisible(true);    
    this.setSize(400,500);  
}   
public static void main(String args[])    
{   
    new DemoForm();
}   
}

表示first_name指向txtfld

答案 1 :(得分:-3)

您使用的是IDE吗? NetBeans具有swing设计界面,您可以将所需内容拖放到窗格中。您可以从那里更改所需的任何显示。