功能:实现加减乘除,取余,取倒数,取平方根,退格,清零,清除,显示作者以及单位功能
代码使用方法:可以使用Eclipse直接进行编译运行,也可以放在一个新建文件夹中新建一个Calc.java文件(文件名必须为Calc,扩展名必须为java才可以),将下面的代码复制粘贴进去,保存,然后打开命令行窗口,cd到该文件夹下面,执行 javac Calc.java 命令编译,然后java Calc命令运行,效果如上。
111537903.png

由于编译之后会产生多个class文件,所以建议将Calc.java文件新建到一个新建的文件夹下。

111559646.png

(新手编写,欢迎评价)


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;

public class Calc {
public double a; //作为运算的第一个参数
public double b; //作为运算的第二个参数
public String input; //接收用户通过按钮给出的输入
public StringBuffer output; //用来盛放显示框上显示的字符串
public String oper; //用来记录用户按下的运算符 +-*/%
public boolean isNull; //用来标识output是否为空
//构造方法
public Calc(){
b = 0.0;
output = new StringBuffer("");
oper = "";
isNull = true;
final JFrame jf = new JFrame("简单计算器");
final JTextField jt = new JTextField();
jt.setEditable(false);
jt.setPreferredSize(new Dimension(450,40));
jt.setHorizontalAlignment(JTextField.RIGHT);
Panel p1 = new Panel();

JButton jBack = new JButton("BACK");
jBack.setPreferredSize(new Dimension(105,40));
//设置退格键的监听器
jBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(!output.toString().equals("")){
output.deleteCharAt(output.length()-1);
jt.setText(output.toString());
}

}
});
//设置CE键的监听器
JButton jCE = new JButton("CE");
jCE.setPreferredSize(new Dimension(105,40));
jCE.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
jt.setText("0");
output = new StringBuffer("");
}
});
JButton jC = new JButton("C");
//设置C键的监听器
jC.setPreferredSize(new Dimension(105,40));
jC.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
jt.setText("0");
a = 0.0;
b = 0.0;
oper = "";
input = null;
output = new StringBuffer("");
}
});
//设置About退格键的监听器
JButton jAbout = new JButton("About");
jAbout.setPreferredSize(new Dimension(105,40));
jAbout.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {
JDialog jd = new JDialog(jf,"About");
jd.setModal(true);
jd.setSize(200,120);
jd.setLocation(300,250);
JLabel l = new JLabel("<html>From LiuWei<br>From SDNU",JLabel.CENTER);
//l.setLayout(BorderLayout.CENTER);
jd.add(l);
l.setVisible(true);
jd.setVisible(true);


}
});
p1.add(jBack);
p1.add(jCE);
p1.add(jC);
p1.add(jAbout);
jf.add(jt,BorderLayout.NORTH);
jf.add(p1);
String[] jbn = {"7","8","9",
"4","5","6",
"1","2","3",
"-","0","." };
Panel p2 = new Panel();
p2.setLayout(new GridLayout(4,3));
p2.setSize(260, 200);
JButton jb = null;
ActionListener al = null;
//将数字键添加到JFrame中,添加相同的监听器
for (int i = 0; i < jbn.length; i++) {
jb = new JButton(jbn[i]);
jb.setPreferredSize(new Dimension(70,40));
jb.setName(jbn[i]);
al = new MyNumActionListener(jb,jt);
jb.addActionListener(al);
p2.add(jb);
}
al = null;
Panel p3 = new Panel();
p3.setPreferredSize(new Dimension(160,200));
p3.setLayout(new GridLayout(4,2));
String[] jbc = {"/","sqrt",
"*","%",
"-","1/x",
"+","="};
//将运算符按钮添加到JFrame中,并添加监听器
for (int i = 0; i < jbc.length; i++) {
jb = new JButton(jbc[i]);
jb.setName(jbc[i]);
//如果是+-*/%这五个基本运算符,则添加MyOperActionListener监听器
if(!(jbc[i].equals("=")||jbc[i].equals("sqrt")||jbc[i].equals("1/x"))){
al = new MyOperActionListener(jb,jt);
//如果是=号,则添加MyEqualActionListener监听器
}else if(jbc[i].equals("=")){
al = new MyEqualActionListener(jb,jt);
//如果是1/x,sqrt运算符,则添加MyOtherActionListener监听器
}else{
al = new MyOtherActionListener(jb,jt);
}
jb.addActionListener(al);
jb.setPreferredSize(new Dimension(70,40));
p3.add(jb);  //将按钮逐个添加进JFrame中
}
GridLayout fl = new GridLayout(1,2);
fl.setHgap(10);
Panel p4 = new Panel();
p4.setLayout(fl);
p4.add(p2);
p4.add(p3);
jf.add(p4,BorderLayout.SOUTH);
jf.setBounds(200, 150, 460, 340);
jf.setResizable(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
jf.setExtendedState(JFrame.MAXIMIZED_HORIZ);
}
//算法器,运算数是否合法在调用的时候去检测
public double calc(double a,double b,String oper){
if(oper.equals("+")) return a+b;
if(oper.equals("-")) return a-b;
if(oper.equals("/")) return a/b;
if(oper.equals("*")) return a*b;
if(oper.equals("%")) return a%b;
if(oper.equals("sqrt")) return Math.sqrt(a);
if(oper.equals("1/x")) return 1/a;
return 0;
}
public static void main(String[] args) {
Calc ct = new Calc();

}
class MyNumActionListener implements ActionListener{

private JButton jb ;
private JTextField jt;
public MyNumActionListener(JButton jb,JTextField jt){
this.jb = jb;
this.jt = jt;
}
public void actionPerformed(ActionEvent e) {
//System.out.println(jb.getName() + "被点击了!");
input = jb.getName();
output.append(input);
isNull = false;
//System.out.println(output);
jt.setText(output.toString());
}

}
//四个内部类,四个具有特殊功能的监听器,是本小程序的核心,也是笔者花费时间最多的地方
class MyOperActionListener implements ActionListener{

private JButton jb ;
private JTextField jt;
public MyOperActionListener(JButton jb,JTextField jt){
this.jb = jb;
this.jt = jt;
}
public void actionPerformed(ActionEvent e) {
if(input == null) return;
input = jb.getName();
oper = input;
if(!isNull){
a = Double.parseDouble(output.toString());
}
//System.out.println("a是" + CalcT.a);
//System.out.println(output);
//jt.setText(output.toString());
output = new StringBuffer("");
isNull = true;
}
}

