当我拖放它的尺寸时,右下会有多余的空隙。这是什么原因?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();
}
}
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();
}
}
解决方案 »
- swing类中表的问题
- 怎么才可以在上传资源时上传超过10M的东西啊
- 请问Java中两个方法的用法?
- 测CPU 主频,型号,使用率,风扇转速,温度,内存大小的这些软件的实现原来是什么 谢谢
- 关于在editplus中编辑java
- 急!用jdbc连接sql2000
- 如何知道操作系统是windows 还是redhat linux,以及如何调用linux的命令?
- JCreator的JDK profiles如何设置?
- KeyEvent如何响应中文词组的键盘输入?
- Jave 真的什么都能做吗?它有没有Delphi 方便,我说的是搞数据库方面
- 请帮忙 jsp如果调用ocx里面的方法!急。。。
- 父子类方法调用
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);
}
}
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);
}
}
}
因为这个布局是平分所有单元格
所以当你的这个面板的像素不能被你的行数/列数整除的时候,
肯定会有空隙的存在
比如200像素可以平分成50份,每一份4个像素
但是203个像素要平分成50份,肯定也只能每一单元4个像素,最后还多出3个像素的空白