본문 바로가기
IT창고/JAVA

GUI 계산기

by 창구창고 2007. 1. 22.
반응형

import java.awt.*;
import java.awt.event.*;
public class CalculatorGUI extends Frame implements ActionListener,ItemListener {
 String name_01,name_02;
 double Num2=0;
 double buf;
 private Label ans;
 
 private Button key1,key2,key3, key4,key5,key6;
 private Button key7,key8,key9,key0,keydot;
 private Button keyPlus,keyMin,keyMul,keyDiv,keyEquel,keyCE,KeyC;
 private Button keyBack,MC,MR,MS,Mplus;
 private Button keySqrt,keyPer,keyDivX,keyPM;
 private Button keyPi;
 private Button keySta,keyAve,keySum,keyS,keyDat;
 private Button keyFE,keyLG,keyRG,keyDms,keySin,keyCos,keyTan,keyExp,keyln;
 private Button keyxy,keyx3,keyx2,keylog,keyNnot,keyMod,keyOr,keyLsh,keyAnd;
 private Button keyXor,keyNot,keyInt,keyA,keyB,keyC,keyD,keyE,keyF;
 
 char OPS='=';
 boolean OPStart, OPPer, OPPM;
 CheckboxGroup cbg, cbg2;
 Checkbox hex, dec, oct, bin, linux, Degrees,Radians,Grads, Inv, Hyp;
 
 private MenuItem viewNomalMenu;
 private MenuItem viewGongMenu;
 private MenuItem copyMenu;
 private MenuItem pasteMenu;
 private MenuItem aboutMenu ;
 Menu view = new Menu("보기");
 
 StringBuffer sp=new StringBuffer();
 Panel panel,pa,panel2;
 Panel Pt ,Pt2;
 Panel Gong1,Gong2,Gong3,Gongtotal,p1,p2,p3,p4,p5,p6;
 private double Num;
 private double result;
 
 public CalculatorGUI(){
  super("계산기");
  CalculNomal();
  MenuBar menubar = new MenuBar();
  setMenuBar(menubar);  
 
  Menu Edit = new Menu("편집");
  copyMenu = new MenuItem("복사");
  pasteMenu = new MenuItem("붙여넣기");
  Edit.add(copyMenu );
  Edit.add(pasteMenu );
 
  Menu help = new Menu("도움말");
  aboutMenu = new MenuItem("About");
  help.add(aboutMenu);
 
  menubar.add(Edit);
  menubar.add(view);
  menubar.add(help);
 
  aboutMenu.addActionListener( this);
 
  setSize(300,200);
  setVisible(true);
 
  addWindowListener(new WindowHandler());
 }
 
