관리 메뉴

BLOG ESPERANCA

JAVA GUI 사칙연산 계산기 본문

Program/Java

JAVA GUI 사칙연산 계산기

yobuce 2008. 5. 22. 16:58

사용자 삽입 이미지



import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class ButtonActionListener implements ActionListener {
 private int intFlag = 0;
 private String strAct = null;
 private TextField display = null;
 private String strOP = null;
 private String strTmp = null;
 
 public ButtonActionListener (TextField display) {
  this.display = display;
 }
 

 public void actionPerformed(ActionEvent arg0) {
  strAct = arg0.getActionCommand();
 
  if (strAct.equals("/") || strAct.equals("*") || strAct.equals("-") || strAct.equals("+")){
   strTmp =display.getText();
   strOP = strAct;
   intFlag = 1;
  }
  else if (strAct.equals("=")){
   if (intFlag == 2){
    long f = Long.parseLong(strTmp);
    long e = Long.parseLong(display.getText());
    long result;
   
    if(strOP.equals("+")){
     result = f + e;
     display.setText(""+ result);
    }
    else if (strOP.equals("*")){
     result = f * e;
     display.setText(""+ result);
    }
    else if (strOP.equals("/")){
     if(e ==0)
      display.setText("Error:Divide by 0");
     else{
      result = f / e;
      display.setText(""+ result);
     }
    }
    intFlag = 0;
   }
  }
  else if (strAct.equals("C")){
   intFlag = 0;
   display.setText("0");
  }
  else{
   if(intFlag == 1){
    intFlag = 2;
    display.setText("0");
   }
   if(display.getText().equals("0")){
    display.setText(strAct);
   }
   else{
    display.setText(display.getText() + strAct);
   }
  }
 }
}


import java.awt.Button;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionListener;

public class ButtonPane extends Panel {
 private TextField display = null;
 private Button[] butt = null;
 private final String[] buttName = { "7" , "8", "9" , "/",
   "4", "5", "6", "*",
   "1", "2", "3", "-",
   "0", "C", "=", "+"};
 
 public ButtonPane (TextField display){
  this.display = display;
  setLayout ( new GridLayout(4,4));
  ActionListener act1 = new ButtonActionListener (display);
  butt = new Button[buttName.length];
  for(int i = 0; i < butt.length; i++){
   butt[i] = new Button(buttName[i]);
   butt[i].addActionListener(act1);
   add (butt[i]);
  }
 }
}


import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.*;


public class Calculator implements WindowListener {
 
 private Frame mainFrame = null;
 private TextField display = null;
 private Panel displayPanel = null;
 private ButtonPane buttonPane = null;
 private String strTitle = null;
 
 public Calculator (String strTitle){
  this.strTitle = strTitle;
 }
 
 public void init(){
  mainFrame = new Frame (strTitle);
 
  mainFrame.setLayout(new BorderLayout());
  mainFrame.setBounds(10,10, 250, 250);
  mainFrame.addWindowListener(this);
 
  display = new TextField (30);
  display.setText("0");
  display.setEditable(false);
 
  displayPanel = new Panel();
  displayPanel.setBounds(0, 30, 250, 40);
  displayPanel.add(display);
 
  buttonPane = new ButtonPane (display);
  buttonPane.setBounds(0, 70, 250, 180);
 
  mainFrame.add("North", displayPanel);
  mainFrame.add("Center", buttonPane);
 
  mainFrame.setVisible(true);
 
 }
 

 @Override
 public void windowActivated(WindowEvent arg0) {
  // TODO Auto-generated method stub

 }

 @Override
 public void windowClosed(WindowEvent arg0) {
  // TODO Auto-generated method stub

 }

 @Override
 public void windowClosing(WindowEvent arg0) {
  // TODO Auto-generated method stub
  System.out.println("dfsdf");
  mainFrame.setVisible(false);
  mainFrame.dispose();
  System.exit(0);
 }

 @Override
 public void windowDeactivated(WindowEvent arg0) {
  // TODO Auto-generated method stub
  System.out.println("dfljsdflksdf");

 }

 @Override
 public void windowDeiconified(WindowEvent arg0) {
  // TODO Auto-generated method stub

 }

 @Override
 public void windowIconified(WindowEvent arg0) {
  // TODO Auto-generated method stub

 }

 @Override
 public void windowOpened(WindowEvent arg0) {
  // TODO Auto-generated method stub

 }
 public static void main (String[] args){
  Calculator calculator = new Calculator ("dsfsdf");
  calculator.init();
 }

}


0 Comments
댓글쓰기 폼