Program discussed below have 4 JButton's arranged in following manner.
When the buttons Red,Green and Blue are clicked it changes the color of Display button.
DisplayColor.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class DisplayColor extends JFrame implements ActionListener
{
JButton jbtnDisplay,jbtnRed,jbtnGreen,jbtnBlue;
Color cR=Color.RED;
Color cG=Color.GREEN;
Color cB=Color.BLUE;
DisplayColor()
{
super ("DISPLAY COLOR");
setSize(1000,500);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null);
Container c=getContentPane();
jbtnDisplay=new JButton("");
jbtnDisplay.setBounds(200,100,500,200);
jbtnDisplay.setBackground(Color.BLACK);
jbtnDisplay.addActionListener (this);
c.add (jbtnDisplay);
jbtnRed=new JButton("RED");
jbtnRed.setBounds(50+100,350,100,50);
jbtnRed.addActionListener (this);
c.add (jbtnRed);
jbtnGreen=new JButton("GREEN");
jbtnGreen.setBounds(250+100,350,100,50);
jbtnGreen.addActionListener (this);
c.add (jbtnGreen);
jbtnBlue=new JButton("BLUE");
jbtnBlue.setBounds(450+100,350,100,50);
jbtnBlue.addActionListener (this);
c.add (jbtnBlue);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource()==(jbtnRed))
{
jbtnDisplay.setBackground(Color.RED);
}
if (ae.getSource()==(jbtnGreen))
{
jbtnDisplay.setBackground(Color.GREEN);
}
if (ae.getSource()==(jbtnBlue))
{
jbtnDisplay.setBackground(Color.BLUE);
}
}
public static void main (String [] args)
{
DisplayColor app=new DisplayColor();
}
}