java网格吸附_java网格布局怎样才会使组件之间紧密贴合

展开全部

package aTest;

import java.awt.*;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

public class test extends JFrame {

// JFrame f=new JFrame("迷宫");

int a[][] = { { 2, 0, 1, 0, 0, 0, 1, 0 }, { 0, 0, 1, 0, 0, 0, 1, 0 },

62616964757a686964616fe78988e69d8331333337373730{ 0, 0, 1, 0, 1, 1, 0, 0 }, { 0, 1, 1, 1, 0, 0, 0, 0 },

{ 0, 0, 0, 1, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 1, 0, 1 },

{ 0, 1, 1, 1, 1, 0, 0, 1 }, { 1, 1, 0, 0, 0, 1, 0, 1 },

{ 1, 1, 0, 0, 0, 0, 0, 3 } };

test(String title) {

super(title);

//Container c = getContentPane();

//setLayout(new GridLayout(8, 9, 50, 50));

GridLayout grid=new GridLayout(8,9);

grid.setHgap(5);

//grid.setRows(5);

grid.setVgap(5);

setLayout(grid);

for (int h = 0; h < 9; h++) {

for (int g = 0; g < 8; g++) {

switch (a[h][g]) {

case 0:

JButton p = new JButton();

p.setBackground(Color.red);

add(p);

break;

case 1:

JButton q = new JButton();

q.setBackground(Color.blue);

add(q);

break;

case 2:

JButton m = new JButton("入口");

m.setBackground(Color.green);

add(m);

break;

case 3:

JButton n = new JButton("出口");

n.setBackground(Color.green);

add(n);

break;

}

}

}

setVisible(true);

pack();

setLocation(450, 200);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public static void main(String args[]) {

test t = new test("迷宫");

}

}

0ece85da1777973ee81edd312955d2c3.png

设置表格的高度和宽度

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值