 private void CalculNomal(){
  ans =  new Label("0",Label.RIGHT);
  add(ans, BorderLayout.NORTH);
  OPS='=';
  OPStart=false;
  OPPer=false;
  OPPM=false;
  panel2=new Panel();
  panel=new Panel();
  Pt= new Panel();
  Pt2 =new Panel();
 
  Pt.setLayout(new GridLayout(4,5,4,4));
  Pt2.setLayout(new GridLayout(1,4,4,4));
  panel2.setLayout(new GridLayout(4,1,4,4));
 
  keyBack=new Button("BackSpace");
  MC = new Button("MC");
  MR = new Button("MR");
  MS = new Button("MS");
  Mplus=new Button("M+");
 
  key1=new Button("1");
  key2=new Button("2");
  key3=new Button("3");
  key4=new Button("4");
  key5=new Button("5");
  key6=new Button("6");
  key7=new Button("7");
  key8=new Button("8");
  key9=new Button("9");
  key0=new Button("0");
  keyPlus=new Button("+");
  keyMin=new Button("-");
  keyMul=new Button("*");
  keyDiv=new Button("/");
  keyEquel=new Button("=");
  keyCE=new Button("CE");
  KeyC=new Button("C");
  keydot=new Button(".");
  keySqrt=new Button("Sqrt");
  keyPer=new Button("%");
  keyDivX=new Button("1/x");
  keyPM=new Button("+/-");
 
  Pt.add(key7);
  Pt.add(key8);
  Pt.add(key9);
  Pt.add(keyDiv);
  Pt.add(keySqrt);
  Pt.add(key4);
  Pt.add(key5);
  Pt.add(key6);
  Pt.add(keyMul);
  Pt.add(keyPer);
  Pt.add(key1);
  Pt.add(key2);
  Pt.add(key3);
  Pt.add(keyMin);
  Pt.add(keyDivX);
  Pt.add(key0);
  Pt.add(keyPM);
  Pt.add(keydot);
  Pt.add(keyPlus);
  Pt.add(keyEquel);
 
  Pt2.add(keyBack);
  Pt2.add(keyCE);
  Pt2.add(KeyC);
 
  panel2.add(MC);
  panel2.add(MR);
  panel2.add(MS);
  panel2.add(Mplus);
 
  NumberButtonHandler num=new NumberButtonHandler();
  OpButtonHandler Op = new OpButtonHandler();
  CEButtonHandler CE = new CEButtonHandler();
  CButtonHandler C = new CButtonHandler();
  PMButtonHandler Pm = new PMButtonHandler();
  BackButtonHandler Back = new BackButtonHandler();
  SOPButtonHandler SOP = new SOPButtonHandler();
 
  key1.addActionListener(num);
  key2.addActionListener(num);
  key3.addActionListener(num);
  key4.addActionListener(num);
  key5.addActionListener(num);
  key6.addActionListener(num);
  key7.addActionListener(num);
  key8.addActionListener(num);
  key9.addActionListener(num);
  key0.addActionListener(num);
  keydot.addActionListener(num);
 
  keyPlus.addActionListener(Op);
  keyMin.addActionListener(Op);
  keyMul.addActionListener(Op);
  keyDiv.addActionListener(Op);
  keyEquel.addActionListener(Op);
  keydot.addActionListener(Op);
  keyCE.addActionListener(CE);
  keyPer.addActionListener(Op);
  KeyC.addActionListener(C);
  keyBack.addActionListener(Back);
  keyPM.addActionListener(Pm);
  keySqrt.addActionListener(SOP);
  keyDivX.addActionListener(SOP);
 
  panel.setLayout(new BorderLayout(10,6));
  panel.add(Pt2,BorderLayout.NORTH);
  panel.add(Pt,BorderLayout.CENTER);
  panel.add(panel2,BorderLayout.WEST);
  add(panel,BorderLayout.CENTER);
  name_01=" 공학용";
  name_02="●일반용";
 
  viewGongMenu = new MenuItem(name_01);
  viewNomalMenu = new MenuItem(name_02);
  view.removeAll();
  view.add(viewGongMenu);
  view.add(viewNomalMenu);
 
  viewGongMenu.addActionListener( this);
  viewNomalMenu.addActionListener( this);
 }
 
