close
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class MyChoice extends Applet implements ItemListener
{
Label lbl1, lbl2, lbl3;
String[] strF = {"暑條加大", "飲料加大"};
Checkbox[] chkF= new Checkbox[strF.length];

int[] price_cho = {40, 40, 60, 60, 60, 70};
String[] str_cho_price = {"1號餐(120元)", "2號餐(130元)",
"3號餐(110元)", "4號餐(105元)", "5號餐(160元)", "6號餐(140元)"};
Choice cho;
int total; //總價錢
String msg="";
Font f=new Font("細明體", Font.BOLD, 12);
public void init()
{
lbl1 = new Label("***************** 麥當勞點餐系統 *****************");
lbl2= new Label(" 請您選擇主餐食物:");
lbl3= new Label(" 請您選擇加大(5元): ");
cho = new Choice();
for (int i=0; i cho.add(str_cho_price[i]);
cho.addItemListener(this);
for (int i=0; i {
chkF[i] = new Checkbox(strF[i]);
chkF[i].addItemListener(this);
}
add(lbl1);
add(lbl2);
add(cho);
add(lbl3);
for (int i=0; i {
add(chkF[i]);
}
}
public void itemStateChanged(ItemEvent ie)
{
total = 0;
msg = "";
int n = cho.getSelectedIndex();
total += price_cho[n];
msg += cho.getSelectedItem();
for (int i=0; i {
if(chkF[i].getState()==true)
{
total += 10 ;
msg += ","+ chkF[i].getLabel();
}
}
repaint();
}
public void paint(Graphics g)
{
g.setFont(f);
g.drawString("您的餐點是:" + msg , 10, 130);
g.drawString("您的餐點總共要: "+total+"元",10,150);
}
}
arrow
arrow
    全站熱搜

    Neo Chao 發表在 痞客邦 留言(0) 人氣()