// Fig. 12.6: ShowColors2.java // Choosing colors with JColorChooser. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ShowColors2 extends JFrame { private JButton changeColorButton; private Color color = Color.LIGHT_GRAY; 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( new ActionListener() { // anonymous inner class // display JColorChooser when user clicks button 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.LIGHT_GRAY; // change content pane's background color container.setBackground( color ); } } // end anonymous inner class ); // end call to addActionListener container.add( changeColorButton ); setSize( 400, 130 ); setVisible( true ); } // end ShowColor2 constructor // execute application public static void main( String args[] ) { ShowColors2 application = new ShowColors2(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } } // end class ShowColors2 /************************************************************************** * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and * * Prentice Hall. All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * *************************************************************************/