2014-04-01 59 views
0

我新在Java GUI,我花了3 小時試圖找出我做錯了或誤解了,我應該得到這樣的:GUI的Java文本和文本框相反顯示

enter image description here

但我的代碼中的文本顯示在文本字段後

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); 
+0

使用適當的佈局。 – Rahul

+0

你能更具體嗎? –

回答

3

您正在按照錯誤順序將組件插入到面板中。您首先插入文本字段,然後插入標籤。反其道而行之,即代替:

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

做:

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