 public void CalculGong(){
  cbg= new CheckboxGroup();
  cbg2= new CheckboxGroup();
  ans =  new Label("0",Label.RIGHT);
  add(ans, BorderLayout.NORTH);
  OPS='=';
  OPStart=false;
 
  Inv=new Checkbox("Inv",true);
  Hyp=new Checkbox("Hyp");
 
  Degrees=new Checkbox("Degrees",cbg2,true);
  Radians=new Checkbox("Radians",cbg2,false);
  Grads=new Checkbox("Grads",cbg2,false);
 
  hex = new Checkbox("Hex", cbg, true);
  dec = new Checkbox("Dec", cbg, false);
  oct = new Checkbox("Oct", cbg, false);
  bin= new Checkbox("Bin", cbg, false);
 
  panel2=new Panel();
  panel=new Panel();
  p1=new Panel();
  p2=new Panel();
  p3=new Panel();
  p4=new Panel();
  p5=new Panel();
  p6=new Panel();
  Pt= new Panel();
  Pt2 =new Panel();
  Gong1=new Panel();
  Gong2=new Panel();
  Gong3=new Panel();
  Gongtotal=new Panel();
 
  Gongtotal.setLayout(new GridLayout(1,2,5,5));
  Gong1.setLayout(new GridLayout(5,1,4,4));
  Gong2.setLayout(new GridLayout(5,3,4,4));
  Gong3.setLayout(new GridLayout(2,1,4,4));
  Pt.setLayout(new GridLayout(5,6,4,4));
  Pt2.setLayout(new GridLayout(2,1,4,4));
  p1.setLayout((new GridLayout(1,3,4,4)));
  p2.setLayout((new GridLayout(1,3,4,4)));
  p3.setLayout((new GridLayout(1,4,4,4)));
  p4.setLayout((new GridLayout(1,4,4,4)));
  panel2.setLayout(new GridLayout(5,1,4,4));
 
  keyBack=new Button("BackSpace");
  MC = new Button("MC");
  MR = new Button("MR");
  MS = new Button("MS");
  Mplus=new Button("M+");

  key1=new Button("1");
  key2=new Button("2");
  key3=new Button("3");
  key4=new Button("4");
  key5=new Button("5");
  key6=new Button("6");
  key7=new Button("7");
  key8=new Button("8");
  key9=new Button("9");
  key0=new Button("0");
  keyPlus=new Button("+");
  keyMin=new Button("-");
  keyMul=new Button("*");
  keyDiv=new Button("/");
  keyEquel=new Button("=");
  keyCE=new Button("CE");
  KeyC=new Button("C");
  keydot=new Button(".");
  keySqrt=new Button("Sqrt");
  keyPer=new Button("%");
  keyDivX=new Button("1/x");
  keyPM=new Button("+/-");
 
  keyPi=new Button("Pi");
  keySta=new Button("Sta");
  keyAve=new Button("Ave");
  keySum=new Button("Sum");
  keyS=new Button("S");
  keyDat=new Button("Dat");
  keyFE=new Button("F-E");
  keyLG=new Button("(");
  keyRG=new Button(")");
  keyDms=new Button("dms");
  keySin=new Button("sin");
  keyCos=new Button("cos");
  keyTan=new Button("tan");
  keyExp=new Button("Exp");
  keyln=new Button("ln");
  keyxy=new Button("x^y");
  keyx3=new Button("x^3");
  keyx2=new Button("x^2");
  keylog=new Button("log");
  keyNnot=new Button("n!");
  keyMod=new Button("Mod");
  keyOr=new Button("Or");
  keyLsh=new Button("Lsh");
  keyAnd=new Button("And");
  keyXor=new Button("Xor");
  keyNot=new Button("Not");
  keyInt=new Button("int");
  keyA=new Button("A");
  keyB=new Button("B");
  keyC=new Button("C");
  keyD=new Button("D");
  keyE=new Button("E");
  keyF=new Button("F");
  keyA.enable(false);
  keyB.enable(false);
  keyC.enable(false);
  keyD.enable(false);
  keyE.enable(false);
  keyF.enable(false);
  keyAve.enable(false);
  keySum.enable(false);
  keyS.enable(false);
  keyDat.enable(false);
 
  p1.add(Degrees);
  p1.add(Radians);
  p1.add(Grads);
 
  p3.add(hex);
  p3.add(dec);
  p3.add(oct);
  p3.add(bin);
 
  p4.add(Inv);
  p4.add(Hyp);
  p4.add(new Label());
 
  Gong3.add(p3);
  Gong3.add(p4);
 
  Gong1.add(keySta);
  Gong1.add(keyAve);
  Gong1.add(keySum);
  Gong1.add(keyS);
  Gong1.add(keyDat);
  Gong2.add(keyFE);
  Gong2.add(keyLG);
  Gong2.add(keyRG);
  Gong2.add(keyDms);
  Gong2.add(keyExp);
  Gong2.add(keyln);
  Gong2.add(keySin);
  Gong2.add(keyxy);
  Gong2.add(keylog);
  Gong2.add(keyCos);
  Gong2.add(keyx3);
  Gong2.add(keyNnot);
  Gong2.add(keyTan);
  Gong2.add(keyx2);
  Gong2.add(keyDivX);
  p5.setLayout(new BorderLayout(10,6));
  p5.add(Gong3,BorderLayout.NORTH);
  p5.add(Gong2,BorderLayout.CENTER);
  p5.add(Gong1,BorderLayout.WEST);
  p5.add(panel2,BorderLayout.EAST);
 
  Pt.add(key7);
  Pt.add(key8);
  Pt.add(key9);
  Pt.add(keyDiv);
  Pt.add(keyMod);
  Pt.add(keyAnd);
  Pt.add(key4);
  Pt.add(key5);
  Pt.add(key6);
  Pt.add(keyMul);
  Pt.add(keyOr);
  Pt.add(keyXor);
  Pt.add(key1);
  Pt.add(key2);
  Pt.add(key3);
  Pt.add(keyMin);
  Pt.add(keyLsh);
  Pt.add(keyNot);
  Pt.add(key0);
  Pt.add(keyPM);
  Pt.add(keydot);
  Pt.add(keyPlus);
  Pt.add(keyEquel);
  Pt.add(keyInt);
  Pt.add(keyA);
  Pt.add(keyB);
  Pt.add(keyC);
  Pt.add(keyD);
  Pt.add(keyE);
  Pt.add(keyF);
 
  p2.add(keyBack);
  p2.add(keyCE);
  p2.add(KeyC);
  panel2.add(MC);
  panel2.add(MR);
  panel2.add(MS);
  panel2.add(Mplus);
  panel2.add(keyPi);
 
  Pt2.add(p1);
  Pt2.add(p2);

  NumberButtonHandler num=new NumberButtonHandler();
  OpButtonHandler Op = new OpButtonHandler();
  CEButtonHandler CE = new CEButtonHandler();
  CButtonHandler C = new CButtonHandler();
  BackButtonHandler Back = new BackButtonHandler();

  key1.addActionListener(num);
  key2.addActionListener(num);
  key3.addActionListener(num);
  key4.addActionListener(num);
  key5.addActionListener(num);
  key6.addActionListener(num);
  key7.addActionListener(num);
  key8.addActionListener(num);
  key9.addActionListener(num);
  key0.addActionListener(num);
  keydot.addActionListener(num);

  keyPlus.addActionListener(Op);
  keyMin.addActionListener(Op);
  keyMul.addActionListener(Op);
  keyDiv.addActionListener(Op);
  keyEquel.addActionListener(Op);
  keydot.addActionListener(Op);
  keyCE.addActionListener(CE);
  KeyC.addActionListener(C);
  keyBack.addActionListener(Back);

  panel.setLayout(new BorderLayout(10,6));
  panel.add(Pt2,BorderLayout.NORTH);
  panel.add(Pt,BorderLayout.CENTER);
  Gongtotal.add(p5);
  Gongtotal.add(panel);
  add(Gongtotal,BorderLayout.CENTER);
 
  name_01="●공학용";
  name_02=" 일반용";
  viewGongMenu = new MenuItem(name_01);
  viewNomalMenu = new MenuItem(name_02);
  view.removeAll();
  view.add(viewGongMenu);
  view.add(viewNomalMenu);
  viewGongMenu.addActionListener( this);
  viewNomalMenu.addActionListener( this);
 }
 public class PMButtonHandler implements ActionListener {
  public void actionPerformed(ActionEvent e){
   if(!OPPM){
   sp.insert(0,'-');
   OPPM=true;
   }
   else{
    sp.deleteCharAt(0);
    OPPM=false;
   }
   ans.setText(sp.toString());
  }
 }
 
