java 改变框架的背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28105594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing background color of Frame
提问by Pranay Binju
I just started Java AWT programming.I can't change background color of my frame.!Here is my code..and below that error..Plz tell me why I'm facing this error and how to get rid of that.. Thanks in advance!
我刚刚开始 Java AWT 编程。我无法更改我的框架的背景颜色。!这是我的代码..在那个错误下面..请告诉我为什么我面临这个错误以及如何摆脱它..提前致谢!
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);
}
});
}
}
回答by Giulio Biagini
It works for me. Are you sure to have imported all required packages?
这个对我有用。您确定已导入所有必需的包吗?
import java.awt.Color;
import java.awt.Color;
Try with this code, which is the simplest you can do to check if the problem is due to set background color or is due to something other:
尝试使用此代码,这是检查问题是由于设置背景颜色还是其他原因引起的最简单的方法:
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);
}
}