显示对面的GUI java文本和文本字段

时间:2014-04-01 05:49:55

标签: java swing user-interface layout jlabel

我是java的新手,我花了3  小时试图找出我做错了什么或误解了,我应该得到这个:

enter image description here

但我的代码中的文字显示在textfields

之后
   textPanel = new JPanel();
     textPanel.setLayout(new GridLayout(3,0)); 

     fName = new JTextField( 15 ) ; 
     textPanel.add(fName);
     jlbName = new JLabel ( "Firstname" );
    jlbName.setHorizontalAlignment(JLabel.RIGHT);
    textPanel.add(jlbName);

     lName = new JTextField( 15 ) ; 
    textPanel.add(lName);
    jlbName = new JLabel ( "LastName" );

    jlbName.setHorizontalAlignment(JLabel.RIGHT);
    textPanel.add(jlbName);



    libNo = new JTextField( 15 ) ; 
    textPanel.add(libNo);
       libNo.setEditable(false);
    jlbName = new JLabel ( "Library Number" );

    jlbName.setHorizontalAlignment(JLabel.RIGHT);
    textPanel.add(jlbName);


   add(textPanel,BorderLayout.EAST);
   JButton jbtN = new JButton("Add borrower");
   add(jbtN ,BorderLayout.SOUTH);

1 个答案:

答案 0 :(得分:3)

您正以错误的顺序将组件插入面板。首先插入文本字段,然后插入标签。相反,即代替:

textPanel.add(fName);
...
textPanel.add(jlbName);
...

做的:

textPanel.add(jlbName);
...
textPanel.add(fName);
...