更改框架的背景颜色

Changing background color of Frame

我刚开始 Java AWT programming.I 不能改变我框架的背景颜色。这是我的代码..在那个错误下面..请告诉我为什么我面对这个错误以及如何摆脱它.. 提前致谢!

import java.awt.*;
import java.awt.event.*;

class F1 extends Frame
{  

  public void paint(Graphics g)
 {
  g.drawString("Hi",200,300); 

 }


public static void main(String args[])
 {
  F1 f = new F1();
  f.setVisible(true);
  f.setSize(1500,1500);
  f.setBackground(Color.BLUE);
  f.setTitle("First fRAME");
  f.addWindowListener(new WindowAdapter()
  {
        public void windowClosing(WindowEvent x)
 {
  System.exit(0);

 }

  });

}
}

对我有用。您确定已导入所有必需的包吗?

  • import java.awt.Color;

尝试使用此代码,这是检查问题是由于设置背景颜色还是其他原因引起的最简单方法:

import java.awt.Color;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;



public class Test
{
    public static void main(String[] args)
    {
        Frame frame = new Frame("Title");
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.addWindowListener(new WindowAdapter() {
            @Override public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        frame.setBackground(Color.BLUE);

        frame.setVisible(true);
    }
}