初学JAVA,在做一个界面练习GridLayout里面的组件会按照窗体大小的变化而变化,并且是平均分配单元格的,但是在大部分情况下,并不能完全平均分配窗体,所以最后一个组件后面必定会出现空隙,有什么办法能让这空隙被组件填满吗??测试代码如下:
public class Test extends JFrame {
private static final long serialVersionUID=20101231L;
public Test() {
Container container=this.getContentPane();
container.setBackground(Color.RED);

this.setSize(265, 500);

JPanel panelA=new JPanel(new GridLayout(1,4));
panelA.setOpaque(false);
container.add(panelA,BorderLayout.NORTH);

JLabel labelA=new JLabel("aa");
labelA.setOpaque(true);
labelA.setBackground(Color.LIGHT_GRAY);
labelA.addMouseListener(new TestListener());
panelA.add(labelA);

JLabel labelB=new JLabel("bb");
labelB.setOpaque(true);
labelB.setBackground(Color.LIGHT_GRAY);
labelB.addMouseListener(new TestListener());
panelA.add(labelB);

JLabel labelC=new JLabel("cc");
labelC.setOpaque(true);
labelC.setBackground(Color.LIGHT_GRAY);
labelC.addMouseListener(new TestListener());
panelA.add(labelC);

JLabel labelD=new JLabel("dd");  //此label后面会有非常小的一段背景色显示出来,有什么好的方法能让这几个label横向填满窗体吗?
labelD.setOpaque(true);
labelD.setBackground(Color.LIGHT_GRAY);
labelD.addMouseListener(new TestListener());
panelA.add(labelD);

JPanel panelB=new JPanel();
panelB.setBackground(Color.WHITE);
container.add(panelB, BorderLayout.CENTER);

this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class TestListener extends MouseAdapter {
public void mouseEntered(MouseEvent e) {
JLabel label=(JLabel)e.getSource();
label.setBackground(Color.GRAY);
}
public  void mouseExited(MouseEvent e) {
JLabel label=(JLabel)e.getSource();
label.setBackground(Color.LIGHT_GRAY);
}
}
public static void main(String[] args) {
new Test();
}
}

解决方案 »

  1.   

    楼主真犀利,以前还没真注意到,试了下果然是,不过在随便拖动JFrame大小时有时候那个小空隙没出现。。
      

  2.   

    没出现应该是因为刚好能平均分吧…………我这样算不算转牛角尖啊= =,这个问题想了很久,然后还试了一下SpringLayout,发现还是有小空隙,不过这空隙跑到第一个组件后面了= =
      

  3.   

    GridLayoutpublic GridLayout(int rows,
                      int cols,
                      int hgap,
                      int vgap)    创建具有指定行数和列数的网格布局。给布局中的所有组件分配相等的大小。    此外,将水平和垂直间距设置为指定值。水平间距将置于列与列之间。将垂直间距将置于行与行之间。    rows 和 cols 中的一个可以为零(但不能两者同时为零),这表示可以将任何数目的对象置于行或列中。    所有 GridLayout 构造方法都服从这一规定。    参数:
            rows - 该 rows 具有表示任意行数的值零
            cols - 该 cols 具有表示任意列数的值零
            hgap - 水平间距
            vgap - 垂直间距 
        抛出:
            IllegalArgumentException - 如果将 rows 和 cols 的值都设置为零设定的合适 就行
      

  4.   

    楼主需要相通一个问题,
    就是GridLayout是一个把所有格子平分成一样大小的布局
    然后还有一点,就是一个控件在屏幕上的展示是以像素点为基本单位的所以,如果当一个版面的长度(或宽度),无法被横向(或纵向)的组件数量整除的时候
    就必然会有空隙,因为GridLayout必须保证所有的格子一样大不希望有空隙,楼主可以尝试GridBagLayout
      

  5.   

