当我拖放它的尺寸时,右下会有多余的空隙。这是什么原因?import java.awt.*;class WinGrid extends Frame {
private static final long serialVersionUID = 4317426513460347061L;
GridLayout grid;
WinGrid() {
grid = new GridLayout(12, 12);
setLayout(grid);
Label label[][] = new Label[12][12];
for (int i=0; i<12; i++) {
for (int j=0; j<12; j++) {
label[i][j] = new Label();
if (0 == (i+j)%2) {
label[i][j].setBackground(Color.black);
}
else {
label[i][j].setBackground(Color.white);
}
add(label[i][j]);
}
}
setBounds(10, 10, 260, 260);
setVisible(true);
validate();
}
}public class Example7_c {
    public static void main(String args[]) {
     new WinGrid();
    }
}

解决方案 »

  1.   

    因为你大小设置的不协调。setBounds(10, 10, 260, 260); 这里260改成252即可。
      

  2.   

    现在可以了,谢谢!看了下,Box也一样。多出来的pixels,自行显示。
    import java.awt.*;
    import javax.swing.Box;public class Example7_d {
        public static void main(String args[]) {
         new WindowBox();
        }
    }class WindowBox extends Frame {
    Box baseBox, boxV1, boxV2;
    WindowBox() {
    boxV1 = Box.createVerticalBox();
    boxV1.add(new Label("姓名"));
    boxV1.add(Box.createVerticalStrut(8));
    boxV1.add(new Label("EMail"));
    boxV1.add(Box.createVerticalStrut(8));
    boxV1.add(new Label("职业"));

    boxV2 = Box.createVerticalBox();
    boxV2.add(new TextField(12));
    boxV2.add(Box.createVerticalStrut(8));
    boxV2.add(new TextField(12));
    boxV2.add(Box.createVerticalStrut(8));
    boxV2.add(new TextField(12));

    baseBox = Box.createHorizontalBox();
    baseBox.add(boxV1);
    baseBox.add(Box.createVerticalStrut(10));
    baseBox.add(boxV2);
    setLayout(new FlowLayout());
    add(baseBox);
    setBounds(120, 125, 250, 150);
    setVisible(true);
    }
    }
      

  3.   

    g.drawString("单", 6, 16); //为何0 0反而一点不能显示呢?import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;public class Example7_e {
        public static void main(String args[]) {
         new WindowNull();
        }
    }class WindowNull extends Frame {
    private static final long serialVersionUID = -4572488041360047550L;
    WindowNull() {
    setLayout(null);
    MyButton button = new MyButton();
    Panel p = new Panel();
    p.setLayout(null);
    p.setBackground(Color.cyan);
    p.add(button);
    button.setBounds(20, 10, 25, 70);
    add(p);
    p.setBounds(50, 50, 90, 100);
    setBounds(120, 125, 200, 200);
    setVisible(true);
    }
    }class MyButton extends Button implements ActionListener {
    private static final long serialVersionUID = -5448685371256768985L;
    int n = -1;
    MyButton() {
    addActionListener(this);
    }

    public void paint(Graphics g) {
    g.drawString("单", 6, 16);
    g.drawString("击", 6, 36);
    g.drawString("我", 6, 56);
    } @Override
    public void actionPerformed(ActionEvent e) {
    n = (n+1) % 3;
    if (0 == n) {
    setBackground(Color.red);
    }
    else if (1 == n) {
    setBackground(Color.yellow);
    }
    else if (2 == n) {
    setBackground(Color.green);
    }
    }
    }
      

  4.   

    楼主,你要明白GridLayout有空隙是经常的
    因为这个布局是平分所有单元格
    所以当你的这个面板的像素不能被你的行数/列数整除的时候,
    肯定会有空隙的存在
    比如200像素可以平分成50份,每一份4个像素
    但是203个像素要平分成50份,肯定也只能每一单元4个像素,最后还多出3个像素的空白