Unterschied: 'setContentPane(panel)' und getContentPane().add(panel);

the_skywalker

Mitglied
Hallo,

kann mir jemand mal den Unterschied zwischen

  • setContentPane(panel);
und

  • getContentPane().add(panel);
erklären.

Ich habe beides ausprobiert und beides geht. Doch welche Art ist besser oder gar richtig?

Stefan
 
the_skywalker hat gesagt.:
Hallo,

kann mir jemand mal den Unterschied zwischen

  • setContentPane(panel);
und

  • getContentPane().add(panel);
erklären.

Ich habe beides ausprobiert und beides geht. Doch welche Art ist besser oder gar richtig?

Stefan

Ich verwende immer getContentPane()... und das wird auch so vorgeschlagen von der IDE/Compiler, wenn man direkt Komponenten einem JFrame hinzufügen will.

So steht es in der API in JFrame:

public Container getContentPane()

Returns the contentPane object for this frame.


public void setContentPane(Container contentPane)

Sets the contentPane property. This method is called by the constructor.

Swing's painting architecture requires an opaque JComponent in the containment hiearchy. This is typically provided by the content pane. If you replace the content pane it is recommended you replace it with an opaque JComponent.
 
Zurück