Develop a program using ActionListener in Java When User Click on Red Button it should change frame color to Red
When User Click on Red,Green,Yellow Button it should change frame color to Red,Green,Yellow
ActionListener in Java
import java.awt.*;
import java.awt.event.*;
public class C1 extends Frame implements ActionListener
{
Button b1,b2,b3;
C1()
{
b1=new Button("Red");
b1.setBounds(100,50,80,40);
add(b1);
b2=new Button("Green");
b2.setBounds(100,100,80,40);
add(b2);
b3=new Button("Yellow");
b3.setBounds(100,150,80,40);
add(b3);
setSize(500,500);
setLayout(null);
setVisible(true);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
setBackground(Color.RED);
}
if(e.getSource()==b2)
{
setBackground(Color.GREEN);
}
if(e.getSource()==b3)
{
setBackground(Color.YELLOW);
}
}
public static void main(String a[])
{
new C1();
}
}
Output:
ActionListener in Java
import java.awt.*;
import java.awt.event.*;
public class C1 extends Frame implements ActionListener
{
Button b1,b2,b3;
C1()
{
b1=new Button("Red");
b1.setBounds(100,50,80,40);
add(b1);
b2=new Button("Green");
b2.setBounds(100,100,80,40);
add(b2);
b3=new Button("Yellow");
b3.setBounds(100,150,80,40);
add(b3);
setSize(500,500);
setLayout(null);
setVisible(true);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
setBackground(Color.RED);
}
if(e.getSource()==b2)
{
setBackground(Color.GREEN);
}
if(e.getSource()==b3)
{
setBackground(Color.YELLOW);
}
}
public static void main(String a[])
{
new C1();
}
}
Output:
Comments
Post a Comment
If you have any query, please let us know