求一个JAVA程序

/////calculator.java代码import java.applet.*;import java.awt.*;import java.awt.event.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;import javax.swing.event.*;public class calculator extends JApplet{Container contentPane; JButton btn0 = new JButton(); JButton btn1 = new JButton(); JButton btn2 = new JButton(); JButton btn3 = new JButton(); JButton btn4 = new JButton(); JButton btn5 = new JButton(); JButton btn6 = new JButton(); JButton btn7 = new JButton(); JButton btn8 = new JButton(); JButton btn9 = new JButton(); //特殊按钮 JButton btnClear = new JButton(); //清除 JButton btnEqual = new JButton(); //等于 JButton btnPlus = new JButton(); //+操作e68a843231313335323631343130323136353331333236373866符 JButton btnMinus = new JButton(); //-操作符 JButton btnMultiply = new JButton(); //*操作符 JButton btnDivide = new JButton(); //除操作符 JTextField texResult = new JTextField(); boolean flag=false; String operand1; String operand2; double result; String action; public void init(){contentPane = getContentPane(); contentPane.setLayout(null); setSize(new Dimension(250, 250)); btn0.setBounds(new Rectangle(50, 230, 45, 45)); btn0.setFont(new java.awt.Font(\"Tahoma\", Font.PLAIN, 14)); btn0.setText(\"0\"); btn0.addActionListener(new btn0(this)); //清除操作符 btnClear.setBounds(new Rectangle(100,230,45,45)); btnClear.setFont(new java.awt.Font(\"Tahoma\",Font.PLAIN,14)); btnClear.setText(\"C\"); btnClear.addActionListener(new btnClear(this)); //=操作符 btnEqual.setBounds(new Rectangle(150,230,45,45)); btnEqual.setFont(new java.awt.Font(\"Tahoma\",Font.PLAIN,14)); btnEqual.setText(\"=\"); btnEqual.addActionListener(new btnEqual(this)); //+操作符 btnPlus.setBounds(new Rectangle(200,230,45,45)); btnPlus.setFont(new java.awt.Font(\"Tahoma\",Font.PLAIN,14)); btnPlus.setText(\"+\"); btnPlus.addActionListener(new btnPlus(this)); btn1.setBounds(new Rectangle(50,180,45,45)); btn1.setFont(new java.awt.Font(\"Tahoma\",Font.PLAIN,14)); btn1.setText(\"1\"); btn1.addActionListener(new btn1(this)); btn2.setBounds(new Rectangle(100,180,45,45)); btn2.setFont(new java.awt.Font(\"Tahoma\",Font.PLAIN,14)); btn2.setText(\"2\"); btn2.addActionListener(new btn2(this)); btn3.setBounds(new Rectangle(150,180,45,45)); btn3.setFont(new java.awt.Font(\"Tahoma\",Font.PLAIN,14)); btn3.setText(\"3\"); btn3.addActionListener(new btn3(this)); //-操作符 btnMinus.setBounds(new Rectangle(200,180,45,45)); btnMinus.setFont(new java.awt.Font(\"Tahoma\",Font.PLAIN,16)); btnMinus.setText(\"-\"); btnMinus.addActionListener(new btnMinus(this)); btn4.setBounds(new Rectangle(50,130,45,45)); btn4.setFont(new java.awt.Font(\"Tahoma\",Font.PLAIN,14)); btn4.setText(\"4\"); btn4.addActionListener(new btn4(this)); btn5.setBounds(new Rectangle(100,130,45,45)); btn5.setFont(new java.awt.Font(\"Tahoma\",Font.PLAIN,14)); btn5.setText(\"5\"); btn5.addActionListener(new btn5(this)); btn6.setBounds(new Rectangle(150,130,45,45)); btn6.setFont(new java.awt.Font(\"Tahoma\",Font.PLAIN,14)); btn6.setText(\"6\"); btn6.addActionListener(new btn6(this)); //*操作符 btnMultiply.setBounds(new Rectangle(200,130,45,45)); btnMultiply.setFont(new java.awt.Font(\"Tahoma\",Font.PLAIN,14)); btnMultiply.setText(\"*\"); btnMultiply.addActionListener(new btnMultiply(this)); btn7.setBounds(new Rectangle(50,80,45,45)); btn7.setFont(new java.awt.Font(\"Tahoma\",Font.PLAIN,14)); btn7.setText(\"7\"); btn7.addActionListener(new btn7(this)); btn8.setBounds(new Rectangle(100,80,45,45)); btn8.setFont(new java.awt.Font(\"Tahoma\",Font.PLAIN,14)); btn8.setText(\"8\"); btn8.addActionListener(new btn8(this)); btn9.setBounds(new Rectangle(150,80,45,45)); btn9.setFont(new java.awt.Font(\"Tahoma\",Font.PLAIN,14)); btn9.setText(\"9\"); btn9.addActionListener(new btn9(this)); //除操作符 btnDivide.setBounds(new Rectangle(200,80,45,45)); btnDivide.setFont(new java.awt.Font(\"Tahoma\",Font.PLAIN,14)); btnDivide.setText(\"/\"); btnDivide.addActionListener(new btnDivide(this)); texResult.setText(\"\"); texResult.setBounds(new Rectangle(40, 35, 215, 35)); texResult.setFont(new java.awt.Font(\"Tahoma\", Font.PLAIN, 14)); texResult.setHorizontalAlignment(JTextField.RIGHT); contentPane.add(btn0); contentPane.add(btn1); contentPane.add(btn2); contentPane.add(btn3); contentPane.add(btn4); contentPane.add(btn5); contentPane.add(btn6); contentPane.add(btn7); contentPane.add(btn8); contentPane.add(btn9); contentPane.add(btnClear); contentPane.add(btnEqual); contentPane.add(btnPlus); contentPane.add(btnMinus); contentPane.add(btnMultiply); contentPane.add(btnDivide); contentPane.add(texResult); }//0的事件监听器 public void btn0_actionPerformed(ActionEvent e){ if(flag){ texResult.setText(btn0.getActionCommand()); flag = false; } else{ texResult.setText(texResult.getText()+btn0.getActionCommand()); } } class btn0 implements ActionListener{ private calculator adaptee; btn0(calculator adaptee){ this.adaptee=adaptee; } public void actionPerformed(ActionEvent e){ adaptee.btn0_actionPerformed(e); } }//1的事件监听器 public void btn1_actionPerformed(ActionEvent e){ if(flag){ texResult.setText(btn1.getActionCommand()); flag = false; } else{ texResult.setText(texResult.getText()+btn1.getActionCommand()); } } class btn1 implements ActionListener{ private calculator adaptee; btn1(calculator adaptee){ this.adaptee=adaptee; } public void actionPerformed(ActionEvent e){ adaptee.btn1_actionPerformed(e); } } //2的事件监听器 public void btn2_actionPerformed(ActionEvent e){ if(flag){ texResult.setText(btn2.getActionCommand()); flag = false; } else{ texResult.setText(texResult.getText()+btn2.getActionCommand()); } } class btn2 implements ActionListener{ private calculator adaptee; btn2(calculator adaptee){ this.adaptee=adaptee; } public void actionPerformed(ActionEvent e){ adaptee.btn2_actionPerformed(e); } } //3的事件监听器 public void btn3_actionPerformed(ActionEvent e){ if(flag){ texResult.setText(btn3.getActionCommand()); flag = false; } else{ texResult.setText(texResult.getText()+btn3.getActionCommand()); } } class btn3 implements ActionListener{ private calculator adaptee; btn3(calculator adaptee){ this.adaptee=adaptee; } public void actionPerformed(ActionEvent e){ adaptee.btn3_actionPerformed(e); } } //4的事件监听器 public void btn4_actionPerformed(ActionEvent e){ if(flag){ texResult.setText(btn4.getActionCommand()); flag = false; } else{ texResult.setText(texResult.getText()+btn4.getActionCommand()); } } class btn4 implements ActionListener{ private calculator adaptee; btn4(calculator adaptee){ this.adaptee=adaptee; } public void actionPerformed(ActionEvent e){ adaptee.btn4_actionPerformed(e); } } //5的事件监听器 public void btn5_actionPerformed(ActionEvent e){ if(flag){ texResult.setText(btn5.getActionCommand()); flag = false; } else{ texResult.setText(texResult.getText()+btn5.getActionCommand()); } } class btn5 implements ActionListener{ private calculator adaptee; btn5(calculator adaptee){ this.adaptee=adaptee; } public void actionPerformed(ActionEvent e){ adaptee.btn5_actionPerformed(e); } } //6的事件监听器 public void btn6_actionPerformed(ActionEvent e){ if(flag){ texResult.setText(btn6.getActionCommand()); flag = false; } else{ texResult.setText(texResult.getText()+btn6.getActionCommand()); } } class btn6 implements ActionListener{ private calculator adaptee; btn6(calculator adaptee){ this.adaptee=adaptee; } public void actionPerformed(ActionEvent e){ adaptee.btn6_actionPerformed(e); } } //7的事件监听器 public void btn7_actionPerformed(ActionEvent e){ if(flag){ texResult.setText(btn7.getActionCommand()); flag = false; } else{ texResult.setText(texResult.getText()+btn7.getActionCommand()); } } class btn7 implements ActionListener{ private calculator adaptee; btn7(calculator adaptee){ this.adaptee=adaptee; } public void actionPerformed(ActionEvent e){ adaptee.btn7_actionPerformed(e); } } //8的事件监听器 public void btn8_actionPerformed(ActionEvent e){ if(flag){ texResult.setText(btn8.getActionCommand()); flag = false; } else{ texResult.setText(texResult.getText()+btn8.getActionCommand()); } } class btn8 implements ActionListener{ private calculator adaptee; btn8(calculator adaptee){ this.adaptee=adaptee; } public void actionPerformed(ActionEvent e){ adaptee.btn8_actionPerformed(e); } } //9的事件监听器 public void btn9_actionPerformed(ActionEvent e){ if(flag){ texResult.setText(btn9.getActionCommand()); flag = false; } else{ texResult.setText(texResult.getText()+btn9.getActionCommand()); } } class btn9 implements ActionListener{ private calculator adaptee; btn9(calculator adaptee){ this.adaptee=adaptee; } public void actionPerformed(ActionEvent e){ adaptee.btn9_actionPerformed(e); } } //清除操作 public void btnClear_actionPerformed(ActionEvent e){ texResult.setText(\"\"); } class btnClear implements ActionListener{ private calculator adaptee; btnClear(calculator adaptee){ this.adaptee=adaptee; } public void actionPerformed(ActionEvent e){ adaptee.btnClear_actionPerformed(e); } } //+操作事件监听器 public void btnPlus_actionPerformed(ActionEvent e){ operand1 = texResult.getText(); action = \"plus\"; flag = true; } class btnPlus implements ActionListener{ private calculator adaptee; btnPlus(calculator adaptee){ this.adaptee=adaptee; } public void actionPerformed(ActionEvent e){ adaptee.btnPlus_actionPerformed(e); } } //-操作事件监听器 public void btnMinus_actionPerformed(ActionEvent e){ operand1 = texResult.getText(); action = \"minus\"; flag = true; } class btnMinus implements ActionListener{ private calculator adaptee; btnMinus(calculator adaptee){ this.adaptee=adaptee; } public void actionPerformed(ActionEvent e){ adaptee.btnMinus_actionPerformed(e); } } //*操作事件监听器 public void btnMultiply_actionPerformed(ActionEvent e){ operand1 = texResult.getText(); action = \"multiply\"; flag = true; } class btnMultiply implements ActionListener{ private calculator adaptee; btnMultiply(calculator adaptee){ this.adaptee=adaptee; } public void actionPerformed(ActionEvent e){ adaptee.btnMultiply_actionPerformed(e); } } //除操作事件监听器 public void btnDivide_actionPerformed(ActionEvent e){ operand1 = texResult.getText(); action = \"divide\"; flag = true; } class btnDivide implements ActionListener{ private calculator adaptee; btnDivide(calculator adaptee){ this.adaptee=adaptee; } public void actionPerformed(ActionEvent e){ adaptee.btnDivide_actionPerformed(e); } } //=操作事件监听器 public void btnEqual_actionPerformed(ActionEvent e){ double digit1; double digit2; operand2 = texResult.getText(); if(!flag){ if(action.equals(\"plus\")){ digit1 = Double.parseDouble(operand1); digit2 = Double.parseDouble(operand2); result = digit1 + digit2; texResult.setText(\"\" + result); flag = true; } else if(action.equals(\"minus\")){ digit1 = Double.parseDouble(operand1); digit2 = Double.parseDouble(operand2); result = digit1 - digit2; texResult.setText(\"\"+result); flag = true; } else if(action.equals(\"multiply\")){ digit1 = Double.parseDouble(operand1); digit2 = Double.parseDouble(operand2); result = digit1 * digit2; texResult.setText(\"\"+result); flag = true; } else if(action.equals(\"divide\")){ digit1 = Double.parseDouble(operand1); digit2 = Double.parseDouble(operand2); result = digit1 / digit2; texResult.setText(\"\"+result); flag = true; } } } class btnEqual implements ActionListener{ private calculator adaptee; btnEqual(calculator adaptee){ this.adaptee=adaptee; } public void actionPerformed(ActionEvent e){ adaptee.btnEqual_actionPerformed(e); } }}////calculator.html文件代码<html><head><title>计算器</title></head><center><applet code=calculator.class width=300 height=300></applet></center></html>以上只要有java运行环境JDK,将calculator.java文件编译,然后建一文本文件,直接运行calculator.html就可以看到效果

