源代码:
package shixun;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingConstants;import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.Color;
public class Calculator extends JFrame implements ActionListener {// 第一行:计算式private JTextField expText = new JTextField();//单行文本框// 第二行:计算结果,设初始值为0private JTextField resultText = new JTextField("0");private String num1="",num2="";//两个操作数:数1、数2private String fh=""; //运算符private double r; //计算结果// 构造方法public Calculator() {// 调用父类JFrame的构造函数,设置标题super("计算器");//设置各个按钮上的文字String[] keysValue= {"7","8","9","÷","4","5","6","×","1","2","3","-","0","CE","+","^","sin","cos","tan","=","sqrt","ln"};//设置各个按钮上的动作命令标识String[] actionCmd= {"7","8","9","÷","4","5","6","*","1","2","3","-","0","CE","+","^","sin","cos","tan","=","sqrt","ln"};//创建一个按钮数组JButton keys[]=new JButton[keysValue.length];Font font=new Font("宋体",Font.PLAIN,18);//设置计算式文本框的位置为(10,10),宽为:280,高为:40expText.setBounds(10, 10, 280, 40);expText.setFont(font);expText.setBackground(Color.white);expText.setEditable(false);// 计算式不能修改//设置计算结果文本框的大小resultText.setBounds(10, 50, 280, 40);resultText.setFont(font);resultText.setBackground(Color.white);resultText.setEditable(false);// 计算结果不能修改//设置计算结果文本框的对齐方式:右对齐resultText.setHorizontalAlignment(SwingConstants.RIGHT);// 设置窗口布局this.setLayout(null);//设置为null即为清空布局管理器this.add(expText); // 将计算式文本框添加到窗口中this.add(resultText);// 将计算结果文本框添加到窗口中// 放置按钮int x=10,y=100; for (int i=0;i<keysValue.length;i++) {keys[i]=new JButton();keys[i].setText(keysValue[i]);keys[i].setActionCommand(actionCmd[i]);keys[i].setBounds(x,y,70,45);//按钮的宽为70,高为45keys[i].setFont(font);if(x<=150) {x+=70;} else {x=10;y+=50;}this.add(keys[i]);//将每个按钮添加到窗口中}// 给每个按钮添加监听for (int i=0;i<keysValue.length;i++) {keys[i].addActionListener(this);}// 设置窗口大小this.setSize(310, 440);// 窗口大小不能修改this.setResizable(false);//设置窗口的相对位置,位于屏幕中央this.setLocationRelativeTo(null);//设置窗口的关闭操作:直接关闭this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true); // 设置窗口可见}//计算功能public void result(String z) {if(z.equals("+"))r=Double.parseDouble(num1)+Double.parseDouble(num2);if(z.equals("-"))r=Double.parseDouble(num1)-Double.parseDouble(num2);if(z.equals("*"))r=Double.parseDouble(num1)*Double.parseDouble(num2);if(z.equals("÷"))r=Double.parseDouble(num1)/Double.parseDouble(num2);if(z.equals("sin"))r=Math.sin(Double.parseDouble(num1));if(z.equals("cos"))r=Math.cos(Double.parseDouble(num1));if(z.equals("tan"))r=Math.tan(Double.parseDouble(num1));if(z.equals("^"))r=Math.pow(Double.parseDouble(num1),Double.parseDouble(num2));if(z.equals("ln"))r=Math.log(Double.parseDouble(num1));if(z.equals("sqrt"))r=Math.sqrt(Double.parseDouble(num1));num1=Double.toString(r);//将结果显示在文本框resultText中resultText.setText(num1);//算完后将 数2 和 运算符 清空num2="";fh="";}//重写接口ActionListener里的抽象方法public void actionPerformed(ActionEvent e){//e.getActionCommand()得到的是组件对象上的字符串String cmd=e.getActionCommand();if(cmd.equals("0")||cmd.equals("1")||cmd.equals("2")||cmd.equals("3")||cmd.equals("4")||cmd.equals("5")||cmd.equals("6")||cmd.equals("7")||cmd.equals("8")||cmd.equals("9")) {if(fh.equals("")) {num1+=cmd;expText.setText(num1);}else { num2+=cmd;expText.setText(num1+fh+num2);}}//运算if(cmd.equals("+")) {if(fh!="")result(fh);//调用result函数计算结果,并将结果显示在文本框resultText中fh="+";//在计算式文本框中显示第一个数和符号expText.setText(num1+fh);}if(cmd.equals("-")) {if(fh!="")result(fh);fh="-";expText.setText(num1+fh);}if(cmd.equals("*")) {if(fh!="")result(fh);fh="*";expText.setText(num1+fh);}if(cmd.equals("÷")) {if(fh!="")result(fh);fh="÷";expText.setText(num1+fh);}if(cmd.equals("^")) {if(fh!="")result(fh);fh="^";expText.setText(num1+fh);}if(cmd.equals("sin")) {if(fh!="")result(fh);fh="sin";expText.setText(fh+num1);}if(cmd.equals("cos")) {if(fh!="")result(fh);fh="cos";expText.setText(fh+num1);}if(cmd.equals("tan")) {if(fh!="")result(fh);fh="tan";expText.setText(fh+num1);}if(cmd.equals("sqrt")) {if(fh!="")result(fh);fh="sqrt";expText.setText("√"+num1);}if(cmd.equals("ln")) {if(fh!="")result(fh);fh="ln";expText.setText(fh+num1);}if(cmd.equals("=")) {result(fh);}// "CE"键清空数据if(cmd.equals("CE")) {num1="";num2="";fh="";expText.setText("");resultText.setText("0");}}public static void main(String[] args) {// TODO Auto-generated method stubnew Calculator();}}
运行结果: