jPanel子类无法使用.setBackground()函数的问题

主函数中:

/************carve*************/
MyCarve carve=new MyCarve();
carve.setBackground(Color.RED); //这句话没用
carve.setLayout(new GridLayout(1,1));
mainPanel.add(carve);


MyCarve.java

public class MyCarve extends JPanel {
	
	public MyCarve(){
		super();
	}
	
	public void paint(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		for(int i=0;i
解决办法,在paint函数中加入 super.paint(g); 就行了

public class MyCarve extends JPanel {
	
	public MyCarve(){
		super();
	}
	
	public void paint(Graphics g) {
		super.paint(g);
		Graphics2D g2d = (Graphics2D) g;
		for(int i=0;i



你可能感兴趣的:(Java)