In: Computer Science
Write an application with three buttons labeled “Red”, “Green”, and “Blue” that changes the background color of a panel in the center of the frame to red, green, or blue.
Add icons to the buttons of Exercise E19.1. Use a JButton constructor with an Icon argument and supply an ImageIcon.
this is the code that I already wrote that I need to do the above to.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.ImageIcon;
public class backgroundColor extends JFrame
{
private static final int FRAME_WIDTH = 500;
private static final int FRAME_HEIGHT = 500;
private JPanel colorPanel;
public backgroundColor()
{
setTitle("Colored Buttons");
createColorBackground();
setSize(FRAME_WIDTH,
FRAME_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private void createColorBackground()
{
colorPanel = new JPanel();
add(colorPanel,
BorderLayout.CENTER);
buttonColors();
}
private void buttonColors()
{
JPanel buttonsPanel =
createButtons();
add(buttonsPanel,
BorderLayout.SOUTH);
}
private JPanel createButtons()
{
JPanel buttonsPanel = new
JPanel(new GridLayout(3, 1));
buttonsPanel.add(createButton("Red", Color.RED));
buttonsPanel.add(createButton("Green", Color.GREEN));
buttonsPanel.add(createButton("Blue", Color.BLUE));
return buttonsPanel;
}
private JButton createButton(String label, final Color
color)
{
JButton button = new
JButton(label);
ActionListener listener = new
backgroundColorListener();
button.addActionListener(listener);
return button;
}
class backgroundColorListener
implements ActionListener
{
public void
actionPerformed(ActionEvent action)
{
if(action.getActionCommand().contentEquals("Red"))
colorPanel.setBackground(Color.red);
else
if(action.getActionCommand().contentEquals("Green"))
colorPanel.setBackground(Color.green);
else
if(action.getActionCommand().contentEquals("Blue"))
colorPanel.setBackground(Color.blue);
}
}
public static void main(String[] args)
{
new backgroundColor();
}
}
PROGRAM :
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
public class WindowApplication extends JFrame implements
ActionListener{
JPanel colorPanel;
JPanel checkBoxPanel;
JCheckBox red;
JCheckBox green;
JCheckBox blue;
public WindowApplication() {
super("Window Application");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(400, 378);
getContentPane().setLayout(null);
colorPanel = new JPanel();
colorPanel.setBounds(45, 21, 309, 186);
colorPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
getContentPane().add(colorPanel);
checkBoxPanel = new JPanel();
checkBoxPanel.setBounds(10, 262, 364, 54);
getContentPane().add(checkBoxPanel);
red = new JCheckBox("Red");
green = new JCheckBox("Green");
blue = new JCheckBox("Blue");
checkBoxPanel.add(red);
checkBoxPanel.add(green);
checkBoxPanel.add(blue);
red.addActionListener(this);
green.addActionListener(this);
blue.addActionListener(this);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(red) || e.getSource().equals(green) ||
e.getSource().equals(blue)){
int redComponent = 0;
int greenComponent = 0;
int blueComponent = 0;
if(red.isSelected())
redComponent = 255;
if(green.isSelected())
greenComponent = 255;
if(blue.isSelected())
blueComponent = 255;
colorPanel.setBackground(new Color(redComponent, greenComponent,
blueComponent));
this.repaint();
}
}
public static void main(String[] args) {
new WindowApplication();
}
}
OUTPUT ;