java两个按钮间距取消,JButton列之间的间距

I am working on a simple GUI where there is an isle between the first two columns and the next two columns of JButtons. The code looks as follows:

JPanel panel = new JPanel(new GridLayout(50, 4));

JScrollPane scrollable = new JScrollPane(panel);

for (int row = 0; row < rows; row++) {

for (int column = 0; column < columns; column++) {

JButton button = new JButton("Row " + row + " seat " + column);

panel.add(button);

}

}

Current Look

How do I add an isle in the middle between the first two and last two columns using java swing?

解决方案

Use two panels...

You could use two panels (for the seats) and one of the isle, for example...

JPanel left = new JPanel(new GridLayout(0, 2));

JPanel isle = new JPanel();

JPanel right = new JPanel(new GridLayout(0, 2));

for (int row = 0; row < 10; row++) {

for (int col = 0; col < 4; col++) {

JButton btn = new JButton("Row " + row + " seat " + col);

if (col < 2) {

left.add(btn);

} else {

right.add(btn);

}

}

}

setLayout(new GridLayout(1, 3));

add(left);

add(isle);

add(right);

J2WG8.png

Use a "filler" component...

You could place a "filler" component between the 2nd and 3rd columns...

K6aa1.png

setLayout(new GridLayout(0, 5));

for (int row = 0; row < 10; row++) {

for (int col = 0; col < 4; col++) {

JButton btn = new JButton("Row " + row + " seat " + col);

if (col == 2) {

add(new JPanel());

}

add(btn);

}

}

Use GridBagLayout and apply insets to produce a gap...

b6BJF.png

setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

gbc.gridx = 0;

gbc.gridy = 0;

for (int row = 0; row < 10; row++) {

gbc.insets = new Insets(1, 1, 1, 1);

for (int col = 0; col < 4; col++) {

JButton btn = new JButton("Row " + row + " seat " + col);

if (col == 2) {

gbc.insets = new Insets(1, 40, 1, 1);

} else {

gbc.insets = new Insets(1, 1, 1, 1);

}

add(btn, gbc);

gbc.gridx++;

}

gbc.gridy++;

gbc.gridx = 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值