2010-01-27 191 views
1

你好,我想獲得一個JPanel這裏面揮杆佈局:Swing佈局問題

的JLabel - JTextField的 - JComboBox中

的面板大小調整,我想文本域擴大而不是其他兩個。一切都必須保持一致。我嘗試了網格包佈局,但不起作用......或者我不能。想法?

+1

分享您的代碼!這對我們來說會更容易幫助... – nanda 2010-01-27 15:42:47

回答

5

如果你有三個組成部分,並希望一箇中間擴展,比如,你可以使用一個BorderLayout的,把你的JLabel在BorderLayout.WEST,你的JComboBox在BorderLayout.EAST和你想要的擴展(JTextField)在BorderLayout.CENTER

以下是醜陋的,但目的是微乎其微又顯示你想要的行爲:

public class Gotch { 
    public static void main(String[] args) { 
     JFrame main = new JFrame(); 
     JPanel p = new JPanel(); 
     p.setLayout(new BorderLayout()); 
     p.add(new JLabel("test"), BorderLayout.WEST); 
     p.add(new JTextField("growable"), BorderLayout.CENTER); 
     p.add(new JComboBox(), BorderLayout.EAST); 
     main.add(p); 
     main.pack(); 
     main.setVisible(true); 
    } 
} 
+1

我剛剛提出了相同的建議。 GridBagLayouts給你更詳細的靈活性,但他們很難以後重構。 – Stroboskop 2010-01-27 15:53:38

1

擴展Box是一個不錯的選擇:

import java.awt.Dimension; 
import java.awt.EventQueue; 
import javax.swing.Box; 
import javax.swing.BoxLayout; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 

public class MyPanel extends Box { 

    public MyPanel(int axis) { 
     super(axis); 
     this.setPreferredSize(new Dimension(320, 240)); 
     JLabel lb = new JLabel("label"); 
     lb.setAlignmentX(JLabel.CENTER_ALIGNMENT); 
     this.add(lb); 
     JTextField tf = new JTextField("field"); 
     this.add(tf); 
     String [] items = { "One", "Two", "Three" }; 
     JComboBox c = new JComboBox(items); 
     c.setMaximumSize(new Dimension(100, Short.MAX_VALUE)); 
     this.add(c); 
    } 

    private static void create() { 
      JFrame f = new JFrame(); 
      f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
      MyPanel p = new MyPanel(BoxLayout.Y_AXIS); 
      f.add(p); 
      f.pack(); 
      f.setVisible(true); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       create(); 
      } 
     }); 
    } 
} 
3

以爲我會發布一些代碼如何使用GridBagLayout來完成。當你有一些不適合BorderLayout的東西時可能會很有用,這通常會在製作GUI時出現。

public class Main { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     JFrame frame = new JFrame("GridBagLayout Demo"); 
     frame.setLayout(new GridBagLayout()); 

     JLabel label = new JLabel("Demo Label"); 
     JTextField textField = new JTextField("Demo Text"); 
     JComboBox comboBox = new JComboBox(new String[] {"hello", "goodbye", "foo"}); 

     GridBagConstraints cons = new GridBagConstraints(); 
     cons.insets = new Insets(10, 10, 10, 10); 
     frame.add(label, cons); 

     cons.gridx = 1; 
     cons.weightx = 1; 
     cons.weighty = 1; 
     cons.insets = new Insets(10, 0, 10, 10); 
     cons.fill = GridBagConstraints.HORIZONTAL; 

     frame.add(textField, cons); 

     cons.gridx = 2; 
     cons.weightx = 0; 
     cons.weighty = 0; 
     cons.insets = new Insets(10, 0, 10, 10); 
     cons.fill = GridBagConstraints.NONE; 

     frame.add(comboBox, cons); 

     frame.pack(); 
     frame.setVisible(true); 
    } 
}