前提
本计算器是基于Swing编写。可以完成加法、减法、乘法、除法和取余运算。且有小数点、退格、清零和求倒数功能。
1.启动类
public class GuiCaculator {
// 启动类
public static void main(String[] args) {
new CaculatorPanel().init();
}
}
2.常量类
// 常量都放在这里,以后只需要修改这里即可。 并且该常量都要设置为静态变量,这样别人才能访问到
public class Const {
// 获取屏幕的宽度和高度
public static int Screen_W= Toolkit.getDefaultToolkit().getScreenSize().width;
public static int Screen_Y=Toolkit.getDefaultToolkit().getScreenSize().height;
// 获取窗口的宽度和高度
public static int Frame_W=300;
public static int Frame_Y=300;
// 获取中心点位置
public static int frame_x=(Screen_W-Frame_W)/2;
public static int frame_y=(Screen_Y-Frame_Y)/2;
// 计算机标题
public static String Title="简易计算器";
// 面板按钮顺序 %是求余 X 是退格 R是reciprocal 倒数
public static String btnString="+-*/789C456X123R0.%=";
// 正则表达式
public static String regex="[\\+\\-*%/.R=CX]";
}
3.计算器类
// 计算器类
class CaculatorPanel extends JFrame implements ActionListener {
private String operator=null; // 操作符
private String firstInput=null; // 第一次输入的值
private Double firstNum=0d; // 第一个操作数
private Double secondNum=0d; // 第二个操作数
private Double result=0d; // 结果
private String wrongNum; // 错误数字,用于 X 删除的时候
private String rightNum; // 正确的数字,用于 X 删除的时候
private int length; // 错误数字的长度
JTextArea jTextArea = new JTextArea(8, 24);
public void init() {
// 获取容器
Container container = this.getContentPane();
JPanel jPanel = new JPanel();
JPanel jPane2 = new JPanel();
// 显示计算的结果,上面的面板
jTextArea.setFont(new Font("monospaced", Font.BOLD, 20));
jPanel.add(jTextArea);
// 下面的面板
jPane2.setLayout(new GridLayout(5,4));
for (int i = 0; i < Const.btnString.length(); i++) {
String btnString=Const.btnString.substring(i, i+1);
JButton jButton = new JButton();
jButton.setText(btnString);
jButton.setFont(new Font("微软雅黑",Font.PLAIN , 16));
if(btnString.matches(Const.regex)){
jButton.setForeground(Color.RED);
}
jButton.addActionListener(this);
jPane2.add(jButton);
}
container.setLayout(new GridLayout(2, 1));
container.add(jPanel);
container.add(jPane2);
setTitle(Const.Title);
setResizable(false);
setSize(Const.Frame_W, Const.Frame_Y);
setLocation(Const.frame_x, Const.frame_y);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String clickStr=e.getActionCommand();
// 清空
if(clickStr.equals("C")){
this.jTextArea.setText("");
}
else if("X".equals(clickStr)){
wrongNum=this.jTextArea.getText();
length=wrongNum.length();
// 为什么是length减1? 比如 15,长度是2,subString(0,2)从0开始到1,两位,而我要的是一位,故长度减1。subString(0,2)包含0,不包含2
rightNum=wrongNum.substring(0, length-1);
System.out.println(rightNum);
this.jTextArea.setText(rightNum);
}
// public int indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
else if(".0123456789".indexOf(clickStr)!=-1){
// 把监听到的值,连续显示到页面上去。故需要原本的值加上现在监听到的值
this.jTextArea.setText(jTextArea.getText()+clickStr);
}
// 当点到+-/*%记录下面的值
else if(clickStr.matches("[\\+\\-*/%]{1}")){
// 记录操作符
operator=clickStr;
// 记录第一个输入的值
firstInput=this.jTextArea.getText();
// 清空
this.jTextArea.setText("");
}
// 当点到 等号的操作
else if(clickStr.equals("=")){
firstNum=Double.valueOf(firstInput);
secondNum=Double.valueOf(this.jTextArea.getText());
switch (operator){
case "+":
result=firstNum+secondNum;
break;
case "-":
result = firstNum - secondNum;
break;
case "*":
result=firstNum*secondNum;
break;
case "/":
if (secondNum!=0)
result=firstNum/secondNum;
break;
case "%":
if (secondNum!=0)
result=firstNum%secondNum;
break;
}
this.jTextArea.setText(result.toString());
}
else if("R".equals(clickStr)&&result!=0) {
result = 1 / result;
this.jTextArea.setText(result.toString());
}
}
}