2012-02-16 70 views
3

美好的一天,請想知道如何減少下圖中標籤和文本框之間的空格,以及如何在標籤和框架之間創建一些空格。謝謝。減少GridLayout Java Swing中的空間

login page

我給這家代碼:

private void initUI(JFrame parent) { 
    // private void initUI() { 
    myPanel = new JPanel(new GridLayout(3,2,1,1)); 
    buttons_panel = new JPanel(new FlowLayout()); 

    username_label = new JLabel("Username: "); 
    password_label = new JLabel("Password: "); 
    username = new JTextField(20); 
    password = new JPasswordField(20); 

    ok = new JButton("Ok"); 
    ok.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent e) { 

     } 
    }); 

    cancel = new JButton("Cancel"); 
    cancel.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent e) { 
      dispose(); 
     } 
    }); 

    myPanel.add(username_label); 
    myPanel.add(username); 
    myPanel.add(password_label); 
    myPanel.add(password); 

    buttons_panel.add(ok); 
    buttons_panel.add(cancel); 

    getContentPane().add(myPanel, BorderLayout.CENTER); 
    getContentPane().add(buttons_panel, BorderLayout.PAGE_END); 

    pack(); 
    setResizable(false); 
    setLocationRelativeTo(parent); 
} 

我應該使用的GridBagLayout這個替代..

回答

2

您還可以使用GridLayouthgaphgap參數,並添加匹配,空Border,如圖所示here

+0

家庭聯繫:-) – mKorbel 2012-02-16 12:45:25