程式代碼:
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);
}
}運作結果:
初始狀态:
按鈕"是":
按鈕"否":
按鈕"取消":