Login    New User    Forgot Password    Help      

Button

Java Tutorials

Example

//buttons

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

public class control2 extends Applet implements ActionListener
{
String msg="";
Button yes,no,cancel;
public void init()

{
yes=new Button("yes");
no=new Button("no");
cancel=new Button("cancel");

add(yes);
add(no);
add(cancel);
yes.addActionListener(this);
no.addActionListener(this);
cancel.addActionListener(this);

}

public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
if(str.equals("yes"))
{
msg="You pressed yes";
}
else if(str.equals("no"))
{
msg="you pressed no";
}
else
{
msg="You pressed cancel";
}
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,6,100);
}
}

Output

Output in applet

Java Tutorials