import java.awt.*;
import javax.swing.*;
public class Test {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(getContentPane());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private static JPanel createContentPane() {
JPanel redPanel = new JPanel();
redPanel.setBackground(Color.red);
redPanel.setLocation(10, 10);
redPanel.setSize(50,50);
return redPanel;
}
private static JPanel getContentPane(){
// GridLayout tends to ignore the size hints,
// creates a grid of the available space and
// tries to size the components to fill the
// space of the grid cells.
JPanel mainPanel = new JPanel(new GridLayout(2, 4, 10, 30));
mainPanel.setPreferredSize(new Dimension(604, 310));
JPanel tempPanel = new JPanel();
tempPanel.setBackground(Color.blue);
tempPanel.setMinimumSize(new Dimension(50, 50));
tempPanel.setMaximumSize(new Dimension(50, 50));
tempPanel.setPreferredSize(new Dimension(50, 50));
mainPanel.add(tempPanel);
mainPanel.add(createContentPane());
mainPanel.add(createContentPane());
mainPanel.add(createContentPane());
return mainPanel;
}
}