class MyEqualActionListener implements ActionListener{
private JButton jb ;
private JTextField jt;
public MyEqualActionListener(JButton jb,JTextField jt){
this.jb = jb;
this.jt = jt;
}
public void actionPerformed(ActionEvent e) {
if(oper.equals("")||input.equals("+")||input.equals("-")
||input.equals("*")||input.equals("/")||input.equals("%")){
return;
}
//如果上次刚按下了“=”,则直接返回。
if(!isNull) b = Double.parseDouble(output.toString());
if((oper.equals("/")&&b==0)){
output = new StringBuffer("");
a = 0.0;
b = 0.0;
oper = "";
input = null;
jt.setText("finilly");
return;
}
a = calc(a,b,oper);
b = 0.0;
//System.out.println("运算器执行!");
output = new StringBuffer(new Double(a).toString());
//System.out.println(output);
jt.setText(output.toString());
oper = ""; //运算符置为空,防止连续两次按下“=”导致错误的多次运算
output = new StringBuffer("");
isNull = true;

}
}

class MyOtherActionListener implements ActionListener{

private JButton jb ;
private JTextField jt;
public MyOtherActionListener(JButton jb,JTextField jt){
this.jb = jb;
this.jt = jt;
}
public void actionPerformed(ActionEvent e) {
if(output.toString().equals(""))
output.append(a);
a = Double.parseDouble(output.toString());
a = calc(a,b,jb.getName());
//System.out.println("运算器执行!");
output = new StringBuffer(new Double(a).toString());
//System.out.println(output);
jt.setText(output.toString());
output = new StringBuffer("");
}
}
}