像以下这种的绘图,为什么背景颜色改不了?

import java.awt.*;
import javax.swing.*;
class Ua extends JFrame
{
public Ua()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,300);
setLocation(100,100);
setVisible(true);
getContentPane().setBackground(Color.BLACK);
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawRect(100,100,120,100);
}

幕布斯6054654
浏览 116回答 2
2回答

红糖糍粑

import java.awt.*;import javax.swing.*;import java.awt.image.BufferedImage; class Ua extends JFrame {    public Ua()    {        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         this.setSize(300,300);        this.setLocation(100,100);          this.setVisible(true);      }    public void paint(Graphics g)    {    BufferedImage showImage = new BufferedImage(300,300,BufferedImage.TYPE_BYTE_INDEXED);    Graphics g2 = showImage.getGraphics();    Color c = g2.getColor();    g2.setColor(Color.GREEN);    g2.fillRect(0, 0, 300,300);        g2.setColor(Color.red);        g2.drawRect(100,100,120,100);    g2.setColor(c);    g.drawImage(showImage, 0, 0, this);    }    }  public class Test{    public static void main(String args[])    {        new Ua();    }}

浮云间

把你代码里的getContentPane()去掉试试
打开App,查看更多内容
随时随地看视频慕课网APP