005classCounterextendsJFrameimplementsActionListener{006
007 //创建面板
008 privateJPanel jp =newJPanel ();009 //创建标签
010 privateJLabel jl =newJLabel();011
012 //创建运算按钮数组
013 privateJButton []jb2=newJButton[]{
014 newJButton(\"+\"),newJButton(\"-\"),newJButton(\"*\"),015 newJButton(\"/\"),newJButton(\"=\"),newJButton(\".\")016 };017 018
019 //创建数字按钮数组
JButton jb1[]=newJButton[]020
{newJButton(\"0\"),newJButton(\"1\"),newJButton(\"2\"),021
newJButton(\"3\"),newJButton(\"4\"),newJButton(\"5\"),newJButton(\"6
022 newJButton(\"7\"),newJButton(\"8\"),newJButton(\"9\")};023 024
025 026 027
028 publicCounter(){029 //设置布局管理器030 jp.setLayout(null);
031 //设置标签的位置大小、并添加到面板中032 jl.setBounds(25,10,350,50);033 jp.add(jl);034
035 //处理数字按钮的位置和大小036 for(inti =0;i<=9;i++)037 {
038 if(i==0){
039 jb1[i].setBounds(0,210,100,50);040 }
041 elseif(i>=1&&i<=3){
042 jb1[i].setBounds(0+100*(i-1),160,100,50);043 }
044 elseif(i>=4&&i<=6){
045 jb1[i].setBounds(0+100*(i-4),110,100,50);046 }
047 elseif(i>=7&&i<=9){
048 jb1[i].setBounds(0+100*(i-7),60,100,50);049 }
050 //为按钮注册
051 jb1[i].addActionListener(this);052 //将按钮添加到JPanel中053 jp.add(jb1[i]);0 }055
056 //处理运算按钮的 位置和大小057 for(inti=0;i<6;i++){058 if(i<4){
059 jb2[i].setBounds(300,60+50*i,100,50);060 }
061 elseif(i==4){
062 jb2[i].setBounds(200,210,100,50);063 }0 else{
065 jb2[i].setBounds(100,210,100,50);066 }
067 jb2[i].addActionListener(this);068 jp.add(jb2[i]);069 070 }071
072 //将面板添加到窗体中073 this.add(jp);
074 //设置窗体的可见性,位置大小。
075 this.setVisible(true);
076 this.setBounds(100,100,400,290);077 //设置窗体的标题,并设为不可改变大小078 this.setTitle(\"计算器\");079 this.setResizable(false);080 081 }
082 //实现接口
083 staticString str=\"\";084 staticdoublei=0;085 staticintn=0;086 087
088 publicvoidactionPerformed(ActionEvent e){0 090
091 for(inti=0;i<10;i++){092 if(e.getSource()==jb1[i]){093 str +=i;094 jl.setText(str);095 }096 }097 098
099 if(e.getSource()==jb2[0]){ //加运算
100 i+=Integer.parseInt(str);101 str=\"\";102 jl.setText(\"\");103 104 n=1;105 }
106 if(e.getSource()==jb2[1]){ //减运算107 i+=Integer.parseInt(str);108 str=\"\";109 jl.setText(\"\");110 111 n=2;112 }
113 if(e.getSource()==jb2[2]){ //乘运算114 i+=Integer.parseInt(str);115 str=\"\";116 jl.setText(\"\");117 118 n=3;119 }
120 if(e.getSource()==jb2[3]){ //除运算121 i+=Integer.parseInt(str);122 str=\"\";123 jl.setText(\"\");124
125 n=4;126 }
127// Integer.MIN_VALUE(即能够表示的最小负 int 值)128 129 /*
130 * Math.abs(Integer.MIN_VALUE)的值还是其本身。131 * 通过查阅Java的API文档,我们看到对abs(int a)运算,132
* “如果参数等于 Integer.MIN_VALUE 的值(即能够表示的最小int 值),
133 * 那么结果与该值相同且为负。”所以会有这样的结果。134 */
135 if(e.getSource()==jb2[4]){136 //jl.setText(\"\"+getI(n));137
138 //getI(n)函数被调用了两次
139 /* if(getI(n)==Integer.MIN_VALUE ){140 jl.setText(\"除数不能为0!!\");141 }142 else{
143 jl.setText(\"\"+getI(n));144 }*/
145 doublem = getI(n);
146 if(m==Integer.MIN_VALUE ){147 jl.setText(\"除数不能为0!!\");148 }149 else{
150 jl.setText(\"\"+m);151 }152 153 }1 }155 156
157 publicdoublegetI(intx){158 if(x==1){
159 i+=Integer.parseInt(str);160 str=\"0\";161 }162
163 if(x==2){
1 i-=Integer.parseInt(str);165 str=\"0\";166 }
167 if(x==3){
168 i*=Integer.parseInt(str);169 str=\"0\";170 }
171 if(x==4){
172 if(Integer.parseInt(str)==0.0){173 i=Integer.MIN_VALUE ;174 //jl.setText(\"除数不能为0\");
175 }176 else{
177 i/=Integer.parseInt(str);178 }179 str=\"0\";180 }181
182 returni;183 }184}185 186
187publicclassCounterUGI {
188 publicstaticvoidmain(String[]args){1 Counter counter =newCounter();190 }191 192}
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- igbc.cn 版权所有 湘ICP备2023023988号-5
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务