//界面import javax.swing.*;import java.awt.*;import java.awt.event.*;public class computor{ private JFrame jf = null; private JButton jbadd, jbsub, jbmutil, jbdiv, jbequal, jbdot, jbcr; private JTextField jexpression; private JButton jb[] = new JButton[10]; private Font newfnt; private Font oldfnt; public computor(){ newfnt = new Font(null, Font.BOLD, 30); //expfnt = new Font(null, Font.BOLD, 20); jexpression = new JTextField(\"\"); jexpression.setHorizontalAlignment(JTextField.RIGHT); //jexpression.setFont(expfnt); for(int index=0; index<10; index++) { jb[index] = new JButton(index+\"\"); jb[index].addActionListener(new ExpressionButtonHandler()); jb[index].addMouseListener(new EnterOutHandle()); //jb[index].setFont(fnt); } oldfnt = jb[0].getFont(); jbequal = new JButton(\"=\"); jbequal.addActionListener(new ResultButtonHandler()); //jbequal.setFont(fnt); jbdot = new JButton(\".\"); jbdot.addActionListener(new ExpressionButtonHandler()); //jbdot.setFont(fnt); jbadd = new JButton(\"+\"); jbadd.addActionListener(new ExpressionButtonHandler()); //jbadd.setFont(fnt); jbsub = new JButton(\"-\"); jbsub.addActionListener(new ExpressionButtonHandler()); //jbsub.setFont(fnt); jbmutil = new JButton(\"*\"); jbmutil.addActionListener(new ExpressionButtonHandler()); //jbmutil.setFont(fnt); jbdiv = new JButton(\"/\"); jbdiv.addActionListener(new ExpressionButtonHandler()); //jbdiv.setFont(fnt); jbcr = new JButton(\"CR\"); jbcr.addActionListener(new CRButtonHandler()); //jbcr.setFont(fnt); jf = new JFrame(); jf.setTitle(\"Swing计算器\"); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setSize(400, 200); jf.setLocation(100, 200); jf.setLayout(new BorderLayout()); //总体布局JPanel jptop = new JPanel(new GridLayout(1,2)); jptop.add(jexpression); jptop.add(jbcr); JPanel jpdown = new JPanel(new GridLayout(4,4)); jpdown.add(jb[1]); jpdown.add(jb[2]); jpdown.add(jb[3]); jpdown.add(jbadd); jpdown.add(jb[4]); jpdown.add(jb[5]); jpdown.add(jb[6]); jpdown.add(jbsub); jpdown.add(jb[7]); jpdown.add(jb[8]); jpdown.add(jb[9]); jpdown.add(jbmutil); jpdown.add(jbdot); jpdown.add(jb[0]); jpdown.add(jbequal); jpdown.add(jbdiv); Container ca = jf.getContentPane(); ca.add(jptop, BorderLayout.NORTH); ca.add(jpdown, BorderLayout.CENTER); jf.show(); } //定义事件处理类 private class ExpressionButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { JButton jbut = (JButton)e.getSource(); String exp = jexpression.getText(); exp = exp + jbut.getText(); jexpression.setText(exp); } } //定义事件处理类 private class CRButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { jexpression.setText(\"\"); } } public class EnterOutHandle implements MouseListener { public void mousePressed(MouseEvent e){ } public void mouseReleased(MouseEvent e){ } public void mouseEntered(MouseEvent e){ JButton jbsource = (JButton)e.getSource(); jbsource.setFont(newfnt); } public void mouseExited(MouseEvent e){ JButton jbsource = (JButton)e.getSource(); jbsource.setFont(oldfnt); } public void mouseClicked(MouseEvent e){ } } //定义事件处理类 private class ResultButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { String exp = jexpression.getText(); //利用expression类来计算结果32313133353236313431303231363533e58685e5aeb931333236373866 expression expr = new expression(exp); double re = 0; try{ re = expr.getResult(); }catch(IllegalExpressionException le) { JOptionPane.showMessageDialog(jf, le.toString()); } jexpression.setText(re + \"\"); } } public static void main(String[] args) { computor jf = new computor(); }}/*.从命令行接收一个表达式,计算该表达式的值。 简单的+ - * / %java work3 1+2javac work3 特殊:单独一个*的参数,系统会将当前目录下的所有的文件名作为参数 因此要求所有的表达式不要有空格,若有空格,则必须用\"\"括起来*/class IllegalExpressionException extends Exception{ public IllegalExpressionException(String exp){ super(exp); }}public class expression{ public String exp = null, texp = null; public String op1, op2; public String operator;public expression(String exp){ this.exp = new String(exp); texp = new String(exp); op1 = new String(); op2 = new String(); operator = new String(); } public void setExpression(String exp){ this.exp = new String(exp); texp = new String(exp); op1 = new String(); op2 = new String(); operator = new String(); } public String getExpression(){ return exp.toString(); } private void getOp1(){ int index = 0; for(index=0; index<texp.length(); index++) { char c = texp.charAt(index); if ((c>=\'0\' && c<=\'9\') || (c==\'.\') || (c==\'e\')|| (c==\'E\')) {} else break; } op1 = texp.substring(0, index); texp = texp.substring(index); //删除op1部分 } private void getOp2(){ //必须要先解析op1, operator op2 = new String(texp); } private void getOperator(){ int index = 0; for(index=0; index<texp.length(); index++) { char c = texp.charAt(index); if ((c>=\'0\' && c<=\'9\') || (c==\'.\')) break; }operator = texp.substring(0, index); texp = texp.substring(index); //删除op1部分 } public double getResult() throws IllegalExpressionException { getOp1(); getOperator(); getOp2(); //对获取到的参数和运算符进行转换 double re = 0; double dop1, dop2; char c = \' \'; //获取运算符 try{ dop1 = Double.parseDouble(op1); dop2 = Double.parseDouble(op2); }catch(NumberFormatException e) { throw new IllegalExpressionException(\"错误的操作数\"); } operator = operator.trim(); if (operator.length()!=1) { throw new IllegalExpressionException(\"错误的运算符\"); } c= operator.charAt(0); switch(c){ case \'+\': re = dop1 + dop2; break; case \'-\': re = dop1 - dop2; break; case \'*\': re = dop1 * dop2; break; case \'/\': re = dop1 / dop2; break; default: throw new IllegalExpressionException(\"错误的运算符\"); } return re; }public static void main(String[] argvs){ expression e = new expression(\"3.1 + 4+\"); try{ System.out.println(e.getResult()); System.out.println(e.op1); System.out.println(e.operator); System.out.println(e.op2); System.out.println(e.getExpression()); } catch(IllegalExpressionException ee) { System.out.println(ee.toString()); } }}