2013-05-06 52 views
1

我正在開發一個Java應用程序,並希望在定位某些標籤和TextField時提供一些幫助。在框架上定位GUI對象

這是我的代碼:

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JComboBox; 
import javax.swing.JButton; 
import javax.swing.JLabel; 
import javax.swing.JList; 
import javax.swing.JTextField; 
import java.awt.BorderLayout; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import java.awt.FlowLayout; 

public class AuctionClient 
{ 
public AuctionClient() 
{ 
    JFrame GUIFrame = new JFrame(); 
    JPanel GUIPanel = new JPanel(); 
    JLabel LabelUserName = new JLabel("UserName:"); 
    JTextField TextFieldUserName = new JTextField("        "); 

    JLabel LabelPassword = new JLabel("Password:"); 
    JTextField TextFieldPassword = new JTextField("        ");   

    GUIFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    GUIFrame.setTitle("Auction Client"); 
    GUIFrame.setSize(500,250); 

    GUIFrame.setLocationRelativeTo(null); 

    GUIPanel.add(LabelUserName); 
    GUIPanel.add(TextFieldUserName);   

    GUIPanel.add(LabelPassword); 
    GUIPanel.add(TextFieldPassword);   

    GUIFrame.add(GUIPanel, BorderLayout.NORTH); 
    GUIFrame.setVisible(true); 
} 
} 

與上面的代碼中,LabelPassword和TextFieldPassword是在同一行作爲LabelUsername和TextFieldUsername。我可以請幫助將LabelPassword和TextFieldPassword放在一個新行上。是否可以指定X,Y座標來定位JFrame上的對象?

這裏是展現你的形象如何,目前正在顯示的對象:

enter image description here

http://canning.co.nz/Java/Positioning_Image.png

回答

5

您不應該嘗試使用座標定位組件。而是使用適當的LayoutManager's並使用邏輯條件和約束來定位組件。

下面是使用GridBagLayout一個例子:

import java.awt.BorderLayout; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 

import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JPasswordField; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 

public class AuctionClient { 
    public AuctionClient() { 
     JFrame guiFrame = new JFrame(); 
     JPanel guiPanel = new JPanel(new GridBagLayout()); 
     JLabel userNameLabel = new JLabel("UserName:"); 
     JTextField userNameTextField = new JTextField(30); 

     JLabel passwordLabel = new JLabel("Password:"); 
     JTextField passwordTextField = new JPasswordField(30); 

     guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     guiFrame.setTitle("Auction Client"); 
     guiFrame.setSize(500, 250); 

     guiFrame.setLocationRelativeTo(null); 
     GridBagConstraints labelGBC = new GridBagConstraints(); 
     labelGBC.insets = new Insets(3, 3, 3, 3); 
     GridBagConstraints fieldGBC = new GridBagConstraints(); 
     fieldGBC.insets = new Insets(3, 3, 3, 3); 
     fieldGBC.gridwidth = GridBagConstraints.REMAINDER; 
     guiPanel.add(userNameLabel, labelGBC); 
     guiPanel.add(userNameTextField, fieldGBC); 

     guiPanel.add(passwordLabel, labelGBC); 
     guiPanel.add(passwordTextField, fieldGBC); 

     guiFrame.add(guiPanel, BorderLayout.NORTH); 
     guiFrame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new AuctionClient(); 
      } 
     }); 
    } 
} 
+0

@AndrewThompson沒問題;-) – 2013-05-06 18:09:35

5
  • GridLayout, SpringLayout, GridBagLayout可以做,默認情況下

  • 最簡單coud是GridLayout(2, 2, 10, 10)

    1. 但每JComponents是可調整大小與它的容器

    2. 必須改變爲JLabel的水平alingment(setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);setHorizontalAlignment(JLabel.RIGHT);

    3. 確定(JTextField TextFieldUserName = new JTextField(20))的初始尺寸爲任何的LayoutManager

+2

還要考慮''的GroupLayout,看到[這裏](http://stackoverflow.com/a/8504753/230513)。 – trashgod 2013-05-06 11:30:53

+2

:)................. – mKorbel 2013-05-06 11:32:29

+2

我喜歡你放的點,因爲你不僅可以張貼微笑。 +1 – Maroun 2013-05-06 11:39:54