108 Program to create two button and add ActionListener in Java

Code:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Code extends Applet implements ActionListener
{
public void init()
{
showStatus("CodingAtharva Signing in");

Button b1 = new Button("Coding is Easy");
Button b2 = new Button("Coding is Hard");

add(b1);

add(b2);

b1.addActionListener(this);
b2.addActionListener(this);
}

public void actionPerformed(ActionEvent ae)
{
String str = ae.getActionCommand();

if( str.equals("Coding is Easy"))
{
setBackground(Color.green);
}
else
{
setBackground(Color.red);
}
}

}

/*
<applet code="Code" height=300 width=200>
</applet>
*/



Ouput:



Previous
Next Post »