Login    New User    Forgot Password    Help      

Impoved Button

Java Tutorials

Example

// buttons

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

/*
<applet code="control2" width = 250 height=150 >
</applet>
*/
public class control3 extends Applet implements ActionListener
{
String msg="";
Button blist[]=new Button[3];
Button yes,no,cancel;
int i;

public void init()
{
yes=new Button("yes");
no=new Button("no");
cancel=new Button("cancel");

blist[0]=(Button) add(yes);
blist[1]=(Button) add(no);
blist[2]=(Button) add(cancel);

for(i=0;i<3;i++)
{
blist[i].addActionListener(this);
}
}

public void actionPerformed(ActionEvent ae)
{
for(i=0;i<3;i++)
{
if(ae.getSource()==blist[i])
{
msg="you pressed"+blist[i].getLabel();
}
}
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,6,100);
}

}

Output

Output in Applet

Java Tutorials