Write a program that shows a frame with three buttons labele

Write a program that shows a frame with three buttons labeled \"Red\", \"Green\", and \"Blue\" and a label containing an icon showing a circle that is initially red. As the user clocks the buttons, the fill color of the circle should change. When you change the color, you need to invoke the repaint method on the label. The call to repaint ensures that the paintIcon method is called so that the icon can be repainted with the new color.

Solution

Answer :

import java.awt.*;
import java.awt.event.*;

public class Buttoncolors extends Frame implements ActionListener
{
Button rb, gb, bb;      
public Buttoncolors()  
{
FlowLayout fl = new FlowLayout();  
setLayout(fl);

rb = new Button(\"Red\");      
gb = new Button(\"Green\");
bb = new Button(\"Blue\");

rb.addActionListener(this);      
gb.addActionListener(this);
bb.addActionListener(this);

add(rb);          
add(gb);
add(bb);   

setTitle(\"Button in Action\");
setSize(300, 350);
setVisible(true);          
}
          
public void actionPerformed(ActionEvent e)
{
String str = e.getActionCommand();     
System.out.println(\"You clicked \" + str + \" button\");

if(str.equals(\"Red\"))
{
setBackground(Color.red);
}
else if(str.equals(\"Green\"))
{
setBackground(Color.green);
}
else if(str.equals(\"Blue\"))
{
setBackground(Color.blue);
}
}
public static void main(String args[])
{
new Buttoncolors();   
}          
}

 Write a program that shows a frame with three buttons labeled \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site