Java sample(Button & String)


run

---(Source List)---

//Example01
import java.awt.*;
import java.awt.event.*;
public class Example01 extends Frame implements ActionListener{
  int pc=-1;
  //Main
  public static void main(String ar[]){
    Frame f=new Example01();
    f.setTitle("Button & String");
    f.setSize(640,400);
    f.setVisible(true);
  }
  //Button
  Example01(){
    setLayout(new FlowLayout());
    Button b0=new Button("1999");
    Button b1=new Button("2000");
    Button b2=new Button("2001");
    b0.addActionListener(this);
    b1.addActionListener(this);
    b2.addActionListener(this);
    add(b0);
    add(b1);
    add(b2);
    addWindowListener(new WinAdapter());
  }
  //Close
  class WinAdapter extends WindowAdapter{
    public void windowClosing(WindowEvent we){System.exit(0);}
  }
  //Paint
  public void paint(Graphics g){
    String s0="AAAAAA";
    String s1="BBBBBBBBB";
    String s2="CCCC";
    if(pc>-1){
      g.clearRect(0,0,639,399);
    }
    if(pc==0){
      g.drawString(s0,100,150);
    }
    if(pc==1){
      g.drawString(s1,100,150);
    }
    if(pc==2){
      g.drawString(s2,100,150);
    }
  }
  //Event
  public void actionPerformed(ActionEvent ae){
    if(ae.getActionCommand()=="1999"){
      pc=0;
      repaint();
    }
    if(ae.getActionCommand()=="2000"){
      pc=1;
      repaint();
    }
    if(ae.getActionCommand()=="2001"){
      pc=2;
      repaint();
    }
  }
}