JButton列之间的间距 [英] Spacing between columns of JButtons

查看:464
本文介绍了JButton列之间的间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的GUI,其中前两列和JButtons的下两列之间有一个isle。代码如下所示:

  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(行+行+座位+列);
panel.add(按钮);
}
}



使用填充组件......



您可以在第2列和第3列之间放置填充组件...



  setLayout(new GridLayout( 0,5)); 

for(int row = 0; row< 10; row ++){
for(int col = 0; col< 4; col ++){
JButton btn = new JButton(行+行+座位+ col);
if(col == 2){
add(new JPanel());
}
add(btn);
}
}



使用 GridBagLayout 并应用 insets 以产生差距......



  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;
}


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);

Use a "filler" component...

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

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...

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;
}

这篇关于JButton列之间的间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