1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| public class ExemplePositionLayout extends JComponent {
public ExemplePositionLayout() {
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(getForeground());
Rectangle bounds = getBounds();
g.fillOval(0, 0, (int)bounds.getWidth(), (int)bounds.getHeight());
}
public static void main(String[] args) {
JFrame frame = new JFrame("Layout Manager");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add( createComposant(Color.RED), BorderLayout.CENTER );
panel.add( createComposant(Color.BLUE), BorderLayout.NORTH );
panel.add( createComposant(Color.YELLOW), BorderLayout.EAST );
panel.add( createComposant(Color.GREEN), BorderLayout.SOUTH );
panel.add( createComposant(Color.ORANGE), BorderLayout.WEST );
frame.setSize(200, 200);
frame.getContentPane().add(panel);
frame.setVisible(true);
}
private static Component createComposant(Color couleur) {
ExemplePositionAbsolue composant = new ExemplePositionAbsolue();
composant.setPreferredSize(new Dimension(50,70));
composant.setMaximumSize(new Dimension(100,100));
composant.setForeground(couleur);
return composant;
}
} |