代码如下所示:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ButtonDemo extends Applet implements ActionListener
{
String msg="";
//定义按钮"是"、"否"、"取消"
public void init()
{
Button yes=new Button("是");
Button no= new Button("否");
Button maybe=new Button("取消");
add(yes);
add(no);
add(maybe);
yes.addActionListener(this);
no.addActionListener(this);
maybe.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String str=e.getActionCommand();
if(str=="是")
{
msg="您单击了按钮'是'。";
}
else if(str=="否")
{
msg="您单击了按钮'否'。";
}
else
{
msg="您单击了按钮'取消'。";
}
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,6,100);
}
}
运行结果: