您可以使用占用多余的空间GridBagLayout
看不见的组件和“推”的其他组件周围。我用Box.Filler
那些计算机图标推到表板。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class GBLTableWithChairs extends JFrame {
public GBLTableWithChairs() {
setTitle("Table with chairs");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridBagLayout());
GridBagConstraints gbc;
Box.Filler filler;
JLabel label;
JPanel table = new JPanel(new BorderLayout());
table.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.GRAY));
table.add(new JLabel("<html>TABLE TABLE<br/>TABLE TABLE<br/>TABLE TABLE"));
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 1;
add(table, gbc); // to frame
JPanel leftChair = new JPanel(new GridBagLayout());
filler = createHorizontalFiller();
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0d;
leftChair.add(filler, gbc); // to leftChair
label = new JLabel(UIManager.getIcon("FileView.computerIcon"));
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
leftChair.add(label, gbc); // to leftChair
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
add(leftChair, gbc); // to frame
JPanel topChair = new JPanel(new GridBagLayout());
filler = createVerticalFiller();
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.VERTICAL;
gbc.weighty = 1.0d;
topChair.add(filler, gbc); // to topChair
label = new JLabel(UIManager.getIcon("FileView.computerIcon"));
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
topChair.add(label, gbc); // to topChair
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
add(topChair, gbc); // to frame
JPanel rightChair = new JPanel(new GridBagLayout());
filler = createHorizontalFiller();
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0d;
rightChair.add(filler, gbc); // to rightChair
label = new JLabel(UIManager.getIcon("FileView.computerIcon"));
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
rightChair.add(label, gbc); // to rightChair
gbc = new GridBagConstraints();
gbc.gridx = 2;
gbc.gridy = 1;
add(rightChair, gbc); // to frame
JPanel bottomChair = new JPanel(new GridBagLayout());
filler = createVerticalFiller();
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.VERTICAL;
gbc.weighty = 1.0d;
bottomChair.add(filler, gbc); // to bottomChair
label = new JLabel(UIManager.getIcon("FileView.computerIcon"));
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
bottomChair.add(label, gbc); // to bottomChair
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 2;
add(bottomChair, gbc); // to frame
setSize(200, 200);
setLocationRelativeTo(null);
}
private Box.Filler createHorizontalFiller() {
return new Box.Filler(new Dimension(0, 0), new Dimension(0, 0), new Dimension(32767, 0));
}
private Box.Filler createVerticalFiller() {
return new Box.Filler(new Dimension(0, 0), new Dimension(0, 0), new Dimension(0, 32767));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GBLTableWithChairs().setVisible(true);
}
});
}
}
这应该让你开始
为了尽快得到答案,请在提问时提出问题[mcve](http://stackoverflow.com/help/mcve)。 – predi