Java sample(TextField & BarGraph)


run

---(Source List)---

//Example05
import java.awt.*;
import java.awt.event.*;
public class Example05 extends Frame implements ActionListener{
  TextField[] tf=new TextField[12];
  //Main
  public static void main(String ar[]){
    Frame f=new Example05();
    f.setTitle("TextField & BarGraph");
    f.setSize(640,400);
    f.setVisible(true);
  }
  //TextField
  Example05(){
    int i;
    setLayout(new FlowLayout());
    Button b01=new Button("Graph");
    b01.addActionListener(this);
    for(i=0;i<12;i++){
      tf[i]=new TextField("100",3);
      add(tf[i]);
    }
    add(b01);
    addWindowListener(new WinAdapter());
  }
  //Close
  class WinAdapter extends WindowAdapter{
    public void windowClosing(WindowEvent we){System.exit(0);}
  }
  //Paint
  public void paint(Graphics g){
    String rs;
    int hei;
    int i;
    g.clearRect(0,0,639,399);
    for(i=0;i<12;i++){
      rs = tf[i].getText();
      if (rs != null) {
        try {
          hei=Integer.parseInt(rs);
          g.drawRect(100+i*40,300-hei,10,hei);
        } catch (NumberFormatException e) {}
      }
    }
  }
  //Event
  public void actionPerformed(ActionEvent ae){
    if(ae.getActionCommand()=="Graph"){
      repaint();
    }
  }
}