신나게 개발하기

홍대 자바 수업 계산기임 Model / View /Control Float.parseFloat 본문

홍익대 Java/수업

홍대 자바 수업 계산기임 Model / View /Control Float.parseFloat

Yi,Junho 2009. 7. 30. 14:59


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CalculatorGUI extends JFrame implements ActionListener{
  JTextField lblPrint;
  Calculator cc = new Calculator();
 boolean flag;
 public CalculatorGUI(String title){
  super(title);
  JPanel panel = new JPanel(new BorderLayout());
   lblPrint = new JTextField();    
  panel.add(lblPrint,BorderLayout.NORTH);
  JPanel panelCal= new JPanel();
  panelCal.setLayout(new GridLayout(4,4));
//버튼생성하고 리스너 달기
  String[] btnNum = {"9","8","7","*","6","5","4","/","3","2","1","+","0",".","=","-"};
  for(int i=0;i<btnNum.length;i++){
   JButton btn = new JButton(btnNum[i]);
   btn.addActionListener(this);
   panelCal.add(btn);
  }
  panel.add(panelCal,"Center");
  add(panel);
  setSize(200,200);
  setVisible(true);
 }
 public class Calculator{
  float temp;
  float x,y;
  String z;
  public void setFloatI(String xy){
   x=Float.parseFloat(lblPrint.getText());
   System.out.println(x);
  }
  public void setOp(String yun){
   z=yun;
  }
  public void setFloatJ(String j){
   y=Float.parseFloat(lblPrint.getText());
  }
  public float cal(){
   if(z.equals("+"))
   {
    temp=x+y;
    
   }
   else if(z.equals("-"))
   {
    temp=x+y;
    
   }
   else if(z.equals("*"))
   {
    temp=x+y;
    
   }
   else if(z.equals("/"))
   {
    temp=x+y;
    
   }
   System.out.println(temp);
   return temp;
   
  }
 }

   public void actionPerformed(ActionEvent e) {
System.out.println("Button's command is: " + e.getActionCommand());
 if(e.getActionCommand().equals("+")){
       cc.setFloatI(lblPrint.getText());
    lblPrint.setText("");
    cc.setOp("+");
 }else if(e.getActionCommand().equals("-")){
    cc.setFloatI(lblPrint.getText());
    lblPrint.setText("");
    cc.setOp("-");
 }else if(e.getActionCommand().equals("*")){
       cc.setFloatI(lblPrint.getText());
    lblPrint.setText("");
    cc.setOp("*");
 }else if(e.getActionCommand().equals("/")){
       cc.setFloatI(lblPrint.getText());
    lblPrint.setText("");
    cc.setOp("/");
 }else if(e.getActionCommand().equals("=")) {
  cc.setFloatJ(lblPrint.getText());
  lblPrint.setText(Float.toString(cc.cal()));
  flag = true;
 }else {
  if (flag==true){
   lblPrint.setText("");
   flag = false;
  }
  lblPrint.setText(lblPrint.getText()+e.getActionCommand());
 }
  }
 public static void main(String [] args){
  CalculatorGUI c = new CalculatorGUI("계산기");
  c.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
 }
 
}

0 Comments
댓글쓰기 폼