Java code to Choose Background color of a Frame

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class BGChooser extends JPanel{
static JLabel statusbar = new JLabel(“Select a color”);
static Color c ;

public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(c);
}
public static void main(String a[]){
final BGChooser bg = new BGChooser();

//creating buttons
JButton red = new JButton();
JButton green = new JButton();
JButton blue = new JButton();
JButton yellow = new JButton();
JButton black = new JButton();
JButton gray = new JButton();
JButton pink = new JButton();
JButton cyan = new JButton();
JButton orange = new JButton();
JButton magenta = new JButton();
JButton white = new JButton();

// coloring buttons
red.setBackground(Color.RED);
green.setBackground(Color.GREEN);
blue.setBackground(Color.BLUE);
yellow.setBackground(Color.YELLOW);
black.setBackground(Color.BLACK);
gray.setBackground(Color.GRAY);
pink.setBackground(Color.PINK);
cyan.setBackground(Color.CYAN);
orange.setBackground(Color.ORANGE);
magenta.setBackground(Color.MAGENTA);
white.setBackground(Color.WHITE);

//adding functionality to the buttons
red.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
c = (Color.RED);
statusbar.setText(“You selected Red color,the background has now changed to Red”);
bg.repaint();
}
});

green.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
c = (Color.GREEN);
statusbar.setText(“You selected Green color,the background has now changed to Green”);
bg.repaint();
}
});

blue.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
c = (Color.BLUE);
statusbar.setText(“You selected Blue color,the background has now changed to Blue”);
bg.repaint();
}
});

yellow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
c = (Color.YELLOW);
statusbar.setText(“You selected Yellow color,the background has now changed to Yellow”);
bg.repaint();
}
});

black.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
c = (Color.BLACK);
statusbar.setText(“You selected Black color,the background has now changed to Black”);
bg.repaint();
}
});

gray.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
c = (Color.GRAY);
statusbar.setText(“You selected Gray color,the background has now changed to Gray”);
bg.repaint();
}
} );
pink.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){
c = (Color.PINK);
statusbar.setText(“You selected Pink color,the background has now changed to Pink”);
bg.repaint();
}
} );

cyan.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
c = (Color.CYAN);
statusbar.setText(“You selected Cyan color,the background has now changed to Cyan”);
bg.repaint();
}
} );
orange.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
c = (Color.ORANGE);
statusbar.setText(“You selected Orange color,the background has now changed to Orange”);

bg.repaint();
}
} );
magenta.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
c = (Color.MAGENTA);
statusbar.setText(“You selected Magenta color,the background has now changed to Magenta”);
bg.repaint();
}
} );
white.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
c = (Color.WHITE);
statusbar.setText(“You selected White color,the background has now changed to White”);
bg.repaint();
}
} );

//adding buttons to the panel
bg.add(red);
bg.add(green);
bg.add(blue);
bg.add(yellow);
bg.add(black);
bg.add(gray);
bg.add(pink);
bg.add(cyan);
bg.add(orange);
bg.add(magenta);
bg.add(white);

//creating window
JFrame frame = new JFrame(“Pick a Color”);
frame.setSize(500,400);
frame.setLocation(300,200);
frame.add(bg);

frame.add(statusbar,BorderLayout.SOUTH);
frame.setVisible(true);
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s