JFrame jf =new JFrame();
jf.setSize(500, 500;
jf.setVisible(true);
jf.setBackground(Color.RED);
JFrame背景颜色还是白色
Frame jf =new Frame();
jf.setSize(500, 500;
jf.setVisible(true);
jf.setBackground(Color.RED);
而Frame背景颜色是红色,怎么回事
JFrame设置背景色方法如下:
package com.tools;
import java.awt.Color;
import javax.swing.JFrame;
public class Test extends JFrame
{
public static void main(String[] args)
{
//执行入口
new Test();
}
public Test()
{
this.setSize(400,300);
this.setLocation(400,300);
this.setBackground(Color.blue);
this.getContentPane().setBackground(Color.red);
//如果改为true那么就变成了红色。
this.getContentPane().setVisible(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
JFrame是javax.swing包下的,而Frame是java.awt包下的,swing是对awt的拓展和强化,但是这并不代表awt的落后,实际开发中都有用到。
希望对你有所帮助。
:-)
package JavaText_SnowingAndStars;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class Stars {
public static void main(String[] args) {
JFrame w = new JFrame();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = dim.width;
int screenHeight = dim.height;
System.out.println("screen = "+screenWidth+"*"+screenHeight);
w.setSize(screenWidth,screenHeight);
w.setVisible(true);
w.setTitle("Stars");
w.setBackground(Color.BLACK);
w.getContentPane().setBackground(Color.WHITE);
w.getContentPane().setVisible(false);
MyPanelthree mp = new MyPanelthree();
w.add(mp);
}
}
class MyPanelthree extends Panel{
public void paint(Graphics g){
g.setColor(Color.WHITE);
for(int i = 0; i<300;i++){
g.drawString("*", (int)(Math.random()*1600), (int)(Math.random()*900));
}
}
}