java实现简单计算器
功能:简单计算器:两数运算,平方,加负号,实现了复制粘贴功能 * 参考资料:《Java编程思想》
下面是源代码,由于初学Java,许多东西知之甚少,因此代码的质量应该不高,有很多可以改进的地方;
下面是源代码
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;
public class Calculator implements ActionListener{
private static JTextArea text_01, text_02; // 一个显示数据,一个显示“关于计算器”
private static JButton bt[] = new JButton[20]; // 20个按钮
private static double result_01, tempret; // 一个存运算符前面的数字,一个存后面的
private static int n = -1, temp; // n是运算符按下标志
private static String s, stemp; // temp复制粘贴功能用
private static JMenu oprt, help, edit; // 三个菜单组件
private static JMenuItem copy, paste, cut, about, exit; // 5个子菜单
private static DecimalFormat df = new DecimalFormat("#0.00"); // 格式:保留小数点后两位
private static boolean judge_01 = false, judge_02 = false; // 一个小数点按下标志,一个运算符按下标志
public Calculator(){ // 重写构造器,相当于初始化
JFrame fm = new JFrame("计算器"); // 建立框架
fm.setBounds(600, 200, 280, 280); // 框架位置和大小
JMenuBar mb = new JMenuBar(); // 菜单栏
mb.setBackground(Color.LIGHT_GRAY); // 菜单栏颜色
//mb.setBounds(600, 200, 300, 220);
fm.setJMenuBar(mb); // 菜单栏放到框架中
// 3个菜单组件
oprt = new JMenu("操作");
help = new JMenu("帮助");
edit = new JMenu("编辑");
mb.add(oprt); mb.add(help); mb.add(edit);
// 5个子菜单
copy = new JMenuItem("复制");
paste = new JMenuItem("粘贴");
cut = new JMenuItem("剪切");
about = new JMenuItem("关于计算器");
exit = new JMenuItem("退出");
oprt.add(copy); oprt.add(paste); oprt.add(cut); help.add(about); edit.add(exit);
// 两个面板,一个流式布局,一个网格布局
JPanel pl_01 = new JPanel();
JPanel pl_02 = new JPanel();
pl_01.setLayout(new FlowLayout());
pl_02.setLayout(new GridLayout(5, 4, 6, 6));
// 显示
text_01 = new JTextArea(2, 27);
text_01.setFont(new Font("黑体", Font.BOLD, 16));
pl_01.add(text_01);
fm.setContentPane(pl_01); // 面板1放入框架
pl_01.add(pl_02); // 面板2放入面板1
String a[] = {"del", "+/-", "X^2", "+", "7", "8", "9", "-", "4", "5",
"6", "*", "1", "2", "3", "/", "0", ".", "C", "="};
// 通过循环建立按钮对象,
for(int i = 0; i < a.length; i++){
bt[i] = new JButton(a[i]);
pl_02.add(bt[i]);
bt[i].addActionListener(this); // 监听
}
// 运算符颜色
for(int i = 3; i <= 15; i+=4)
bt[i].setForeground(Color.RED);
// 主要是针对菜单栏组件的
show();
fm.setVisible(true);
fm.setResizable(true); // 大小不可调
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
// 初始化
new Calculator();
}
public void actionPerformed(ActionEvent e){
s = e.getActionCommand(); // getActionCommand():以字符串形式返回组件名称
// 数字按钮按下
if("0123456789".indexOf(s) >= 0){
text_01.setText(text_01.getText() + s);
}
// 小数点按钮按下
if(e.getSource() == bt[17] && judge_01 == false && text_01.getText().isEmpty() == false){
// 前提是有数字按下
text_01.setText(text_01.getText() + ".");
judge_01 = true; // 第一个小数点按下标志
}
// 运算符按钮按下
if("+-*/".indexOf(s) >= 0 && judge_02 == false && text_01.getText().isEmpty() == false){
text_01.setText(text_01.getText() + s);
operation();
judge_02 = true; // 运算符按下标志
}
// +/-负号按下
if(e.getSource() == bt[1] && text_01.getText() != "0" && text_01.getText().isEmpty() == false){ // 正负号按钮按下
// 运算符按下之前
if(!judge_02){
text_01.setText("-"+text_01.getText());
}
}
if(e.getSource() == bt[2]){ // 平方按钮按下
if(!judge_02){
result_01 = Double.parseDouble(text_01.getText());
text_01.setText(df.format(result_01*=result_01));
}
}
if(e.getSource() == bt[19]){ // 等号按钮按下
equal();
}
if(e.getSource() == bt[0] && text_01.getText().length() > 0){ // 退格按钮按下
text_01.setText(text_01.getText().substring(0, text_01.getText().length()-1));
// 判断运算符有没有删掉
if(text_01.getText().lastIndexOf("+") > 0 || text_01.getText().lastIndexOf("-") > 0 ||
text_01.getText().lastIndexOf("*") > 0 || text_01.getText().lastIndexOf("/")> 0){
judge_02 = true;
}else{ judge_02 = false; }
}
if(e.getSource() == bt[18]){ // 清空文本
text_01.setText(""); // 清空文本
judge_01 = false;
judge_02 = false;
}
}
public static void show(){
exit.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e){
System.exit(0);
}
});
about.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e){
text_02 = new JTextArea();
text_02.setText("关于计算器:简单计算器\n设计时间:2017年5月5日\n");
JOptionPane.showMessageDialog(null, text_02, "关于计算器", JOptionPane.PLAIN_MESSAGE);
}
});
copy.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e){
if(text_01.getText().isEmpty() == false){
stemp = text_01.getText();
}
}
});
paste.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e){
text_01.setText(text_01.getText()+stemp);
}
});
cut.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e){
if(text_01.getText().isEmpty() == false){
stemp = text_01.getText();
text_01.setText("");
}
}
});
}
public static void operation(){
// 运算符按下时,将运算符前的字符保存成double暂存在result_01
result_01 = Double.parseDouble(text_01.getText().substring(0, text_01.getText().length()-1));
if(s.equals("+"))
n = 0;
if(s.equals("-"))
n = 1;
if(s.equals("*"))
n = 2;
if(s.equals("/"))
n = 3;
}
public static void equal(){
if(n == 0){ // 加号按下
// temp找到“+”所在下标,下标后面就是被加数,被加数保存到tempret中
temp = text_01.getText().indexOf("+");
if(temp < text_01.getText().length()-1){
tempret = Double.parseDouble(text_01.getText().substring(temp+1,text_01.getText().length()));
//System.out.println(result_01);
result_01+= tempret;
s = df.format(result_01); // 格式统一
}else // 如果按下“+”,但是没有被加数时,显示“格式错误”,
s = "格式错误";
//System.out.println(tempret);
}else if(n == 1){
if(temp < text_01.getText().length()-1){
//indexOf(int ch, int fromIndex) 返回指定字符索引,从指定位置搜索
temp = text_01.getText().indexOf("-", 1); // lastindexOf从后面搜索也可以
tempret = Double.parseDouble(text_01.getText().substring(temp+1,text_01.getText().length()));
result_01-= tempret;
s = df.format(result_01);
}else
s = "格式错误";
}else if(n == 2){
if(temp < text_01.getText().length()-1){
temp = text_01.getText().indexOf("*");
tempret = Double.parseDouble(text_01.getText().substring(temp+1,text_01.getText().length()));
//System.out.println(result_01);
result_01*= tempret;
s = df.format(result_01);
}else
s = "格式错误";
}else if(n == 3){
if(temp < text_01.getText().length()-1){
temp = text_01.getText().indexOf("/");
tempret = Double.parseDouble(text_01.getText().substring(temp+1,text_01.getText().length()));
//System.out.println(result_01);
result_01/= tempret;
s = df.format(result_01);
}else
s = "格式错误";
}else {
// 这是运算符也没按下的情况,比如按下56,则=56
s = text_01.getText();
}
n = -1; // 运算符按下标志取消
//s = df.format(result_01);
text_01.setText(text_01.getText()+" = "+s);
}
}