Advertisement
makispaiktis

21. JColorChooser - ShowDialogue - Pick a color

May 31st, 2022 (edited)
1,341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.14 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class Peach extends JFrame{
  6.  
  7.     // Variables
  8.     private JButton b;
  9.     private Color color = Color.WHITE;
  10.     private JPanel panel;
  11.  
  12.     // Constructor
  13.     public Peach(){
  14.         super("JFrame's title");
  15.         panel = new JPanel();
  16.         panel.setBackground(color);
  17.         b = new JButton("Choose a color");
  18.         b.addActionListener(
  19.                 new ActionListener() {
  20.                     @Override
  21.                     public void actionPerformed(ActionEvent e) {
  22.                         color = JColorChooser.showDialog(null, "Pick your color", color);
  23.                         if(color == null)
  24.                             color = Color.WHITE;
  25.                         panel.setBackground(color);
  26.                     }
  27.                 }
  28.         );
  29.  
  30.         add(panel, BorderLayout.CENTER);
  31.         add(b, BorderLayout.SOUTH);
  32.         setSize(425, 150);
  33.         setVisible(true);
  34.     }
  35.  
  36.     // Main Function
  37.     public static void main(String[] args){
  38.         Peach p = new Peach();
  39.         p.setDefaultCloseOperation(EXIT_ON_CLOSE);
  40.     }
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement