小程序-计算器的实现,与比较
程序代码:
import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextArea; public class Calculator { public static void main(String[] args) { new MyFrame(); } } class MyFrame extends JFrame{ private JTextArea txt = new JTextArea(2,21); private String title = "陈瑞丰的计算器"; private ArrayList<String> numbs = new ArrayList<>(10); private String numbers ="1234567890."; private String operators ="-+*/"; public MyFrame(){ setSize(250,220); setTitle(this.title);//固定其标题为“陈瑞丰的计算器”; setResizable(false);//不可修改窗体大小; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗口的关闭按钮的功能; txt.setEditable(false); numbs.add("0");//防止出现空指针,另外,在进行计算时,如果首先输入运算符,则默认第一个数字为0; /*以下为lambda表达式的方式创建的内部类的实现,是实现的ActionListener接口*/ ActionListener actionListener2 =(ActionEvent e)->{ // "+-*/" 的动作监听器; String label = ((JButton) e.getSource()).getText();//获得当前按钮上的信息; txt.setText(myString(numbs)+label); numbs.add(label); }; ActionListener actionListener =(ActionEvent e)->{ //数字键的动作监听器; String ele = numbs.get(numbs.size()-1); //获得当前存储的集合的最后一个元素; String label = ((JButton) e.getSource()).getText();//获得当前按钮上的信息; txt.setText(myString(numbs)+label); if(operators.contains(ele)) numbs.add(label); else numbs.set(numbs.size()-1,ele+label); //System.out.println(numbs); //观察数组是否正确传入到数组中去了; }; /* 使用for循环简并代码以后出现了不可预期的输出结果,将setVisible和setLayout的位置放置到代码的后段,问题得以改善; * 以下代码为创建20个操作按钮,并添加它们的动作监听器;添加组件的顺序会直接影响到组件在框架中的排布,这需要注意*/ ArrayList<JButton> btns = new ArrayList<>(20); add(txt); String[] labels = new String[]{"7","8","9","/","√","4","5","6","*","<-","1","2","3","-","CE","0",".","sin","+","="}; for(int i=0;i<20;i++) { btns.add(new JButton(labels[i])); JButton ele =btns.get(i); add(ele); if(numbers.contains(labels[i])) ele.addActionListener(actionListener); else if(operators.contains(labels[i])) ele.addActionListener(actionListener2); } //设置√的动作监听器; btns.get(4).addActionListener((ActionEvent e)->{ String ele = numbs.get(numbs.size()-1); //获得当前存储的集合的最后一个元素; Double num = 0.0; if(operators.contains(ele)) numbs.add(""+num); else { num=Math.sqrt(Double.parseDouble(ele)); numbs.set(numbs.size()-1,""+num); } txt.setText(myString(numbs)+"\n"+"√("+ele+")="+num); }); //设置<-的动作监听器; btns.get(9).addActionListener((ActionEvent e)->{ numbs.remove(numbs.size()-1); txt.setText(myString(numbs)); }); //设置CE的动作监听器; btns.get(14).addActionListener((ActionEvent e)->{ numbs.removeAll(numbs); numbs.add("0"); txt.setText(myString(numbs)); }); //设置sin的动作监听器; btns.get(17).addActionListener((ActionEvent e)->{ String ele = numbs.get(numbs.size()-1); //获得当前存储的集合的最后一个元素; Double num = 0.0; //如果在一个运算符号后输入sin或者√,则默认计算0; if(operators.contains(ele)) numbs.add(""+num); else { num=Math.sin(Double.parseDouble(ele)); numbs.set(numbs.size()-1,""+num); } txt.setText(myString(numbs)+"\n"+"sin("+ele+")="+num); }); //设置=的动作监听器; btns.get(19).addActionListener((ActionEvent e)->{ ArrayList<String> numbs2=numbs; //创建一个新的数组,作为原数组的拷贝; String ele = numbs2.get(numbs2.size()-1); Double num = 0.0; if(operators.contains(ele)) numbs2.add(""+num); while(numbs2.contains("*")||numbs2.contains("/")){ for(int i=0;i<numbs2.size();i++){ if("/".equals(numbs2.get(i))){ numbs2.set(i,(Double.parseDouble(numbs2.get(i-1))/Double.parseDouble(numbs2.get(i+1)))+""); numbs2.remove(i+1);numbs2.remove(i-1);//注意,这里要先删除i+1,再删除i-1,否则必定出错; } if("*".equals(numbs2.get(i))){ numbs2.set(i,(Double.parseDouble(numbs2.get(i-1))*Double.parseDouble(numbs2.get(i+1)))+""); numbs2.remove(i+1);numbs2.remove(i-1);//注意,这里要先删除i+1,再删除i-1,否则必定出错; } }} for(int i=0;i<numbs2.size();i++){ if("-".equals(numbs2.get(i))){ double the =-Double.parseDouble(numbs2.get(i+1)); numbs2.set(i+1,the+""); numbs2.remove(i); } } double result=0; for(int i=0;i<numbs2.size();i++){ if(!operators.contains(numbs2.get(i))){ result=result+Double.parseDouble(numbs2.get(i)); } } txt.setText(myString(numbs)+"\n"+"result="+result); numbs.removeAll(numbs); numbs.add(result+""); }); setLayout(new FlowLayout(FlowLayout.CENTER));//设置按钮的布局为与开始边对齐; setVisible(true); } private String myString(ArrayList<String> lst){ //用以显示在txt上的数组; String rt = ""; for(String ele:lst) rt=rt+ele; return rt; } }
这个本来是借鉴网上https://www. ;不过写的很不一样,大家可以比较一下,至于算法我写的很粗糙,欢迎各位提出更优化的算法,谢谢!!