JAVA 자료실

Calculator II

페이지 정보

작성자 최고관리자 작성일 70-01-01 09:00 조회 320 댓글 0

본문

[code=php]
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CalcFrame extends JFrame implements ActionListener, KeyListener {
 JTextField text;
 JPanel buttonPanel;
 JButton numButton[] = new JButton[10];
 JButton dotButton, equalButton;
 JButton plusButton, minusButton, mulButton, divButton;
 JButton clearButton;
 String s = "";
 double result = 0;
 double num = 0;
 char operator;
 
 public CalcFrame() {
  setTitle("계산기");
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setVisible(true);
  setLayout(new BorderLayout());
 
  text = new JTextField("0");
  text.setEditable(false);
  buttonPanel = new JPanel();
  buttonPanel.setLayout(new GridLayout(0, 4));
 
  for(int i=0; i<10; i++) {
  numButton[i] = new JButton("" + i);
  numButton[i].addActionListener(this);
  }
 
  dotButton = new JButton(".");
  equalButton = new JButton("=");
  plusButton = new JButton("+");
  minusButton= new JButton("-");
  mulButton = new JButton("*");
  divButton = new JButton("/");
  dotButton.addActionListener(this);
  equalButton.addActionListener(this);
  plusButton.addActionListener(this);
  minusButton.addActionListener(this);
  mulButton.addActionListener(this);
  divButton.addActionListener(this);
 
  buttonPanel.add(numButton[1]);
  buttonPanel.add(numButton[2]);
  buttonPanel.add(numButton[3]);
  buttonPanel.add(plusButton);
  buttonPanel.add(numButton[4]);
  buttonPanel.add(numButton[5]);
  buttonPanel.add(numButton[6]);
  buttonPanel.add(minusButton);
  buttonPanel.add(numButton[7]);
  buttonPanel.add(numButton[8]);
  buttonPanel.add(numButton[9]);
  buttonPanel.add(mulButton);
  buttonPanel.add(dotButton);
  buttonPanel.add(numButton[0]);
  buttonPanel.add(equalButton);
  buttonPanel.add(divButton);
 
  clearButton = new JButton("Clear");
  clearButton.addActionListener(this);
 
  add(text, BorderLayout.NORTH);
  add(buttonPanel, BorderLayout.CENTER);
  add(clearButton, BorderLayout.SOUTH);
 
  addKeyListener(this);
  pack();
 
 }
 public void actionPerformed(ActionEvent e) {
  JButton button = (JButton) e.getSource();
 
  for(int i=0; i<10; i++) {
  if(button == numButton[i]) {
    s += i;
    text.setText(s);
  }
  }
  if(button == dotButton) {
  s += ".";
  text.setText(s);
  }
  else if(button == clearButton) {
  s = "";
  result = 0;
  num = 0;
  operator = 0;
  text.setText("0");
  } else if(button == plusButton) {
  result = Double.parseDouble(text.getText());
  text.setText("0");
  s = "";
  operator = '+';
  System.out.println(result);
  } else if(button == minusButton) {
  result = Double.parseDouble(text.getText());
  text.setText("0");
  s = "";
  operator = '-';
  System.out.println(result);
  } else if(button == mulButton) {
  result = Double.parseDouble(text.getText());
  text.setText("0");
  s = "";
  operator = '*';
  System.out.println(result);
  } else if(button == divButton) {
  result = Double.parseDouble(text.getText());
  text.setText("0");
  s = "";
  operator = '/';
  System.out.println(result);
  } else if(button == equalButton) {
  if(num == 0)
    num = Double.parseDouble(text.getText());
  switch(operator) {
  case '+':
    result += num;
    break;
  case '-':
    result -= num;
    break;
  case '*':
    result *= num;
    break;
  case '/':
    result /= num;
    break;
  }
  text.setText("" + result);
  s = "";
  }
 }
 
 public static void main(String[] args) {
  CalcFrame f = new CalcFrame();
 }
 @Override
 public void keyPressed(KeyEvent e) {
  char ch = e.getKeyChar();
 
  if(e.getKeyCode() == KeyEvent.VK_ESCAPE) {
  s = "";
  result = 0;
  num = 0;
  operator = 0;
  text.setText("0");
  }
 
  for(int i=0; i<10; i++) {
  if(ch == '0' + i) {
    s += i;
    text.setText(s);
  }
  }
  if(ch == '.') {
  s += ".";
  text.setText(s);
  }
  else if(ch == '+') {
  result = Double.parseDouble(text.getText());
  text.setText("0");
  s = "";
  operator = '+';
  System.out.println(result);
  }
  else if(ch == '-') {
  result = Double.parseDouble(text.getText());
  text.setText("0");
  s = "";
  operator = '-';
  System.out.println(result);
  } else if(ch == '*') {
  result = Double.parseDouble(text.getText());
  text.setText("0");
  s = "";
  operator = '*';
  System.out.println(result);
  } else if(ch == '/') {
  result = Double.parseDouble(text.getText());
  text.setText("0");
  s = "";
  operator = '/';
  System.out.println(result);
  } else if(ch == '=') {
  if(num == 0)
    num = Double.parseDouble(text.getText());
  switch(operator) {
  case '+':
    result += num;
    break;
  case '-':
    result -= num;
    break;
  case '*':
    result *= num;
    break;
  case '/':
    result /= num;
    break;
  }
  text.setText("" + result);
  s = "";
  }
 }
 @Override
 public void keyReleased(KeyEvent e) {
  // TODO Auto-generated method stub
 
 }
 @Override
 public void keyTyped(KeyEvent e) {
  // TODO Auto-generated method stub
 
 }
}
[/code]

댓글목록 0

등록된 댓글이 없습니다.

알림 0