package com;import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Calculater extends JFrame implements ActionListener{
// public static final int MOUSE_CLICKED=1;
String oldNum;
String oper;
boolean op;
JPanel panel;
JLabel label;
JTextField txt;
JButton b;
String[] names={"1","2","3","+","4","5","6","--","7","8","9","*","0","%","1/x","/",".","sqrt","+/-" ,"="};
JButton[] bt=new JButton[20];
public Calculater(String title){
super(title);
label=new JLabel("输入框");
panel=new JPanel();
txt=new JTextField();
txt.setHorizontalAlignment(JTextField.RIGHT);
b=new JButton("BackSpace");
panel.setLayout(new GridLayout(5,5));         //布局管理器5X5显示
add(txt,BorderLayout.NORTH); //布局管理器添加至北区
add(panel,BorderLayout.CENTER);
add(b,BorderLayout.SOUTH);
for(int i=0;i<bt.length;i++){ //用循环的方式添加按钮
bt[i]=new  JButton(names[i]);
bt[i].addActionListener(this); //为按钮添加监听事件
panel.add(bt[i]);
}
this.setSize(220,230);      //按200X200显示
setResizable(false); //固定大小.不可改变
//this.pack();
setVisible(true);
}
public int getButton() {
return 1;
}
public static void main(String args []) {

new Calculater("计算器");
}
/*
 * 计算器运算
 * */
public void actionPerformed(ActionEvent event) {
Pattern button = Pattern.compile("\\d");
String tmp = event.getActionCommand();
Matcher m = button.matcher(tmp);
if(m.matches()){
if(op){
oldNum = txt.getText();
txt.setText("");
op = false;
}
txt.setText(txt.getText()+tmp);
}else{

if(tmp.equalsIgnoreCase(".")){
if(txt.getText().equalsIgnoreCase("")){
txt.setText("0"+tmp);
}
txt.setText(txt.getText()+tmp);
}else if(tmp.equalsIgnoreCase("=")){
double d = Double.parseDouble(oldNum);
double n = Double.parseDouble(txt.getText());
if(oper.equalsIgnoreCase("+")){
d = d + n;
}else if(oper.equalsIgnoreCase("-")){
d = d-n;
}else if(oper.equalsIgnoreCase("*")){
d = d * n;
}else{
d = d/n;
}
txt.setText(String.valueOf(d));
oper="";
op = true;
}else{
oper = tmp;
op = true;
}
}
}
}

解决方案 »

  1.   

    这样看很乱
    你发帖的时候插入JAVA代码啊
      

  2.   

    帮你贴JAVA CODE import   java.awt.BorderLayout; 
    import   java.awt.GridLayout; 
    import   java.awt.event.ActionEvent; 
    import   java.util.regex.Matcher; 
    import   java.util.regex.Pattern; 
    import   java.awt.event.ActionListener; 
    import   javax.swing.*; public   class   Calculater   extends   JFrame   implements   ActionListener{ 
    // public   static   final   int   MOUSE_CLICKED=1; 
    String   oldNum; 
    String   oper; 
    boolean   op; 
    JPanel   panel; 
    JLabel   label; 
    JTextField   txt; 
    JButton   b; 
    String[]   names={"1","2","3","+","4","5","6","--","7","8","9","*","0","%","1/x","/",".","sqrt","+/-"   ,"="}; 
    JButton[]   bt=new   JButton[20]; 
    public   Calculater(String   title){ 
    super(title); 
    label=new   JLabel("输入框"); 
    panel=new   JPanel(); 
    txt=new   JTextField(); 
    txt.setHorizontalAlignment(JTextField.RIGHT); 
    b=new   JButton("BackSpace"); 
    panel.setLayout(new   GridLayout(5,5));                 //布局管理器5X5显示 
    add(txt,BorderLayout.NORTH); //布局管理器添加至北区 
    add(panel,BorderLayout.CENTER); 
    add(b,BorderLayout.SOUTH);

    for(int   i=0;i <bt.length;i++){ //用循环的方式添加按钮 
    bt[i]=new   JButton(names[i]); 
    bt[i].addActionListener(this); //为按钮添加监听事件 
    panel.add(bt[i]); 
    }
    this.setSize(220,230);         //按200X200显示 
    setResizable(false); //固定大小.不可改变 
    //this.pack(); 
    setVisible(true); 

    public   int   getButton()   { 
    return   1; 
    }
    public   static   void   main(String   args   [])   { 

    new   Calculater("计算器"); 

    /* 
    *   计算器运算 
    *   */ 
    public   void   actionPerformed(ActionEvent   event)   { 
    Pattern   button   =   Pattern.compile("\\d"); 
    String   tmp   =   event.getActionCommand(); 
    Matcher   m   =   button.matcher(tmp); 
    if(m.matches()){ 
    if(op){ 
    oldNum   =   txt.getText(); 
    txt.setText(""); 
    op   =   false; 

    txt.setText(txt.getText()+tmp); 
    }else{ 

    if(tmp.equalsIgnoreCase(".")){ 
    if(txt.getText().equalsIgnoreCase("")){ 
    txt.setText("0"+tmp);

    txt.setText(txt.getText()+tmp); 
    }else   if(tmp.equalsIgnoreCase("=")){ 
    double   d   =   Double.parseDouble(oldNum); 
    double   n   =   Double.parseDouble(txt.getText()); 
    if(oper.equalsIgnoreCase("+")){ 
    d   =   d   +   n; 
    }else   if(oper.equalsIgnoreCase("-")){ 
    d   =   d-n; 
    }else   if(oper.equalsIgnoreCase("*")){ 
    d   =   d   *   n; 
    }else{
    d   =   d/n;

    txt.setText(String.valueOf(d)); 
    oper=""; 
    op   =   true;
    }else{ 
    oper   =   tmp; 
    op   =   true;


    }
    }