Advertisement
Guest User

Choose Color

a guest
Nov 20th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.10 KB | None | 0 0
  1. package practice.pkg1;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. class test extends JFrame{
  6.     private JButton b;
  7.     private Color color = (Color.BLACK);
  8.     private JPanel panel;
  9.     public test(){
  10.         super("Choose Color");
  11.         panel = new JPanel();
  12.         panel.setBackground(color);
  13.         b = new JButton("Choose a color : ");
  14.         b.addActionListener(
  15.                 new ActionListener(){
  16.                     public void actionPerformed(ActionEvent e){
  17.                         color = JColorChooser.showDialog(null, "Pick your color",color);
  18.                         if(color == null)
  19.                             color = (Color.BLACK);
  20.                         panel.setBackground(color);
  21.                     }
  22.                 }
  23.         );
  24.         add(panel,BorderLayout.CENTER);
  25.         add(b, BorderLayout.SOUTH);
  26.         setSize(350,175);
  27.         setVisible(true);
  28.     }
  29. }
  30. public class Practice1{
  31.     public static void main(String[] args) {
  32.        test t = new test();
  33.        t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  34.     }
  35.    
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement