1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.lang.*; public class awtQi extends JFrame implements ActionListener { private JButton s1,s2,s3,s4,s5,s6,s7,s8,s9,s0,b1,b2,b3,b4,f1,f2; private JTextField resultField; private Panel pNorth,pCenter,pSouth; private boolean end,add,sub,mul,div; private String str; private double num1,num2; JLabel lblStatusbar; public awtQi() { pSouth=new Panel(); pNorth=new Panel(); //面板布局 pCenter=new Panel(); lblStatusbar=new JLabel(“【小项目系列作品-计算器】 编写者:朱峰 班级:06计信1班 “); resultField=new JTextField(“0″); JButtonListener(); add(pNorth,BorderLayout.NORTH); add(pCenter,BorderLayout.CENTER); add(pSouth,BorderLayout.SOUTH); pNorth.setLayout(new GridLayout(1,1)); pNorth.add(resultField); pSouth.add(lblStatusbar); pCenter.setLayout(new GridLayout(4,4)); addButton(); } private void addButton() //改方法在jPanel上添加按钮组 { pCenter.add(s1); pCenter.add(s2); pCenter.add(s3); pCenter.add(b1); pCenter.add(s4); pCenter.add(s5); pCenter.add(s6); pCenter.add(b2); pCenter.add(s7); pCenter.add(s8); pCenter.add(s9); pCenter.add(b3); pCenter.add(s0); pCenter.add(f1); pCenter.add(f2); pCenter.add(b4); } private void JButtonListener() //该方法创建按钮,并且注册监听事件到listenerQi类中 { s1=new JButton(“ 1 “); s1.addActionListener(this); s2=new JButton(“ 2 “); s2.addActionListener(this); s3=new JButton(“ 3 “); s3.addActionListener(this); s4=new JButton(“ 4 “); s4.addActionListener(this); s5=new JButton(“ 5 “); s5.addActionListener(this); s6=new JButton(“ 6 “); s6.addActionListener(this); s7=new JButton(“ 7 “); s7.addActionListener(this); s8=new JButton(“ 8 “); s8.addActionListener(this); s9=new JButton(“ 9 “); s9.addActionListener(this); s0=new JButton(“ 0 “); s0.addActionListener(this); b1=new JButton(“ + “); b1.addActionListener(this); b2=new JButton(“ - “); b2.addActionListener(this); b3=new JButton(“ * “); b3.addActionListener(this); b4=new JButton(“ / “); b4.addActionListener(this); f1=new JButton(“ . “); f1.addActionListener(this); f2=new JButton(“ = “); f2.addActionListener(this); } public static void main(String[] args) { awtQi test=new awtQi(); test.setSize(400,300); test.setVisible(true); } public void num(int i) //决定显示的数值 { String s=null; s=String.valueOf(i); if(end) { resultField.setText(“0″); end=false; } if((resultField.getText()).equals(“0″)) resultField.setText(s); else { s=resultField.getText()+s; resultField.setText(s); } } public void sign(int s) { if (s==1) { add=true; sub=false; mul=false; div=false; } else if (s==2) { add=false; sub=true; mul=false; div=false; } else if (s==3) { add=false; sub=false; mul=true; div=false; } else if (s==4) { add=false; sub=false; mul=false; div=true; } num1=Double.parseDouble(resultField.getText()); System.out.println(“第一个数字已产生:”+num1); end=true; } public void actionPerformed(ActionEvent e) { if (e.getSource()==s1) //以下为0-9的输入 num(1); else if (e.getSource()==s2) num(2); else if (e.getSource()==s3) num(3); else if (e.getSource()==s4) num(4); else if (e.getSource()==s5) num(5); else if (e.getSource()==s6) num(6); else if (e.getSource()==s7) num(7); else if (e.getSource()==s8) num(8); else if (e.getSource()==s9) num(9); else if (e.getSource()==s0) num(0); else if(e.getSource()==b1) //以下为+,-,*,/的输入 sign(1); else if(e.getSource()==b2) sign(2); else if(e.getSource()==b3) sign(3); else if(e.getSource()==b4) sign(4); else if(e.getSource()==f1) //判断.的输入 { str=resultField.getText(); if(str.indexOf(“.”)<=1) str=str+“.”; resultField.setText(str); } else if(e.getSource()==f2) //按下=号时候的计算 { num2=Double.parseDouble(resultField.getText()); System.out.println(“第二个数字已产生:”+num2); if(add) num1=num1+num2; else if(sub) num1=num1-num2; else if(mul) num1=num1*num2; else if(div) num1=num1/num2; System.out.println(“结果为:”+num1); printQi(); } } private void printQi() { resultField.setText(String.valueOf(num1)); //显示最终的结果 end=true; } } |
计算器源码-java
作者: fred
日期: 2007 年 12 月 22 日
评论关闭。