日期:2014-05-20 浏览次数:20830 次
import javax.swing.*;
import java.awt.*;
class ColorPanel extends JPanel
{
Color c1 =new Color(34,177,76);
Color c2 =new Color(0,162,232);
Color c3 =new Color(237,28,36);
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Dimension d = getSize();
g.setColor(c1);
g.fillRect(0,0,(int)d.getWidth(),(int)d.getHeight()/3);
g.setColor(c2);
g.fillRect(0,(int)d.getHeight()/3,(int)d.getWidth(),(int)d.getHeight()/3);
g.setColor(c3);
g.fillRect(0,(int)(d.getHeight()*(1.99)/3),(int)d.getWidth(),(int)d.getHeight()/3);
}
}
public class ColorFrame extends JFrame
{
ColorFrame()
{
setBounds(0,0,800,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new ColorPanel();
add(panel);
setVisible(true);
}
public static void main(String[] args)
{
new ColorFrame();
System.out.println("Hello World!");
}
}