 public void itemStateChanged(ItemEvent ie) {
 }
 
 private class NumberButtonHandler implements ActionListener{
  public void actionPerformed(ActionEvent e){
   sp.append(e.getActionCommand());
   ans.setText(sp.toString());
  }
 }
 private class SOPButtonHandler implements ActionListener{
   public void actionPerformed(ActionEvent e){
    Num = parseNumber(sp.toString());
    if(e.getActionCommand()=="Sqrt"){
     result=Math.sqrt(Num);
     ans.setText(String.valueOf(result));
    }
    else if(e.getActionCommand()=="1/x"){
     result=1/Num;
     ans.setText(String.valueOf(result));
    }
  }
 }
 private static double parseNumber(String number) {
  double real_number;

  try {
    real_number = Double.parseDouble(number);
  } catch (NumberFormatException e) {
    real_number = Double.NaN;
  }

  return real_number;
   }
 private class OpButtonHandler implements ActionListener{
  public void actionPerformed(ActionEvent e){
   char op=e.getActionCommand().charAt(0);
   Num = parseNumber(sp.toString());
   switch(op){
    case '+':
      Operater(Num);
      OPS='+';
      OPStart=true;
      sp.setLength(0);
      ans.setText(String.valueOf(result));
      break;
    case '-':Operater(Num);
      OPS='-';
      OPStart=true;
      sp.setLength(0);
      ans.setText(String.valueOf(result));    
      break;
    case '*':Operater(Num);
      OPS='*';
      OPStart=true;
      sp.setLength(0);
      ans.setText(String.valueOf(result));  
      break;
    case '/':Operater(Num);
      OPS='/';
      OPStart=true;
      sp.setLength(0);
      ans.setText(String.valueOf(result));
      break;
    case '=':
      if(OPPer)Operater(Num2);
      else Operater(Num);
      sp.setLength(0);
      ans.setText(String.valueOf(result));
      OPStart=false;
      OPPer=false;  
      break;
    case '%':
      Num2=result;
      if(OPStart){
       result=result*(Num/100);
      }
      OPPer=true;
      sp.setLength(0);
      ans.setText(String.valueOf(result));    
      break;
   }
   
  }
 }
 public double Operater(double Num){
  if(!OPStart)result=Num;
   switch(OPS){
    case '+':
     result=result+Num;
     break;
    case '-':
     result=result-Num;
     break;
    case '*':
     result=result*Num;
     break;
    case '/':
     result=result/Num;
     break;
    case '=':
     result=Num;
     break;
   }return result;
   
 }

   
 private class CButtonHandler implements ActionListener{
   public void actionPerformed(ActionEvent e){
    OPStart=false;
    OPPM=false;
    OPPer=false;
    OPS='=';
    Num=0;
    result=0;
    sp.setLength(0);
    ans.setText("0");
   }
 }
 private class BackButtonHandler implements ActionListener{
   public void actionPerformed(ActionEvent e){
    if(sp.length()>1){
    int leng=sp.length()-1;
    sp.deleteCharAt(leng);
    ans.setText(sp.toString());
    }
    else ans.setText("0");
   }
 }
 private class CEButtonHandler implements ActionListener{
   public void actionPerformed(ActionEvent e){
    OPStart=false;
    OPPM=false;
    OPPer=false;
    OPS='=';
    ans.setText("0");
   }
 }
 public void actionPerformed(ActionEvent e) {
  if(e.getSource() ==aboutMenu){
   AboutDialog mydlg = new AboutDialog(this, "about", true);
   mydlg.show();
  }else if(e.getSource() == viewGongMenu) {
   setVisible(false);
   dispose();
   removeAll();
   CalculGong();
   setSize(600,300);
   setVisible(true);
  }
  else if(e.getSource() == viewNomalMenu) {
   setVisible(false);
   dispose();
   removeAll();
   CalculNomal();
   setSize(300,200);
   setVisible(true);
  }
 }
 private class AboutDialog extends Dialog implements ActionListener {
  public AboutDialog(Frame parent, String title, boolean modal) {
   super(parent, title, modal);
   add(new Label("mint"),BorderLayout.NORTH);
   add(new Label("mint"), BorderLayout.EAST);
   Button b = new Button("OK");
   add(b, BorderLayout.SOUTH);
   pack();
   b.addActionListener(this);
  }

  public void actionPerformed(ActionEvent e) {
   setVisible(false);
  }
 }

 public class WindowHandler extends WindowAdapter {
   public void windowClosing(WindowEvent e) {
   Window  w = e.getWindow();
   w.setVisible(false);
   w.dispose();
   System.exit(0);
   }
  }
 
 public static void main(String[] args) {
  new CalculatorGUI();
 }
}

반응형

"이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다."