blahs44

Untitled

Sep 27th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.67 KB | None | 0 0
  1. package ColourChooser;
  2.  
  3. import javax.swing.JDialog;
  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6. import javax.swing.JButton;
  7. import javax.swing.JTextArea;
  8. import java.awt.EventQueue;
  9. import java.awt.BorderLayout;
  10. import java.awt.Color;
  11. import java.awt.GridLayout;
  12. import java.awt.event.ActionListener;
  13. import java.awt.event.ActionEvent;
  14. import javax.swing.JColorChooser;
  15.  
  16. public class ColorChooserProgram {
  17.    
  18.     JFrame guiFrame;
  19.     JTextArea tracker;
  20.     JPanel optPanel;
  21.    
  22.     //Note: Typically the main method will be in a
  23.     //separate class. As this is a simple one class
  24.     //example it's all in the one class.
  25.     public static void main(String[] args) {
  26.      
  27.          //Use the event dispatch thread for Swing components
  28.          EventQueue.invokeLater(new Runnable()
  29.          {
  30.              
  31.             @Override
  32.              public void run()
  33.              {
  34.                  
  35.                  new ColorChooserProgram();        
  36.              }
  37.          });
  38.              
  39.     }
  40.    
  41.     public ColorChooserProgram()
  42.     {
  43.         guiFrame = new JFrame();
  44.         //make sure the program exits when the frame closes
  45.         guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  46.         guiFrame.setTitle("Dialog Box Example");
  47.         guiFrame.setSize(500,300);
  48.        
  49.         //This will center the JFrame in the middle of the screen
  50.         guiFrame.setLocationRelativeTo(null);
  51.        
  52.        
  53.         guiFrame.setLayout(new BorderLayout());
  54.        
  55.         //Using a JTextArea to diplay feedback
  56.         tracker = new JTextArea("File Tracker:");
  57.         tracker.setVisible(true);
  58.         guiFrame.add(tracker, BorderLayout.NORTH);
  59.        
  60.         optPanel = new JPanel();
  61.         optPanel.setLayout(new GridLayout(1,2));
  62.        
  63.         guiFrame.add(optPanel,BorderLayout.SOUTH);
  64.        
  65.         //button for the show dialog method
  66.         JButton showButton = new JButton("Show Color Dialog");
  67.         showButton.setActionCommand("Show Color Dialog");
  68.         showButton.addActionListener(new ActionListener()
  69.         {
  70.             public void actionPerformed(ActionEvent event)
  71.             {
  72.                 Color selectedColor = JColorChooser.showDialog(guiFrame, "Pick a Color"
  73.                 , Color.GREEN);
  74.                
  75.                 if (selectedColor != null)
  76.                 {
  77.                     tracker.append("\nThe selected color is make up of Red: "
  78.                         + selectedColor.getRed() + " Blue: " + selectedColor.getBlue()
  79.                         + " Green: " + selectedColor.getGreen());
  80.                 }
  81.             }
  82.         });
  83.        
  84.         optPanel.add(showButton);
  85.        
  86.         //button for the create dialog method
  87.         JButton createButton = new JButton("Create Color Dialog");
  88.         createButton.setActionCommand("Create Color Dialog");
  89.         createButton.addActionListener(new ActionListener()
  90.         {
  91.             @Override
  92.             public void actionPerformed(ActionEvent event)
  93.             {
  94.                 //this users a JColorchooser instance in combination
  95.                 //with a JDialog to create a color chooser dialog. It's modeless
  96.                 //and the OK and Cancel buttons can be listened to.
  97.                 final JColorChooser colorChooser = new JColorChooser();
  98.                 JDialog dialog = JColorChooser.createDialog(guiFrame,
  99.                         "Set Text Area color", false, colorChooser
  100.                         , new ActionListener()
  101.                           {
  102.                               @Override
  103.                              public void actionPerformed(ActionEvent event)
  104.                              {
  105.                                //this actionListenerr is for the OK button
  106.                                tracker.append("\nI can feel my color being changed to "
  107.                                        + colorChooser.getColor());
  108.                                tracker.setBackground(colorChooser.getColor());
  109.                              }
  110.                           }
  111.                         , new ActionListener()
  112.                          {
  113.                              @Override
  114.                              public void actionPerformed(ActionEvent event)
  115.                              {
  116.                                //this actionListener is for the cancel button
  117.                                tracker.append("\nCancel button clicked..");
  118.                              }
  119.                          }
  120.                         );
  121.                 dialog.setVisible(true);
  122.             }
  123.         });
  124.        
  125.         optPanel.add(createButton);
  126.        
  127.         guiFrame.setVisible(true);
  128.        
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment