Tuesday, May 20, 2014

how to change background color of JPanel by clicking buttons?





The JPanel class provides general-purpose containers for lightweight components. By default, panels do not add colors to anything except their own background; however, you can easily add borders to them and otherwise customize their painting. Details can be found in Performing Custom Painting.

In many types of look and feel, panels are opaque by default. Opaque panels work well as content panes and can help with painting efficiently, as described in Using Top-Level Containers. You can change a panel's transparency by invoking the setOpaque method. A transparent panel draws no background, so that any components underneath show through.



JpanelBackground.java


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;



 public class JpanelBackground

 {

 public static void main(String[] args)

 {

 ButtonFrame frame = new ButtonFrame();

 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 frame.show();

 }

 }



 /**

 A frame with a button panel

 */

 class ButtonFrame extends JFrame

 {

 public ButtonFrame()

 {

 setTitle("ButtonTest");

 setSize(WIDTH, HEIGHT);



 // add panel to frame



 ButtonPanel panel = new ButtonPanel();

 Container contentPane = getContentPane();

 contentPane.add(panel);

 }



 public static final int WIDTH = 300;

 public static final int HEIGHT = 200;

 }  //close of class ButtonFrame



 /**

 A panel with three buttons.

 */

 class ButtonPanel extends JPanel

 {

 public ButtonPanel()

 {

 // create buttons



 JButton yellowButton = new JButton("Yellow");

 JButton blueButton = new JButton("Blue");

 JButton redButton = new JButton("Red");



 // add buttons to panel



 add(yellowButton);

 add(blueButton);

 add(redButton);



 // create button actions



 ColorAction yellowAction = new ColorAction(Color.yellow);

 ColorAction blueAction = new ColorAction(Color.blue);

 ColorAction redAction = new ColorAction(Color.red);



 // associate actions with buttons



 yellowButton.addActionListener(yellowAction);

 blueButton.addActionListener(blueAction);

 redButton.addActionListener(redAction);

 }



 /**

 An action listener that sets the panel's background color.

 */

 private class ColorAction implements ActionListener

 {

 public ColorAction(Color c)

 {

 backgroundColor = c;

 }



 public void actionPerformed(ActionEvent event)

 {

 setBackground(backgroundColor);

 //repaint();

 }



 private Color backgroundColor;

 } //close of class ColorAction



}//close of class ButtonPanel




OUTPUT:-