Java sample(TextField & BarGraph)


run

---(Source List)---

//Example15
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
public class Example15 extends JFrame implements ActionListener{
  JTextField[] tf=new JTextField[12];
  JButton bt;
  //Main
  public static void main(String ar[]){
    JFrame f=new Example15();
    f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
    f.setSize(640,400);
    f.setVisible(true);
  }
  //TextFiled
  Example15(){
    super("TextField & BarGraph");
    int i;
    bt=new JButton("Graph");
    bt.addActionListener(this);
    bt.setActionCommand("Graph");
    bt.addActionListener(this);
    Container c=getContentPane();
    JPanel p1=new JPanel();
    for(i=0;i᝼i++){
      tf[i]=new JTextField("100",3);
      p1.add(tf[i]);
    }
    p1.add(bt);
    c.add(p1,BorderLayout.NORTH);
  }
  public void paint(Graphics g){
    String rs;
    double hei;
    int i;
    Graphics2D g2=(Graphics2D) g;
    g.clearRect(0,0,639,399);
    for(i=0;i᝼i++){
      tf[i].repaint();
      rs = tf[i].getText();
      if (rs != null) {
        try {
          hei=Double.parseDouble(rs);
          g2.draw(new Rectangle2D.Double(100+i*40,300-hei,10,hei));
        } catch (NumberFormatException e) {}
      }
    }
    bt.repaint();
  }
  public void actionPerformed(ActionEvent ae){
    if(ae.getActionCommand()=="Graph"){
      repaint();
    }
  }
}