2010-01-27 158 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); 
    } 
}