Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package usocolorchooser;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import javax.swing.JButton;
  8. import javax.swing.JColorChooser;
  9. import javax.swing.JFrame;
  10. import javax.swing.JPanel;
  11.  
  12. /**
  13.  *
  14.  * @author Rafa
  15.  */
  16. public class UsoColorChooser extends JFrame{
  17.     private JButton botonCambiarColor;
  18.     private Color color = Color.CYAN;
  19.    
  20.     public UsoColorChooser (){
  21.         super("Prueba JColocrChooser");
  22.         botonCambiarColor = new JButton("Seleccionar otro Color"); //crea el boton
  23.        
  24.         setSize(300, 300); //Establece tamano de frame
  25.         setVisible(true); //Hace visible el frame
  26.         //manejador de eventos
  27.         botonCambiarColor.addActionListener(
  28.                
  29.                 new ActionListener() { //clase anonima
  30.                     public void actionPerformed(ActionEvent evento){
  31.                         color = JColorChooser.showDialog(UsoColorChooser.this, "Elija un Color", color);
  32.                        
  33.                         if(color == null){
  34.                             color = Color.CYAN;
  35.                         }
  36.                        
  37.                          botonCambiarColor.setBackground(color); //el boton toma el color seleccionado
  38.                     }
  39.                 }
  40.                 );
  41.        
  42.         //Agrega boton al frame
  43.         add(botonCambiarColor, BorderLayout.CENTER);
  44.        
  45.     }
  46.    
  47.     public static void main(String[] args) {
  48.         UsoColorChooser app = new UsoColorChooser();
  49.         app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  50.         app.setLocationRelativeTo(null); //centra el Frame
  51.     }
  52. }