谁帮我完成以下程序 使用java写的小型计算器问题

来源:百度知道 编辑:UC知道 时间:2024/05/31 04:17:57
import java.awt.*;import java.applet.*;import java.awt.event.*;import javax.swing.*;public class My_caculator extends Applet implements ActionListener { Panel pn,pc,pe; //定义3个PANEL,分别来装文本框,数字按纽,控制按纽 JButton digit[]=new JButton[12]; //定义数字键按纽,包括小数点,和等号 JButton caculate[]=new JButton[6]; //定义运算符 JButton control[]=new JButton[2]; //定义控制按纽,包括退格和清空 JTextField DisplayText; double c1=0.0,c2=0.0; //运算的两个数 char flag='n'; //标志按了哪个运算符,初始为n,表示什么运算符没按 public void init() { //------------------PANEL的初始化------- pn=new Panel(); pe=new Panel(); pc=new Panel(); setLayout(new BorderLayout(8,10)); add(pn,BorderLayout.NORTH); add(pe,BorderLayout.EAST); add(pc,BorderLayout.CENTER); //------------------------------------ //-----------计算器显示框初始化------------- DisplayText=new JTextField(30); pn.add(DisplayText); DisplayText.setText("Calculator!"); DisplayText.setHorizontalAlignment(JTextField.RIGHT); DisplayText.setForeground

我用我朋友的账号再对问题进行补充:

else if(e.getActionCommand().equals(".")){ //按的是小数点 if(DisplayText.getText().indexOf('.')<0) DisplayText.setText(DisplayText.getText()+e.getActionCommand()); } else if(e.getActionCommand().equals("+")||e.getActionCommand().equals("-")|| e.getActionCommand().equals("*")||e.getActionCommand().equals("/") ||e.getActionCommand().equals("abs")||e.getActionCommand().equals("sqrt")|| e.getActionCommand().equals("sin")||e.getActionCommand().equals("cos")) {//按的是运算符 c1=Double.parseDouble(DisplayText.getText()); DisplayText.setText(""); switch(e.getActionCommand().charAt(0)){ case '+':flag='p';break; case '-':flag='m';break; case '*':flag='t';break; case '/':flag='d'