为什么我用gridbaglayout设置组件的位置,全聚集在panel中央位置,我想从顶端开始布局,我看了帮助是和权重有关,但我不知道怎么设置。请高手们指点一二!

解决方案 »

  1.   

    用于确定将组件置于何处(在显示区域中)。可能的值有两种:相对和绝对。相对值的解释是相对于容器的 ComponentOrientation 属性,而绝对值则不然。有效值有: 绝对值
      
    GridBagConstraints.NORTH 
    GridBagConstraints.SOUTH 
    GridBagConstraints.WEST 
    GridBagConstraints.EAST 
    GridBagConstraints.NORTHWEST 
    GridBagConstraints.NORTHEAST 
    GridBagConstraints.SOUTHWEST 
    GridBagConstraints.SOUTHEAST 
    GridBagConstraints.CENTER (the default 
    相对值
    GridBagConstraints.PAGE_START 
    GridBagConstraints.PAGE_END 
    GridBagConstraints.LINE_START 
    GridBagConstraints.LINE_END 
    GridBagConstraints.FIRST_LINE_START 
    GridBagConstraints.FIRST_LINE_END 
    GridBagConstraints.LAST_LINE_START 
    GridBagConstraints.LAST_LINE_END 
      

  2.   


    恩 恩 恩      dawn大哥说的有道理我感觉这个布局管理器  过于细致了   以至于  有的时候还不如用定坐标来得快 
      

  3.   

    通常是body才用gridbaylayout吧,顶部和底部我通常用 BoxLayout, 打横的那种
      

  4.   

    通常的布局都是setbound直接设置坐标的吗
      

  5.   

    GridBagLayout还是不错的,很省事。
    最好自己封装个方法来使用这个布局,否则要设置的属性过多。
    通常来说自己只关心控件的行列位置和行列填充。
    public static GridBagConstraints getGridBagConstraints(int row, int col, boolean hFill, boolean vFill) {)
    方法内部根据参数把gridx,gridy和行列填充设置好,
    其他的属性比如weightx,weighty等,设置个自己项目中通用的量吧。布局时如下调用:
    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());
    panel.add(new JTextField(), getGridBagConstraints(1, 1, true, false));
    上面这句的意思就是把一个text添加到panel的1行1列位置,横向填充,纵向不填充。ps:代码直接在回复里写的,如果有拼写错误请无视
      

  6.   

    setbound是一种方法,还可以用BorderFactory来设置边框的宽度,等等
      

  7.   

    GridBagLayout默认的就是居中。要想不居中,必需要加weightx,weighty.你试试,只要加上去那两个属性后,
    可以把控件相互扯开。
      

  8.   

        setBounds(int,int,int,int)精确定位
      

  9.   

    使用setBounds有不好的地方 当页面大小改变时 就会很难看啊
      

  10.   

    强烈建议使用BorderLayout和JPanel 及其他的布局管理器综合起来,可以实现很多好的页面效果 给一个小的例子,用来模拟QQ群聊的面板:
    package test;import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.GridLayout;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;public class TestGridBag extends JFrame { private JTextArea ta1 = new JTextArea(); private JTextArea ta2 = new JTextArea(1, 10); private JTextArea ta3 = new JTextArea(5, 20); private JPanel menber = new JPanel(); private JScrollPane js1 = new JScrollPane(ta1); private JScrollPane js2 = new JScrollPane(ta2); private JScrollPane js3 = new JScrollPane(ta3); public TestGridBag() {
    JPanel jpm = new JPanel(new BorderLayout());
    JPanel jpl = new JPanel(new GridLayout(2, 1));
    getContentPane().add(jpm);
    jpm.add(js1);
    jpm.add(js3, BorderLayout.SOUTH);

    add(jpl, BorderLayout.EAST);
    JPanel jptop = new JPanel(new BorderLayout());
    jptop.add(new JLabel("群公告:"), BorderLayout.NORTH);
    jptop.add(js2);
    jpl.add(jptop);
    jpl.add(menber);
    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(1);
    }
    }); setTitle("TT2009 硅谷矿机群");
    setSize(400, 400);
    setVisible(true);
    } public static void main(String[] args) {
    new TestGridBag();
    }}
      

  11.   

    加上两个属性:weightx,weighty, 然后再定位例如:GridBagConstraints.NORTHWEST这样一定可以