2013-04-15 52 views
0

我是新來的擺動,我懷疑這個問題與BoxLayout有關。我試圖讓一系列文本字段和標籤在幀的頂部依次排列。這是我的代碼:邊界佈局不佔用所有可用空間

public static void main(String[] args) { 
    JFrame frame = new JFrame("New Message"); 
    frame.setSize(100, 100); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JTextArea textArea = new JTextArea(); 
    frame.add(textArea, BorderLayout.CENTER); 

    JPanel list = new JPanel(); 
    list.setLayout(new BoxLayout(list, BoxLayout.Y_AXIS)); 
    frame.add(list, BorderLayout.NORTH); 

    String[] labels = {"To: ", "Cc: ", "Bcc: ", "Subject: "}; 
    for (int i = 0; i < labels.length; i++) { 
     JLabel l = new JLabel(labels[i]); 
     JTextField f = new JTextField(); 
     JPanel p = new JPanel(); 
     p.add(l, BorderLayout.WEST); 
     p.add(f, BorderLayout.CENTER); 
     list.add(p); 
    } 

    frame.pack(); 

    frame.setVisible(true); 

} 

這是結果: enter image description here

我要的是,抄送,密送和視在左邊和文本字段一路佔用其餘的空間。

回答

3

副作用少筆記:

  • 通過在SwingUtilities.invokeLater
  • 包裹UI的初始化開始從EDT你的UI
  • 指定JTextField的列數以及JTextArea
  • 的行和列總是一個好主意
  • 如果您撥打pack()之後,撥打setSize()毫無用處。在一般情況下,忘記任何Swing組件使用setSize()/setLocation/setBounds()(一切都交給LayoutManager的)

GridBagLayout在這裏做了很好的工作。 GroupLayout也可以工作。

見這個例子:

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.JScrollPane; 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 

public class Mail { 

    protected void initUI() { 
     JFrame frame = new JFrame("New Message"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel list = new JPanel(new GridBagLayout()); 
     frame.add(list, BorderLayout.CENTER); 
     GridBagConstraints labelGBC = new GridBagConstraints(); 
     labelGBC.insets = new Insets(3, 3, 3, 3); // Put some space between elements for nicer look 
     labelGBC.anchor = GridBagConstraints.WEST; // Align left within its cell 
     GridBagConstraints fieldGBC = new GridBagConstraints(); 
     fieldGBC.gridwidth = GridBagConstraints.REMAINDER; // Last element of the row 
     fieldGBC.weightx = 1.0; // Cell takes up all extra horizontal space 
     fieldGBC.fill = GridBagConstraints.HORIZONTAL; // Fill the cell horizontally 
     fieldGBC.insets = new Insets(3, 3, 3, 3); // Put some space between elements for nicer look 
     String[] labels = { "To: ", "Cc: ", "Bcc: ", "Subject: " }; 
     for (int i = 0; i < labels.length; i++) { 
      JLabel l = new JLabel(labels[i]); 
      JTextField f = new JTextField(50); 
      list.add(l, labelGBC); 
      list.add(f, fieldGBC); 
     } 
     GridBagConstraints taGBC = new GridBagConstraints(); 
     taGBC.gridwidth = 2; 
     taGBC.weightx = 1.0; // Cell takes up all extra horizontal space 
     taGBC.weighty = 1.0; // Cell takes up all extra vertical space 
     taGBC.fill = GridBagConstraints.BOTH; // Fill cell in both direction 
     taGBC.insets = new Insets(3, 3, 3, 3); // Put some space between elements for nicer look 
     JTextArea textArea = new JTextArea(10, 80); 
     list.add(new JScrollPane(textArea), taGBC); 

     frame.pack(); 

     frame.setVisible(true); 
    } 

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

    } 
} 

而結果

enter image description here

1

嘗試java.awt.FlowLayout以查看一個組件旁邊的組件。

+1

'list'&'p'已經有了'FlowLayout'。 – trashgod

1

你可以嘗試設置BorderLayout每個JPanel p

public static void main(String[] args) { 
    JFrame frame = new JFrame("New Message"); 
    frame.setSize(100, 100); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JTextArea textArea = new JTextArea(); 
    frame.add(textArea, BorderLayout.CENTER); 

    JPanel list = new JPanel(); 
    list.setLayout(new BoxLayout(list, BoxLayout.Y_AXIS)); 
    frame.add(list, BorderLayout.NORTH); 

    String[] labels = {"To: ", "Cc: ", "Bcc: ", "Subject: "}; 
    for (int i = 0; i < labels.length; i++) { 
     JLabel l = new JLabel(labels[i]); 
     JTextField f = new JTextField(); 
     JPanel p = new JPanel(new BorderLayout()); 
     p.add(l, BorderLayout.WEST); 
     p.add(f, BorderLayout.CENTER); 
     list.add(p); 
    } 

    frame.pack(); 

    frame.setVisible(true); 

} 
+0

不起作用,標籤不再與文本字段對齊。 –

+0

編輯源代碼以對齊文本字段和標籤。 – Teetoo

相關問題