我用JAVA编的一个计算器

界面有点像WIN XP带的简单计算器,但只能实现一些简单的功能,目前还没实现键盘输入。由于是初学JAVA,程序可能不会完美,希望大家不吝赐教,大家多多交流。
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Toolkit;
import java.awt.Dimension;
public class Calculator extends JFrame implements ActionListener{
 private final String[] BUTTON_WEST = { " ", "MC", "MR", "MS", "M+" };
 private final String[] BUTTON_NORTH = { "Backspace", "CE", "C" };
 private final String[] BUTTON_CENTER = { "7", "8", "9", "/", "sqrt", "4", "5", "6",
   "*", "%", "1", "2", "3", "-", "1/x", "0", "+/-", ".", "+", "=" };
 private JButton[] button_west = new JButton[BUTTON_WEST.length];
 private JButton[] button_north = new JButton[BUTTON_NORTH.length];
 private JButton[] button_center = new JButton[BUTTON_CENTER.length];
 private JTextField text = new JTextField("0");
 
 private boolean firstButton = true;
 private double resultNum = 0.0;
 private String operator = "=";
 private boolean operateValidFlag = true;
 
 Calculator(){
  super();
  init();
  this.setBackground(Color.LIGHT_GRAY);
  this.setTitle("计算器");
  this.setResizable(false);
  this.pack();
  
 }
 
 void init(){
  text.setHorizontalAlignment(JTextField.RIGHT);
  text.setEditable(false);
  text.setBackground(Color.WHITE);
  
  JPanel button_west_panel = new JPanel();
  button_west_panel.setLayout(new GridLayout(5,1,3,3));
  for(int i=0;i<BUTTON_WEST.length;i++){
   button_west[i] = new JButton(BUTTON_WEST[i]);
   button_west_panel.add(button_west[i]);
   button_west[i].setForeground(Color.RED);
   
  }
  
  JPanel button_north_panel = new JPanel();
  button_north_panel.setLayout(new GridLayout(1,3,3,3));
  for(int i=0;i<BUTTON_NORTH.length;i++){
   button_north[i] = new JButton(BUTTON_NORTH[i]);
   button_north_panel.add(button_north[i]);
   button_north[i].setForeground(Color.RED);
  }
  
  JPanel button_center_panel = new JPanel();
  button_center_panel.setLayout(new GridLayout(4,5,3,3));
  for(int i=0;i<BUTTON_CENTER.length;i++){
   button_center[i] = new JButton(BUTTON_CENTER[i]);
   button_center_panel.add(button_center[i]);
   button_center[i].setForeground(Color.BLUE);
   
  }
  button_center[3].setForeground(Color.RED);
  button_center[8].setForeground(Color.RED);
  button_center[13].setForeground(Color.RED);
  button_center[18].setForeground(Color.RED);
  button_center[19].setForeground(Color.RED);
  
  JPanel panel1 = new JPanel();
  panel1.setLayout(new BorderLayout(3,3));
  panel1.add("North",button_north_panel);
  panel1.add("Center",button_center_panel);
  
  JPanel panel2 = new JPanel();
  panel2.setLayout(new BorderLayout());
  panel2.add("Center",text);
  
  this.getContentPane().setLayout(new BorderLayout(3,5));
  this.getContentPane().add("North",panel2);
  this.getContentPane().add("West",button_west_panel);
  this.getContentPane().add("Center",panel1);
  
  for(int i=0;i<BUTTON_WEST.length;i++){
   button_west[i].addActionListener(this);
   
  }
  
  for(int i=0;i<BUTTON_NORTH.length;i++){
   button_north[i].addActionListener(this);
   
  }
  
  for(int i=0;i<BUTTON_CENTER.length;i++){
   button_center[i].addActionListener(this);
   
  }
 }
 
 public void actionPerformed(ActionEvent e){
  String button = e.getActionCommand();
  if(button.equals(button_north[0])){
   handleBackspace();
  }else if(button.equals(button_north[1])){
   text.setText("0");
  }else if(button.equals(button_north[2])){
   handleC();
  }else if("0123456789.".indexOf(button) >= 0){
   handleNumber(button);
  }else{
   handleOperator(button);
  }
  
 }
 
 private void handleBackspace(){
  String getText = text.getText();
  if(getText.length()>0){
   getText = getText.substring(0,getText.length()-1);
   if(getText.length()==0){
    text.setText("0");
    firstButton = true;
    operator = "=";
   }else{
    text.setText(getText);
   }
  }
 }
 
 private void handleC(){
  text.setText("0");
  firstButton = true;
  operator = "=";
 }
 
 private void handleNumber(String button){
  System.out.println(resultNum);
  if(firstButton){
   text.setText(button);
  }else if ((button.equals(".")) && (text.getText().indexOf(".") < 0)){
   text.setText(text.getText() + ".");
  } else if (!button.equals(".")) {
   text.setText(text.getText() + button);
  }
  firstButton = false;
 }
 
 private void handleOperator(String button){
  System.out.println(operator);
  
  if (operator.equals("/")) {
   if (getNumberFromText() == 0.0){
    operateValidFlag = false;
    text.setText("除数不能为零");
   } else {
    resultNum /= getNumberFromText();
   }
  } else if (operator.equals("1/x")) {
   if (resultNum == 0.0){
    operateValidFlag = false;
    text.setText("零没有倒数");
   } else {
    resultNum = 1 / resultNum;
   }
  } else if (operator.equals("+")){
   resultNum += getNumberFromText();
  } else if (operator.equals("-")){
   resultNum -= getNumberFromText();
  } else if (operator.equals("*")){
   resultNum *= getNumberFromText();
  } else if (operator.equals("sqrt")) {
   resultNum = Math.sqrt(resultNum);
  } else if (operator.equals("%")){
   resultNum = resultNum / 100;
  } else if (operator.equals("+/-")){
   resultNum = resultNum * (-1);
  } else if (operator.equals("=")){
   resultNum = getNumberFromText();
  }
  if (operateValidFlag) {
   long t1;
   double t2;
   t1 = (long) resultNum;
   t2 = resultNum - t1;
   if (t2 == 0) {
    text.setText(String.valueOf(t1));
   } else {
    text.setText(String.valueOf(resultNum));
   }
  }
  operator = button;
  System.out.println(operator);
  System.out.println(resultNum);
  firstButton = true;
  operateValidFlag = true;
 }
 
 private double getNumberFromText() {
  double result = 0;
  try {
   result = Double.valueOf(text.getText()).doubleValue();
  } catch (NumberFormatException e){
  }
  return result;
 }
 
 public static void main(String args[]) {
  Calculator calculator1 = new Calculator();
  Dimension d1=Toolkit.getDefaultToolkit().getScreenSize();
  Dimension d2=calculator1.getSize();
  calculator1.setLocation((d1.width-d2.width)/2,(d1.height-d2.height)/2);
  calculator1.setVisible(true);
  calculator1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}

你可能感兴趣的:(java,职场,休闲)