Java sample(Choice & Picture)


run

---(Source List)---

//Example02
import java.awt.*;
import java.awt.event.*;
public class Example02 extends Frame implements ItemListener{
  Image img01,img02,img03;
  Choice cho;
  int pc=0;
  //Main
  public static void main(String ar[]){
    Frame f=new Example02();
    f.setTitle("Choice & Picture");
    f.setSize(640,400);
    f.setVisible(true);
  }
  //Choice
  Example02(){
    setLayout(new FlowLayout());
    cho=new Choice();
      cho.add("1999");
      cho.add("2000");
      cho.add("2001");
    add(cho);
    cho.addItemListener(this);
    img01=Toolkit.getDefaultToolkit().getImage("bk079.jpg");
    img02=Toolkit.getDefaultToolkit().getImage("bk089.jpg");
    img03=Toolkit.getDefaultToolkit().getImage("bk160.jpg");
    addWindowListener(new WinAdapter());
  }
  //Close
  class WinAdapter extends WindowAdapter{
    public void windowClosing(WindowEvent we){System.exit(0);}
  }
  //Paint
  public void paint(Graphics g){
    if(pc>-1){
      g.clearRect(0,0,639,399);
    }
    if(pc==0){
      g.drawImage(img01,100,150,this);
    }
    if(pc==1){
      g.drawImage(img02,100,150,this);
    }
    if(pc==2){
      g.drawImage(img03,100,150,this);
    }
  }
  //Event
  public void itemStateChanged(ItemEvent ie){
    int  imgidx=cho.getSelectedIndex();
    if(imgidx==0){
      pc=0;
      repaint();
    }
    if(imgidx==1){
      pc=1;
      repaint();
    }
    if(imgidx==2){
      pc=2;
      repaint();
    }
  }
}