PDA

View Full Version : [Java - Swing] Gli oggetti si sovrappongono


AngeL)
23-06-2007, 16:16
se scrivo questo codice:
JFrame frame = new JFrame("Titolo");

JLabel label1 = new JLabel("ABCD");
frame.getContentPane().add(label1);

JLabel label2 = new JLabel("EFGH");
frame.getContentPane().add(label2);

frame.pack();
frame.setVisible(true);
si vede solo il secondo label, il primo probabilmente è sotto, quindi, come posso fare per far apparire il secondo label dopo il primo?

EDIT: no, non si sovrappongono, il secondo sostituisce il primo :eek::

JButton button = new JButton("blah");
frame.getContentPane().add(button);

JLabel label = new JLabel("bleh");
frame.getContentPane().add(label);


appare il label :eek:

morskott
23-06-2007, 16:44
vedi qui (http://www.dis.uniroma1.it/~mecella/didattica/2007/ProgSw2/Esercitazioni/E4.pdf)

AngeL)
23-06-2007, 17:26
vedi qui (http://www.dis.uniroma1.it/~mecella/didattica/2007/ProgSw2/Esercitazioni/E4.pdf)

ahh, bisogna usare il borderlayour :muro:
grazie ;)

andbin
23-06-2007, 20:00
ahh, bisogna usare il borderlayourBeh, usa il layout manager che fa più comodo a te ..... c'è BorderLayout, BoxLayout, GridLayout ecc.....

Ah, un'ultima cosa: non è necessario fare frame.getContentPane().add(...) ma più semplicemente basta fare frame.add(...). La documentazione di JFrame è molto chiara:
As a conveniance add and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary. This means you can write:

frame.add(child);

AngeL)
24-06-2007, 09:21
Beh, usa il layout manager che fa più comodo a te ..... c'è BorderLayout, BoxLayout, GridLayout ecc.....

Ah, un'ultima cosa: non è necessario fare frame.getContentPane().add(...) ma più semplicemente basta fare frame.add(...). La documentazione di JFrame è molto chiara:
As a conveniance add and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary. This means you can write:

frame.add(child);

ok.. adesso vedo cosa trovo su google :D grazie :)

sirus
24-06-2007, 16:21
Un consiglio. Impara ad usare il GridBagLayout. ;)

AngeL)
24-06-2007, 16:34
Un consiglio. Impara ad usare il GridBagLayout. ;)

ok :)