这个学期的期末,因为之前没有写过JAVA的程序,所以有不妥之处欢迎提出~
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 | // Calculator.java import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.math.BigDecimal; public class Calculator extends implements { private boolean ifAction; private double ansPre; private ansStr = ""; private int sign = 0; // 1: + 2: - 3: x 4: / private boolean ifDot = true; private textField; private fnt = new ("Monospace", .PLAIN, 26); private ButtonPanel = new (); private TextPanel = new (); private [] = new [20]; private mb = new (); private fileMenu = new ("文件"); private aboutMenu = new ("关于"); private [] filem = {new ("退出")}; private [] aboutm = {new ("关于作者")}; name[] = {"Bksp", "CE", "Clr", "+/-", " 7 ", " 8 ", " 9 ", " / ", " 4 ", " 5 ", " 6 ", " x ", " 1 ", " 2 ", " 3 ", " - ", " 0 ", " . ", " = ", " + "}; public Calculator() { super("计算器"); fileMenu.add(filem[0]); aboutMenu.add(aboutm[0]); filem[0].addActionListener(this); aboutm[0].addActionListener(this); mb.add(fileMenu); mb.add(aboutMenu); setJMenuBar(mb); textField = new (15); textField.setText("0"); textField.setEditable(false); textField.setFont(fnt); textField.setBackground(.white); textField.setHorizontalAlignment(.RIGHT); TextPanel.add(textField); for (int i = 0; i < name.length; i++) { [i] = new (name[i]); [i].addActionListener(this); } setLayout(new (10, 10)); add(TextPanel, .NORTH); TextPanel.setBorder(.createEmptyBorder(20,10,10,10)); for (int i = 0; i < name.length; i++) { ButtonPanel.add([i]); } ButtonPanel.setBorder(.createEmptyBorder(0,10,30,10)); add(ButtonPanel, .CENTER); ButtonPanel.setLayout(new (5, 4, 10, 10)); } public void initAll() { ansStr = ""; sign = 0; ifDot = true; ifAction = false; refresh(); } public void init() { ansStr = ""; ifDot = true; refresh(); } public void refresh() { if (ansStr.length() == 0) { textField.setText("0"); } else { textField.setText(ansStr); } } public void cal() { if (sign == 1) { ansPre = ansPre + .parseDouble(ansStr); } else if (sign == 2) { ansPre = ansPre - .parseDouble(ansStr); } else if (sign == 3) { ansPre = ansPre * .parseDouble(ansStr); } else if (sign == 4) { ansPre = ansPre / .parseDouble(ansStr); } bd = new (ansPre); bd=bd.setScale(13, .ROUND_HALF_UP); ansPre = bd.doubleValue(); if (sign != 0) { if ((ansPre - (int)ansPre == 0)) { ansStr = "" + (int)ansPre; } else { ansStr = "" + ansPre; } } sign = 0; } public void actionPerformed ( e) { B = e.getActionCommand(); if (B.charAt(1) == '0' && (ansStr.length() < 15 || ifAction)) { if (ifAction) { init(); ifAction = false; } if (!(ansStr.length() == 1 && ansStr.charAt(0) == '0')) { ansStr = ansStr + B.charAt(1); } } else if ((B.charAt(1) >= '1' && B.charAt(1) <= '9') && (ansStr.length() < 15 || ifAction)) { if (ifAction) { init(); ifAction = false; } if (ansStr.length() == 1 && ansStr.charAt(0) == '0') { ansStr = "" + B.charAt(1); } else { ansStr = ansStr + B.charAt(1); } } else if (B.charAt(0) == 'B' && ansStr.length() > 0) { int len = ansStr.length(); if (ansStr.charAt(len - 1) == '.') { ifDot = true; } ansStr = ansStr.substring(0, len - 1); } else if (B.charAt(0) == '+') { if (ansStr.charAt(0) == '-') { int len = ansStr.length(); ansStr = ansStr.substring(1, len); } else if (ansStr.length() > 0){ ansStr = '-' + ansStr; } } else if (B.charAt(1) == '.' && ifDot) { ansStr = ansStr + B.charAt(1); ifDot = false; } else if (B.charAt(1) == 'l') { initAll(); } else if (B.charAt(1) == 'E') { init(); } else if (B.charAt(1) == '+') { if (!ifAction) { if (sign != 0) { cal(); } ansPre = .parseDouble(ansStr); } sign = 1; ifAction = true; } else if (B.charAt(1) == '-') { if (!ifAction) { if (sign != 0) { cal(); } ansPre = .parseDouble(ansStr); } sign = 2; ifAction = true; }else if (B.charAt(1) == 'x') { if (!ifAction) { if (sign != 0) { cal(); } ansPre = .parseDouble(ansStr); } sign = 3; ifAction = true; }else if (B.charAt(1) == '/') { if (!ifAction) { if (sign != 0) { cal(); } ansPre = .parseDouble(ansStr); } sign = 4; ifAction = true; } else if (B.charAt(1) == '=') { cal(); ifAction = true; sign = 0; } else if (B.compareTo("退出") == 0) { .exit(0); } else if (B.compareTo("关于作者") == 0) { .showMessageDialog(null, " 作者:CoderMAX\n 专业:软件工 程\n Blog: www.codermax.com", "关于作者", .PLAIN_MESSAGE); } refresh(); } public static void main([] args) { Calculator frame = new Calculator(); frame.setTitle("计算器 By CoderMAX"); frame.setSize(390, 370); frame.setVisible(true); frame.setResizable(false); frame.setDefaultCloseOperation(.EXIT_ON_CLOSE); } } |
如果需要期末报告留下邮箱~
原创文章,转载请注明来源并保留原文链接
本文链接:
我们也刚学完java,java要是编写windows小程序比mfc方便多了。容器的确很好用。
[]
回复:
二月 18th, 2010 at 10:36 下午
但是JAVA比C++效率低啊-。=做做小程序和外壳还可以- -
[]
回复:
二月 18th, 2010 at 10:57 下午
我现在想想,其实这个问题要从两个方向思考,一个是开发效率,一个是运行效率,有时候开发效率会更重要,google地球和Picasa在linux下的就是用java开发的~
[]
我似乎也写过类似的,N年前。。。
[]
回复:
二月 25th, 2010 at 1:02 下午
这东西就应付应付考试拉,没有实质意义~
[]
回复:
二月 25th, 2010 at 1:45 下午
我那时候也是啊,记得只能按一定规律去按,否则结果就有可能不对了
[]
回复:
二月 25th, 2010 at 2:35 下午
我这个还可以,用BigDecimal提高精度,可以正常使用的,也不用考虑按键顺序,相对还是比较xx的~
[]