JPanelをmy JFrameのcontentPaneとして設定しました。SwanプログラムでJPanelの背景を設定できません。
私が使用:
jPanel.setBackground(Color.WHITE);
白い色は適用されません。
しかし、ときに私が使用します。
jFrame.setBackground(Color.WHITE);
それは私がこの行動に驚いています...動作します。それは反対でなければならないのですか?
SSCCE:ここ
がSSCCEです:
メインクラス:
public class Main {
public static void main(String[] args) {
Window win = new Window();
}
}
ウィンドウクラス:
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);
}
}
コンテナクラス:
import java.awt.Graphics;
import javax.swing.JPanel;
public class Container extends JPanel {
public Container() {
super();
}
public void paintComponent(Graphics g) {
}
}
私testcodeで
のJPanelのサイズは何か?それはコンテンツペインを完全に埋めますか? – bas
すぐに役立つように[SSCCE](http://sscce.org/) – Reimeus
私はjPanelのサイズを設定しませんでした。 contentPaneとして設定すると、自動的に対応するjFrameと同じサイズになると思いました。 – MarAja