Ich habe ein JPanel als ContentPane meines JFrame gesetzt.In meinem Swing-Programm kann JPanels Hintergrund nicht eingestellt werden.
Wenn ich:
jPanel.setBackground(Color.WHITE);
Die weiße Farbe nicht angewendet wird.
Aber wenn ich benutze:
jFrame.setBackground(Color.WHITE);
Es funktioniert ... Ich bin von diesem Verhalten überrascht. Es sollte das Gegenteil sein, oder?
SSCCE:
Hier ist ein SSCCE:
Hauptklasse:
public class Main {
public static void main(String[] args) {
Window win = new Window();
}
}
Fensterklasse:
import java.awt.Color;
import javax.swing.JFrame;
public class Window extends JFrame {
private Container mainContainer = new Container();
public Window(){
super();
this.setTitle("My Paint");
this.setSize(720, 576);
this.setLocationRelativeTo(null);
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainContainer.setBackground(Color.WHITE); //Doesn't work whereas this.setBackground(Color.WHITE) works
this.setContentPane(mainContainer);
this.setVisible(true);
}
}
Container Klasse:
import java.awt.Graphics;
import javax.swing.JPanel;
public class Container extends JPanel {
public Container() {
super();
}
public void paintComponent(Graphics g) {
}
}
was ist die Größe des JPanel? Wird das Inhaltsfenster vollständig ausgefüllt? – bas
Für bessere Hilfe früher ein [SSCCE] (http://sscce.org/) – Reimeus
Ich habe nicht die Größe meines jPanel. Ich dachte, wenn Sie es als contentPane setzen, war es automatisch die gleiche Größe wie der entsprechende jFrame. – MarAja