    解决解决~~TO sunyiz:GridBagLayout我也是试过的,但是还是一样的,其实我如果用GridBagLayout平均分4个单元格,就是GridLayout。除了weightX=25还有什么更好的方法吗?我试了很多布局发现都是无法正好的平分单元格,所以最后还是用了NULL= =测试代码如下:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Test2 extends JFrame {
        private static final long serialVersionUID=20101231L;
        public Test2() {
            Container container=this.getContentPane();
            container.setBackground(Color.RED);
            
            this.setSize(265, 500);
            
            final JPanel panelA=new JPanel(null);
            panelA.setPreferredSize(new Dimension(0,30));
            panelA.setOpaque(false);
            container.add(panelA,BorderLayout.NORTH);
            
            final JLabel labelA=new JLabel("aa");
            labelA.setOpaque(true);
            labelA.setBackground(Color.LIGHT_GRAY);
            labelA.addMouseListener(new TestListener());
            panelA.add(labelA);
            
            final JLabel labelB=new JLabel("bb");
            labelB.setOpaque(true);
            labelB.setBackground(Color.LIGHT_GRAY);
            labelB.addMouseListener(new TestListener());
            panelA.add(labelB);
            
            final JLabel labelC=new JLabel("cc");
            labelC.setOpaque(true);
            labelC.setBackground(Color.LIGHT_GRAY);
            labelC.addMouseListener(new TestListener());
            panelA.add(labelC);
            
            final JLabel labelD=new JLabel("dd");
            labelD.setOpaque(true);
            labelD.setBackground(Color.LIGHT_GRAY);
            labelD.addMouseListener(new TestListener());
            panelA.add(labelD);
            
            panelA.addComponentListener(new ComponentAdapter() {
             public void componentResized(ComponentEvent e) {
             int width=panelA.getSize().width/4;
             int residue=panelA.getSize().width%4;
             labelA.setBounds(0, 0, width, 30);
             labelB.setBounds(width, 0, width, 30);
             labelC.setBounds(width*2, 0, width, 30);
             labelD.setBounds(width*3, 0, width+residue, 30);
             }
            });
            
            JPanel panelB=new JPanel();
            panelB.setBackground(Color.WHITE);
            container.add(panelB, BorderLayout.CENTER);
            
            this.setVisible(true);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        private class TestListener extends MouseAdapter {
            public void mouseEntered(MouseEvent e) {
                JLabel label=(JLabel)e.getSource();
                label.setBackground(Color.GRAY);
            }
            public  void mouseExited(MouseEvent e) {
                JLabel label=(JLabel)e.getSource();
                label.setBackground(Color.LIGHT_GRAY);
            }
        }
        public static void main(String[] args) {
            new Test2();
        }
    }
      

  6.   

    解决解决~~TO sunyiz:GridBagLayout我也是试过的,但是还是一样的,其实我如果用GridBagLayout平均分4个单元格,就是GridLayout。除了weightX=25还有什么更好的方法吗?我试了很多布局发现都是无法正好的平分单元格,所以最后还是用了NULL= =测试代码如下:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Test2 extends JFrame {
        private static final long serialVersionUID=20101231L;
        public Test2() {
            Container container=this.getContentPane();
            container.setBackground(Color.RED);
            
            this.setSize(265, 500);
            
            final JPanel panelA=new JPanel(null);
            panelA.setPreferredSize(new Dimension(0,30));
            panelA.setOpaque(false);
            container.add(panelA,BorderLayout.NORTH);
            
            final JLabel labelA=new JLabel("aa");
            labelA.setOpaque(true);
            labelA.setBackground(Color.LIGHT_GRAY);
            labelA.addMouseListener(new TestListener());
            panelA.add(labelA);
            
            final JLabel labelB=new JLabel("bb");
            labelB.setOpaque(true);
            labelB.setBackground(Color.LIGHT_GRAY);
            labelB.addMouseListener(new TestListener());
            panelA.add(labelB);
            
            final JLabel labelC=new JLabel("cc");
            labelC.setOpaque(true);
            labelC.setBackground(Color.LIGHT_GRAY);
            labelC.addMouseListener(new TestListener());
            panelA.add(labelC);
            
            final JLabel labelD=new JLabel("dd");
            labelD.setOpaque(true);
            labelD.setBackground(Color.LIGHT_GRAY);
            labelD.addMouseListener(new TestListener());
            panelA.add(labelD);
            
            panelA.addComponentListener(new ComponentAdapter() {
             public void componentResized(ComponentEvent e) {
             int width=panelA.getSize().width/4;
             int residue=panelA.getSize().width%4;
             labelA.setBounds(0, 0, width, 30);
             labelB.setBounds(width, 0, width, 30);
             labelC.setBounds(width*2, 0, width, 30);
             labelD.setBounds(width*3, 0, width+residue, 30);
             }
            });
            
            JPanel panelB=new JPanel();
            panelB.setBackground(Color.WHITE);
            container.add(panelB, BorderLayout.CENTER);
            
            this.setVisible(true);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        private class TestListener extends MouseAdapter {
            public void mouseEntered(MouseEvent e) {
                JLabel label=(JLabel)e.getSource();
                label.setBackground(Color.GRAY);
            }
            public  void mouseExited(MouseEvent e) {
                JLabel label=(JLabel)e.getSource();
                label.setBackground(Color.LIGHT_GRAY);
            }
        }
        public static void main(String[] args) {
            new Test2();
        }
    }
      

  7.   

    解决解决~~TO sunyiz:GridBagLayout我也是试过的,但是还是一样的,其实我如果用GridBagLayout平均分4个单元格,就是GridLayout。除了weightX=25还有什么更好的方法吗?我试了很多布局发现都是无法正好的平分单元格,所以最后还是用了NULL= =测试代码如下:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Test2 extends JFrame {
        private static final long serialVersionUID=20101231L;
        public Test2() {
            Container container=this.getContentPane();
            container.setBackground(Color.RED);
            
            this.setSize(265, 500);
            
            final JPanel panelA=new JPanel(null);
            panelA.setPreferredSize(new Dimension(0,30));
            panelA.setOpaque(false);
            container.add(panelA,BorderLayout.NORTH);
            
            final JLabel labelA=new JLabel("aa");
            labelA.setOpaque(true);
            labelA.setBackground(Color.LIGHT_GRAY);
            labelA.addMouseListener(new TestListener());
            panelA.add(labelA);
            
            final JLabel labelB=new JLabel("bb");
            labelB.setOpaque(true);
            labelB.setBackground(Color.LIGHT_GRAY);
            labelB.addMouseListener(new TestListener());
            panelA.add(labelB);
            
            final JLabel labelC=new JLabel("cc");
            labelC.setOpaque(true);
            labelC.setBackground(Color.LIGHT_GRAY);
            labelC.addMouseListener(new TestListener());
            panelA.add(labelC);
            
            final JLabel labelD=new JLabel("dd");
            labelD.setOpaque(true);
            labelD.setBackground(Color.LIGHT_GRAY);
            labelD.addMouseListener(new TestListener());
            panelA.add(labelD);
            
            panelA.addComponentListener(new ComponentAdapter() {
             public void componentResized(ComponentEvent e) {
             int width=panelA.getSize().width/4;
             int residue=panelA.getSize().width%4;
             labelA.setBounds(0, 0, width, 30);
             labelB.setBounds(width, 0, width, 30);
             labelC.setBounds(width*2, 0, width, 30);
             labelD.setBounds(width*3, 0, width+residue, 30);
             }
            });
            
            JPanel panelB=new JPanel();
            panelB.setBackground(Color.WHITE);
            container.add(panelB, BorderLayout.CENTER);
            
            this.setVisible(true);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        private class TestListener extends MouseAdapter {
            public void mouseEntered(MouseEvent e) {
                JLabel label=(JLabel)e.getSource();
                label.setBackground(Color.GRAY);
            }
            public  void mouseExited(MouseEvent e) {
                JLabel label=(JLabel)e.getSource();
                label.setBackground(Color.LIGHT_GRAY);
            }
        }
        public static void main(String[] args) {
            new Test2();
        }
    }
      

  8.   


    GridBagLayout,想要平分,
    所有的weightX全一样就行了,
    比如都是1就行,不需要用100来平分
    为了解决上面说长、宽无法整除时的空隙问题
    你必须要牺牲GridBagLayout中的某行和某一列,
    让他们来完成“撑满”整个面板的任务
    这时就有两个参数可以用上了,就是ipadx和ipady
    它们分别表示最小宽度和高度,
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class Test2 extends JFrame {
    private static final long serialVersionUID = 20101231L; public Test2() {
    Container container = this.getContentPane();
    container.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    container.setBackground(Color.RED);
    this.setSize(265, 500); JPanel panelA = new JPanel();
    panelA.setLayout(new GridBagLayout());
    panelA.setPreferredSize(new Dimension(0, 30));
    panelA.setOpaque(false);
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1;
    c.weighty = 1;
    c.gridy = 0;
    c.ipady = 30;
    container.add(panelA, c); JPanel panelB = new JPanel();
    panelB.setBackground(Color.WHITE);
    c.weighty = 30;
    c.gridy = 1;
    c.ipady = 1500;
    container.add(panelB, c);

    JLabel labelA = new JLabel("aa");
    labelA.setOpaque(true);
    labelA.setBackground(Color.LIGHT_GRAY);
    labelA.addMouseListener(new TestListener());
    c.weighty = 1;
    c.gridy = 0;
    c.gridx = 0;
    c.ipady = 30;
    c.ipadx = 1000;
    panelA.add(labelA, c); JLabel labelB = new JLabel("bb");
    labelB.setOpaque(true);
    labelB.setBackground(Color.LIGHT_GRAY);
    labelB.addMouseListener(new TestListener());
    c.gridx = 1;
    panelA.add(labelB, c); JLabel labelC = new JLabel("cc");
    labelC.setOpaque(true);
    labelC.setBackground(Color.LIGHT_GRAY);
    labelC.addMouseListener(new TestListener());
    c.gridx = 2;
    panelA.add(labelC, c); JLabel labelD = new JLabel("dd");
    labelD.setOpaque(true);
    labelD.setBackground(Color.LIGHT_GRAY);
    labelD.addMouseListener(new TestListener());
    c.gridx = 3;
    panelA.add(labelD, c); this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    } private class TestListener extends MouseAdapter {
    public void mouseEntered(MouseEvent e) {
    JLabel label = (JLabel) e.getSource();
    label.setBackground(Color.GRAY);
    }
    public void mouseExited(MouseEvent e) {
    JLabel label = (JLabel) e.getSource();
    label.setBackground(Color.LIGHT_GRAY);
    }
    } public static void main(String[] args) {
    new Test2();
    }
    }