为什么所有JTextField和JTextArea都显示正确但它不能写在该组件上?

时间:2013-11-15 07:19:41

标签: java swing jtextfield

在以下代码中,组件正确显示但我们无法写入JTextField

public class CurrentOPDDetail extends JInternalFrame
{
    JLabel[] line=new JLabel[10];
    JLabel[] lbl=new JLabel[33];
    JTextField txtName=new JTextField();
    JTextField txtAge=new JTextField();
    JTextField txtSex=new JTextField();
    JTextField txtWeight=new JTextField();

    public CurrentOPDDetail(int n) 
    {
            super("OPD Detail Form",false,false);//Title, Resizable, closable
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            setSize(screenSize.width-500,650);
            setLocation(170,10);    
            setVisible(true);
            setLayout(null);

            lbl[0]=new JLabel("NAME");
            lbl[1]=new JLabel("AGE");
            lbl[2]=new JLabel("SEX");
            lbl[3]=new JLabel("WEIGHT");
            lbl[4]=new JLabel("HISTORY");

                         for(int i=0;i<10;i++)
            {
                line[i]=new JLabel("");
                line[i].setBackground(Color.BLACK);
                line[i].setOpaque(true);
            }


                        lbl[0].setBounds(1, 5, 40, 25);
            txtName.setBounds(41, 5, 200, 25);
            lbl[1].setBounds(262, 5, 30, 25);
            txtAge.setBounds(293, 5, 50, 25);
            lbl[2].setBounds(370, 5, 30, 25);
            txtSex.setBounds(400, 5, 50, 25);
            lbl[3].setBounds(470, 5, 50, 25);
            txtWeight.setBounds(520, 5, 100, 25);

            line[0].setBounds(1, 33, screenSize.width-499, 5);

            lbl[4].setBounds(1, 40, 150, 25);

                        add(line[0]);
            add(lbl[0]);
            add(txtName);
            add(lbl[1]);
            add(txtAge);
            add(lbl[2]);
            add(txtSex);
            add(lbl[3]);
            add(txtWeight);
        }

}

1 个答案:

答案 0 :(得分:3)

只需使用JFramegetContentPane().add()即可。 内部窗口的文本字段可以正常工作:

    JFrame frame = new JFrame();
    frame.getContentPane().add(new CurrentOPDDetail(5));
    frame.setVisible(true);