使用兩個面板...
你可以使用兩個面板(對於席)和小島之一,例如...
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);
使用「填充物「組件...
您可以在第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("Row " + row + " seat " + 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;
}
使用兩個或兩個面板,每一個島;在一組按鈕之間添加一個空面板;使用'GridBagLayout'和插入< - 有三種不同的方法可以嘗試 – MadProgrammer