java无法在我的Swing程序中设置JPanel的背景。
我将JPanel设置为JFrame的内容窗格
当我使用:
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) {
}
}
共 (0) 个答案