package com.java.concource;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
@SuppressWarnings("serial")
public class TestCalculator extends JFrame implements ActionListener{
private JButton[] jb = new JButton[19];
private JPanel jp0 = null;
private JTextField tf = null;
private StringBuffer sb = null;
private StringBuffer sB = null;
private MyStack numberStack = null;
private MyStack operStack = null;
private LinkedList<String> ll = null;
private LinkedList<String> numberList = null;
private LinkedList<String> operList = null;
private float str = 0;
public TestCalculator() {
super("计算器");
jp0 = new JPanel();
jp0.setLayout(new GridLayout(5,3,10,10));
sb = new StringBuffer("");
sB = new StringBuffer("");
for(int i=0;i<10;i++){
jb[i] = new JButton(String.valueOf(i));
}
jb[10] = new JButton("+");
jb[11] = new JButton("-");
jb[12] = new JButton("*");
jb[13] = new JButton("/");
jb[14] = new JButton("=");
jb[15] = new JButton(".");
jb[16] = new JButton("C");
jb[17] = new JButton("Del");
jb[18] = new JButton("Esc");
this.setLayout(new BorderLayout());
for(JButton o : jb){
o.addActionListener(this);
jp0.add(o);
}
this.add(jp0,BorderLayout.CENTER);
tf = new JTextField();
tf.setBackground(Color.PINK);
tf.setText("");
tf.setEditable(false);
this.add(tf,BorderLayout.SOUTH);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
this.setSize(300, 300);
this.setVisible(true);
this.validate();
}
public void actionPerformed(ActionEvent e){
if(sb.toString().indexOf("Error")!=-1){
sb = new StringBuffer("");
tf.setText(sb.toString());
}
if(sb.toString().indexOf("=")!=-1){
sb = new StringBuffer("");
sb.append(String.valueOf(str));
tf.setText(sb.toString());
}
if(e.getSource()==jb[17]){
if(sb.toString()==null)
tf.setText("");
sb.deleteCharAt(sb.length()-1);
tf.setText(sb.toString());
}
else if(e.getSource()==jb[18]){
System.exit(0);
}
else if(e.getSource()==jb[16]){
sb = new StringBuffer("");
tf.setText("");
numberList = null;
}
else if(e.getSource()!=jb[14]){
JButton j = (JButton)e.getSource();
String str = j.getText();
sb.append(str);
tf.setText(sb.toString());
}
else{
char[] op = {'+','-','*','/','.'};
for(char c : op){
if(sb.toString().startsWith(String.valueOf(c)) || sb.toString().endsWith(String.valueOf(c))){
sb.append(" " + "Error:格式错误!");
tf.setText(sb.toString());
return;
}
}
String[] st = {"+-","-+","+*","*+","+/","/+","-*","*-","-/","/-","*/","/*","++","--","**","//","..",".+",".*",".-","./","+.","-.","*.","/."};
for(String o : st){
if(sb.toString().indexOf(o)!=-1){
sb.append(" " + "Error:格式错误!");
tf.setText(sb.toString());
return;
}
}
numberStack = new MyStack();
operStack = new MyStack();
ll = new LinkedList<String>();
sb.append("=");
char[] number = {'0','1','2','3','4','5','6','7','8','9','.'};
char[] oper = {'+','-','*','/','='};
char[] c = sb.toString().toCharArray();
@SuppressWarnings("unused")
boolean flag = false;
for(int i=0;i<c.length;i++){
int index = numberStack.size();
for(char o : number){
if(c[i]==o){
sB.append(String.valueOf(o));
break;
}
}
if(numberStack.size()>index)
continue;
for(char o : oper){
if(c[i]==o){
flag = true;
ll.addLast(sB.toString());
sB = new StringBuffer("");
ll.addLast(String.valueOf(o));
break;
}
}
}
while(ll.size()!=0){
String str = ll.removeFirst();
if(str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/")){
if(str.equals("+")){
operStack.push(str);
}
else if(str.equals("-")){
operStack.push(str);
}
else if(str.equals("*")){
double var0 = Double.valueOf(ll.removeFirst());
double var1 = Double.valueOf(numberStack.pop());
double var = var0*var1;
numberStack.push(String.valueOf(var));
}
else if(str.equals("/")){
double var0 = Double.valueOf(ll.removeFirst());
double var1 = Double.valueOf(numberStack.pop());
double var = var1/var0;
numberStack.push(String.valueOf(var));
}
}
else{
numberStack.push(str);
}
}
numberList = numberStack.getString();
operList = operStack.getString();
while(operList.size()!=0){
double var0 = Double.valueOf(numberList.removeFirst());
double var1 = Double.valueOf(numberList.removeFirst());
double var = 0;
String opera = operList.removeFirst();
if(opera.equals("+")){
var = var0+var1;
}
else{
var = var0-var1;
}
numberList.addFirst(String.valueOf(var));
}
str = new Double(numberList.removeFirst()).floatValue();
sb.append(str);
tf.setText(sb.toString());
operList = null;
numberList = null;
numberStack = null;
operStack = null;
ll = null;
return;
}
}
public static void main(String[] args) {
new TestCalculator();
}
}
用的是JDK1.6
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MathCalc extends JFrame{
JTextField textDisplay;
public String [] name={"1","2","3","4","5","6","7","8","9","0",".","c","+","-","*","/","1/x","+/-","="};
JButton [] button=new JButton[name.length];
JButton btn;
double Operand1;
double result=0;
boolean ClearDisplay;
String Operator;
public MathCalc(){
textDisplay=new JTextField(20);
for(int i=0;i<name.length;i++)
button[i]=new JButton(name[i]);
for(int i=0;i<name.length-1;i++)
button[i].setPreferredSize(new Dimension(52,28));
button[18].setPreferredSize(new Dimension(105,28));
layoutComponents(); //加入按钮
registerEventHander(); //按钮注册事件监听程序
setTitle("计算器");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
private void layoutComponents(){ //布局按钮面板
Container contentPane=getContentPane();
contentPane.setLayout(new GridLayout(5,1));
JPanel TextPane=new JPanel();
TextPane.add(textDisplay);
JPanel buttonPane0 = new JPanel();
JPanel buttonPane1 = new JPanel();
JPanel buttonPane2 = new JPanel();
JPanel buttonPane3 = new JPanel();
for(int i=0;i<=2;i++)
{
buttonPane0.add(button[i]);
buttonPane0.add(button[10]);
buttonPane0.add(button[11]);
}
for(int i=3;i<=5;i++)
{
buttonPane1.add(button[i]);
buttonPane1.add(button[12]);
buttonPane1.add(button[13]);
}
for(int i=6;i<=8;i++)
{
buttonPane2.add(button[i]);
buttonPane2.add(button[14]);
buttonPane2.add(button[15]);
}
buttonPane3.add(button[9]);
buttonPane3.add(button[16]);
buttonPane3.add(button[17]);
buttonPane3.add(button[18]);
contentPane.add(TextPane);
contentPane.add(buttonPane0);
contentPane.add(buttonPane1);
contentPane.add(buttonPane2);
contentPane.add(buttonPane3);
}
public static void main(String args[]){ //main函数
JFrame.setDefaultLookAndFeelDecorated(true);
MathCalc mathcal=new MathCalc();
mathcal.setVisible(true);
}
private void registerEventHander(){ //注册数字按钮事件
btnNumberEventHander number=new btnNumberEventHander();
for(int i=0;i<=10;i++)
button[i].addActionListener(number);
btnAlcEventHander alc=new btnAlcEventHander(); //运算符按钮注册事件监听程序
for(int i=12;i<=15;i++)
button[i].addActionListener(alc);
btnAccessEventHander access=new btnAccessEventHander(); //注册“=”的监听事件
button[18].addActionListener(access);
btnAcEventHander clear=new btnAcEventHander(); //注册清零键按钮事件
button[11].addActionListener(clear);
btnChangeEventHander change= new btnChangeEventHander(); //注册“+/-”按钮事件
button[17].addActionListener(change);
btnXEventHander d=new btnXEventHander();
button[16].addActionListener(d); //注册1/x键按钮事件
}
private class btnNumberEventHander implements ActionListener{ //数字按钮事件
public void actionPerformed(ActionEvent e){
JButton btn=(JButton)e.getSource();
if(ClearDisplay){
textDisplay.setText("");
ClearDisplay=false;
}
int n=countdot(textDisplay.getText());
if(btn.getText()==".")
{
if(textDisplay.getText().equals("")||n==1) //处理多个小数点问题
JOptionPane.showMessageDialog(null, "不能有多个小数点,小数点不能放在第一位!");
else textDisplay.setText(textDisplay.getText()+btn.getText());
}
else textDisplay.setText(textDisplay.getText()+btn.getText());
}
}
private class btnAlcEventHander implements ActionListener{ //处理运算符按钮事件
public void actionPerformed(ActionEvent e){
JButton btnalc=(JButton)e.getSource();
Operator=btnalc.getText();
Operand1=Double.parseDouble(textDisplay.getText());
textDisplay.setText("");
}
}
private class btnAccessEventHander implements ActionListener{ //等号(=)事件处理程序
public void actionPerformed(ActionEvent e){
double Operand2=Double.parseDouble(textDisplay.getText());
if(Operator.equals("+"))
result=Operand1+Operand2;
else if(Operator.equals("-"))
result=Operand1-Operand2;
else if(Operator.equals("*"))
result=Operand1*Operand2;
else {
if(Operand2!=0)
result=Operand1/Operand2;
else {
JOptionPane.showMessageDialog(null, "除数不能为零,结果出错!");
}
}
textDisplay.setText(""+result);
ClearDisplay=true;
}
}
private class btnAcEventHander implements ActionListener{ //清零键注册事件
public void actionPerformed(ActionEvent e){
textDisplay.setText("");
Operator="";
Operand1=0;
result=0;
}
}
private class btnChangeEventHander implements ActionListener{ //"+/-"按钮事件处理程序
public void actionPerformed(ActionEvent e){
result=-Double.parseDouble(textDisplay.getText());
textDisplay.setText(String.valueOf(result));
}
}
private class btnXEventHander implements ActionListener{ //1/x按钮事件处理程序
public void actionPerformed(ActionEvent e){
if(textDisplay.getText().equals("0"))
JOptionPane.showMessageDialog(null, "除数不能为零,请重新输入!");
else {
result=1.0/Double.parseDouble(textDisplay.getText());
textDisplay.setText(String.valueOf(result));
ClearDisplay=true;
}
}
}
int countdot(String s) { //处理多个小数点问题函数
int count=0;
for(int i=0;i<s.length();i++)
{
char ss=s.charAt(i);
if(ss=='.')
count++;
}
return count;
}
}