文章原始位置: http://blog.csdn.net/cppfaner/archive/2005/04/28/367144.aspx
文章1
//ShowColors2.java
//Demonstrating JColorChooser.
//Java core packages
import java.awt.*;
import java.awt.event.*;
//Java extension packages
import javax.swing.*;
public class ShowColors2 extends JFrame
{
private JButton changeColorButton;
private Color color = Color.lightGray;
private Container container;
//set up GUI
public ShowColors2()
{
super("Using JColorChooser");
container = getContentPane();
container.setLayout(new FlowLayout());
//set up changeColorButton and register its event handler
changeColorButton = new JButton( "Change Color" );
changeColorButton.addActionListener(
//anonymous inner class
new ActionListener()
{
public void actionPerformed(ActionEvent event )
{
color = JColorChooser.showDialog(ShowColors2.this,"Choose a color",color);
//set default color,if no color is returned
if(color == null )
color = Color.lightGray;
//container.setBackground( color );
}//end actionPerformd
}//end of ActionListioner
);//end call to addActionListener
container.add( changeColorButton );
setSize( 400,130 );
setVisible( true );
}
//execute application
public static void main(String arges[] )
{
ShowColors2 application = new ShowColors2();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
原始文章位置: http://blog.csdn.net/cppfaner/archive/2005/04/27/365086.aspx
文章2
// ShowColors.java
//Demonstrating Colors.
//Java core packages
import java.awt.*;
import java.awt.event.*;
//Java extension packages
import javax.swing.*;
public class MainClass extends JFrame
{
//constuctor sets window's title bar string and dimensions
public MainClass()
{
super("Using Colors");
setSize(400,130);
setVisible(true);
}
//draw rectangles and strings in defferent colors
public void paint(Graphics g)
{
//call superclass's paint method
super.paint(g);
//set new drawing color using integers
g.setColor(new Color(255,0,0));
g.fillRect(25,25,100,20);
g.drawString("Current RGB:" + g.getColor(),130,40);
//set new drawing color using floats
g.setColor(new Color(0.0f,1.0f,0.0f));
g.fillRect(25,75,100,20);
g.drawString("Current RGB:" + g.getColor(),130,65);
//set new drawing color sing static Color objects
g.setColor(Color.blue);
g.fillRect(25,75,100,20);
g.drawString("Current RGB:" + g.getColor(),130,90);
//display individual RGB values
Color color = Color.magenta; //紫红色
g.setColor(color);
g.fillRect(25,100,100,20);
g.drawString("RGB values:" + color.getRed() + "," + color.getGreen() + "," + color.getBlue(),130,115);
}
//execute application
public static void main(String args[])
{
MainClass application = new MainClass();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//调用JFrame的方法,以期在用户点击关闭窗口时能中止程序
}
}