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 static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
/**
* {@inheritDoc}
*/
@Override
public void run() {
JComponent component = new JComponent() {
/**
* {@inheritDoc}
*/
@Override
public void paint(Graphics g) {
super.paint(g);
Dimension size = getSize();
g.setColor(Color.BLACK);
g.drawLine(0, 0, size.width, size.height);
g.drawLine(size.width, 0, 0, size.height);
}
};
component.setLayout(null);
// Composant Swing en bleu.
JPanel panel = new JPanel();
panel.setBackground(Color.BLUE);
panel.setBounds(100, 100, 250, 250);
component.add(panel);
// Composant AWT en rouge.
Canvas canvas = new Canvas();
canvas.setBackground(Color.RED);
canvas.setBounds(30, 30, 400, 400);
component.add(canvas);
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(component, BorderLayout.CENTER);
frame.setSize(600, 600);
frame.setVisible(true);
}
});
} |
Partager