Swing 计算器

Swing 计算器_第1张图片
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

/**
 * @author shy2850
 */
public class Calculator extends JApplet{
 private static final long serialVersionUID = 6011877688810114645L;
 static final char[] OPTS = {'+','-','*','/','%','p'};
 
 private JPanel mainPanel = new JPanel();
 private JPanel buttonPanel = new JPanel();
 private JTextField mainScreen = new JTextField("0");
 
 private Double num1;
 private Double num2;
 private Character opt;
 
 public void init(){
  setLayout(new BorderLayout());//简单布局
  add(mainScreen,BorderLayout.NORTH);
  add(buttonPanel,BorderLayout.CENTER);
  
  mainScreen.setFocusable(false);//不让键盘输入数据
  buttonPanel.setLayout(new GridLayout(4, 5, 8, 8));
  for (int i = 0; i < 10; i++) {
   buttonPanel.add(new NumberButton(new Integer(i)));
  }
  for (int i = 0; i < OPTS.length; i++) {
   buttonPanel.add(new DoubleOptButton(OPTS[i]));
  }
  buttonPanel.add(new BackspaceButton());
  buttonPanel.add(new DoteButton());
  buttonPanel.add(new ClearButton());
  buttonPanel.add(new AnswerButton());
 }
 
 public static void main(String[] args) {
      JFrame frame = new JFrame("我的计算器");
   Calculator c = new Calculator();
   c.init(); 
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setResizable(false);
   frame.setContentPane(c);
   frame.setBounds(400, 300, 400, 240);
   frame.setVisible(true);
 
 }
 
 /**
  * 按键类:作用是在mainScreen上面写字符
  * @author shy2850
  *
  */
 private class NumberButton extends JButton implements ActionListener{
  private static final long serialVersionUID = 2275846997927158356L;
  
  private String value;
  
  public NumberButton(int value) {
   super(value+"");
   this.value = value+"";
   addActionListener(this);
  }
  
  public void actionPerformed(ActionEvent e) {
   if("0".equals(mainScreen.getText())){
    mainScreen.setText(value);
   }else{
    mainScreen.setText(mainScreen.getText()+value);
   }
  }
 }
 
 private class DoteButton extends JButton implements ActionListener{
  private static final long serialVersionUID = -258745651987084843L;

  public DoteButton() {
   super(".");
   addActionListener(this);
  }
  
  public void actionPerformed(ActionEvent e) {
   String str = mainScreen.getText();
   if(!str.contains(".")){
    mainScreen.setText(str+".");
   }
  }
 }
 
 /**
  * 退格键 
  */
 private class BackspaceButton extends JButton implements ActionListener{

  private static final long serialVersionUID = 1405042174911616475L;
  
  public BackspaceButton() {
   super("back");
   addActionListener(this);
  }

  public void actionPerformed(ActionEvent e) {
   String str = mainScreen.getText();
   if(str.length() > 1){
    mainScreen.setText(str.substring(0,str.length()-1));
   }else if(str.length() == 1){
    mainScreen.setText("0");
   }
  }
  
 }
 
 /**
  * 运算符
  */
 private class DoubleOptButton extends JButton implements ActionListener{
  private static final long serialVersionUID = -7364459365692040675L;
  
  private char thisOpt;
  
  public DoubleOptButton(char opt) {
   super(""+(opt=='p'?"pow":opt));
   thisOpt = opt;
   addActionListener(this);
  }

  public void actionPerformed(ActionEvent arg0) {
   num1 = parseDouble();
   opt = thisOpt;
   mainScreen.setText("0");
  }
 }
 
 //清除按钮
 private class ClearButton extends JButton implements ActionListener{
  private static final long serialVersionUID = -5313525990144589143L;
  public ClearButton() {
   super("CE");
   addActionListener(this);
  }
  public void actionPerformed(ActionEvent e) {
   mainScreen.setText("0");
   clean();
  }
 }
 
 // 包装数值型转换工具
 private double parseDouble(){
  try {
   return Double.parseDouble(mainScreen.getText());
  } catch (NumberFormatException e) {
   JOptionPane.showMessageDialog(null, "数据异常!");
   mainScreen.setText("0");
   return 0.0;
  }
 }
 
 //清除内存数据
 private void clean(){
  num1 = null;
  num2 = null;
  opt = null;
 }
 
 // 等于号
 private class AnswerButton extends JButton implements ActionListener{

  private static final long serialVersionUID = -4642598104367432428L;
  public AnswerButton() {
   super("=");
   addActionListener(this);
  }

  public void actionPerformed(ActionEvent e) {
   num2 = parseDouble();
   if(null != opt){
    double answer = 0;
    switch(opt){
     case '+': 
      answer = num1+num2;
      break;
     case '-': 
      answer = num1-num2;
      break;
     case '*': 
      answer = num1*num2;
      break;
     case '/': 
      answer = num1/num2;
      break;
     case '%': 
      answer = num1%num2;
      break;
     case 'p': 
      answer = Math.pow(num1, num2);
      break;
     default: JOptionPane.showMessageDialog(null, "错误操作符!");
    }
    String result = answer+"";
    if(result.endsWith(".0")){
     result = result.replace(".0", "");
    }
    mainScreen.setText(result);
    clean();
   }
   
  }
  
 }

}

你可能感兴趣的:(Swing 计算器)