//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://com.8s8s.com/it/it9964.htm