Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.34 KB | None | 0 0
  1. package ch9;
  2. import java.awt.event.*;
  3. import java.awt.*;
  4. import javax.swing.*;
  5. public class Q1 extends JFrame implements ActionListener{
  6. static Q1 frm=new Q1();
  7. static JLabel label =new JLabel("java");
  8. static JTextField txt =new JTextField();
  9. static JButton b0=new JButton("0");
  10. static JButton b1=new JButton("1");
  11. static JButton b2=new JButton("2");
  12. static JButton b3=new JButton("3");
  13. static JButton b4=new JButton("4");
  14. static JButton b5=new JButton("5");
  15. static JButton b6=new JButton("6");
  16. static JButton b7=new JButton("7");
  17. static JButton b8=new JButton("8");
  18. static JButton b9=new JButton("9");
  19. static JButton b10 = new JButton("+");
  20. static JButton b11 = new JButton("-");
  21. static JButton b12 = new JButton("*");
  22. static JButton b13 = new JButton("/");
  23. static JButton b14 = new JButton("C");
  24. static JButton b15 = new JButton("=");
  25.  
  26. static int tmp =0;//sum
  27. static char tmp2;//紀錄上一個符號
  28. public static void main(String arg[])
  29. {
  30. Container container = frm.getContentPane();
  31. frm.setLayout(null);
  32. //各元件基礎設定
  33. b1.setBounds(20,200,50,50);
  34. b1.addActionListener(new Actlis());
  35. b2.setBounds(70,200,50,50);
  36. b2.addActionListener(new Actlis());
  37. b3.setBounds(120,200,50,50);
  38. b3.addActionListener(new Actlis());
  39. b13.setBounds(170,200,50,50);
  40. b13.addActionListener(new Actlis());
  41. b4.setBounds(20,150,50,50);
  42. b4.addActionListener(new Actlis());
  43. b5.setBounds(70,150,50,50);
  44. b5.addActionListener(new Actlis());
  45. b6.setBounds(120,150,50,50);
  46. b6.addActionListener(new Actlis());
  47. b12.setBounds(170,150,50,50);
  48. b12.addActionListener(new Actlis());
  49. b7.setBounds(20,100,50,50);
  50. b7.addActionListener(new Actlis());
  51. b8.setBounds(70,100,50,50);
  52. b8.addActionListener(new Actlis());
  53. b9.setBounds(120,100,50,50);
  54. b9.addActionListener(new Actlis());
  55. b14.setBounds(170,100,50,50);
  56. b14.addActionListener(new Actlis());
  57. b0.setBounds(20,250,50,50);
  58. b0.addActionListener(new Actlis());
  59. b10.setBounds(70,250,50,50);
  60. b10.addActionListener(new Actlis());
  61. b11.setBounds(120,250,50,50);
  62. b11.addActionListener(new Actlis());
  63. b15.setBounds(170,250,50,50);
  64. b15.addActionListener(new Actlis());
  65.  
  66. txt.setBounds(20, 40, 200, 30);
  67. txt.setHorizontalAlignment(JTextField.RIGHT);
  68. label.setForeground(Color.BLUE);
  69. label.setFont(new Font("defalut",Font.ITALIC+Font.BOLD,40));
  70.  
  71. frm.setLocation(5, 5);
  72. frm.add(label);
  73. frm.setVisible(true);
  74. frm.setTitle("計算機");
  75. frm.setSize(260,380);
  76. frm.addWindowListener(new Actlis());
  77.  
  78. container.add(txt);
  79. container.add(label);
  80. container.add(b0);
  81. container.add(b1);
  82. container.add(b2);
  83. container.add(b3);
  84. container.add(b4);
  85. container.add(b5);
  86. container.add(b6);
  87. container.add(b7);
  88. container.add(b8);
  89. container.add(b9);
  90. container.add(b10);
  91. container.add(b11);
  92. container.add(b12);
  93. container.add(b13);
  94. container.add(b14);
  95. container.add(b15);
  96.  
  97. }
  98. static class Actlis extends WindowAdapter implements ActionListener{
  99. @Override
  100. public void actionPerformed(ActionEvent e) {
  101.  
  102. if (e.getSource()==b1) //b0~9是顯示數字
  103. {
  104. txt.setText(txt.getText()+"1");
  105. }
  106. if (e.getSource()==b2)
  107. {
  108. txt.setText(txt.getText()+"2");
  109.  
  110. }
  111. if (e.getSource()==b3)
  112. {
  113. txt.setText(txt.getText()+"3");
  114.  
  115. }
  116. if (e.getSource()==b4)
  117. {
  118. txt.setText(txt.getText()+"4");
  119.  
  120. }
  121. if (e.getSource()==b5)
  122. {
  123. txt.setText(txt.getText()+"5");
  124.  
  125. }
  126. if (e.getSource()==b6)
  127. {
  128. txt.setText(txt.getText()+"6");
  129.  
  130. }
  131. if (e.getSource()==b7)
  132. {
  133. txt.setText(txt.getText()+"7");
  134.  
  135. }
  136. if (e.getSource()==b8)
  137. {
  138. txt.setText(txt.getText()+"8");
  139.  
  140. }
  141. if (e.getSource()==b9)
  142. {
  143. txt.setText(txt.getText()+"9");
  144.  
  145. }
  146. if (e.getSource()==b0)
  147. {
  148. txt.setText(txt.getText()+"0");
  149.  
  150. }
  151. if (e.getSource()==b10) //+ 以下由加號做解釋依此類推
  152. {
  153. if(tmp2=='+') //上一個符號是+就做加法
  154. {
  155. tmp+=Integer.parseInt(txt.getText());
  156. txt.setText(null);
  157. }
  158. else if(tmp2=='-')//上一個符號是-就做減法
  159. {
  160. tmp-=Integer.parseInt(txt.getText());
  161. txt.setText(null);
  162. }
  163. else if(tmp2=='*')//上一個符號是*就做乘法
  164. {
  165. tmp*=Integer.parseInt(txt.getText());
  166. txt.setText(null);
  167. }
  168. else if(tmp2=='/')//上一個符號是/就做除法
  169. {
  170. tmp/=Integer.parseInt(txt.getText());
  171. txt.setText(null);
  172. }
  173. else
  174. {
  175. tmp=Integer.parseInt(txt.getText());
  176. txt.setText(null);
  177.  
  178. }
  179. tmp2='+';// 存入tmp2做後續判斷
  180. }
  181. if (e.getSource()==b11)//-
  182. {
  183. if(tmp2=='+')
  184. {
  185. tmp+=Integer.parseInt(txt.getText());
  186. txt.setText(null);
  187. }
  188. else if(tmp2=='-')
  189. {
  190. tmp-=Integer.parseInt(txt.getText());
  191. txt.setText(null);
  192. }
  193. else if(tmp2=='*')
  194. {
  195. tmp*=Integer.parseInt(txt.getText());
  196. txt.setText(null);
  197. }
  198. else if(tmp2=='/')
  199. {
  200. tmp/=Integer.parseInt(txt.getText());
  201. txt.setText(null);
  202. }
  203. else
  204. {
  205. tmp=Integer.parseInt(txt.getText());
  206. txt.setText(null);
  207.  
  208. }
  209. tmp2='-';// 存入tmp2做後續判斷
  210. }
  211. if (e.getSource()==b12)//*
  212. {
  213. if(tmp2=='+')
  214. {
  215. tmp+=Integer.parseInt(txt.getText());
  216. txt.setText(null);
  217. }
  218. else if(tmp2=='-')
  219. {
  220. tmp-=Integer.parseInt(txt.getText());
  221. txt.setText(null);
  222. }
  223. else if(tmp2=='*')
  224. {
  225. tmp*=Integer.parseInt(txt.getText());
  226. txt.setText(null);
  227. }
  228. else if(tmp2=='/')
  229. {
  230. tmp/=Integer.parseInt(txt.getText());
  231. txt.setText(null);
  232. }
  233. else
  234. {
  235. tmp=Integer.parseInt(txt.getText());
  236. txt.setText(null);
  237.  
  238. }
  239. tmp2='*';// 存入tmp2做後續判斷
  240. }
  241. if (e.getSource()==b13)// /
  242. {
  243. if(tmp2=='+')
  244. {
  245. tmp+=Integer.parseInt(txt.getText());
  246. txt.setText(null);
  247. }
  248. else if(tmp2=='-')
  249. {
  250. tmp-=Integer.parseInt(txt.getText());
  251. txt.setText(null);
  252. }
  253. else if(tmp2=='*')
  254. {
  255. tmp*=Integer.parseInt(txt.getText());
  256. txt.setText(null);
  257. }
  258. else if(tmp2=='/')
  259. {
  260. tmp/=Integer.parseInt(txt.getText());
  261. txt.setText(null);
  262. }
  263. else
  264. {
  265. tmp=Integer.parseInt(txt.getText());
  266. txt.setText(null);
  267.  
  268. }
  269. tmp2='/';
  270. }
  271. if (e.getSource()==b14)// C 歸零
  272. {
  273. txt.setText(null);
  274. tmp=0;
  275. tmp2='c';
  276. }
  277. if (e.getSource()==b15)// =
  278. {
  279. if(tmp2=='+')
  280. {
  281. tmp+=Integer.parseInt(txt.getText());
  282. txt.setText(Integer.toString(tmp));
  283. }
  284. else if(tmp2=='-')
  285. {
  286. tmp-=Integer.parseInt(txt.getText());
  287. txt.setText(Integer.toString(tmp));
  288. }
  289. else if(tmp2=='*')
  290. {
  291. tmp*=Integer.parseInt(txt.getText());
  292. txt.setText(Integer.toString(tmp));
  293. }
  294. else if(tmp2=='/')
  295. {
  296. tmp/=Integer.parseInt(txt.getText());
  297. txt.setText(Integer.toString(tmp));
  298. }
  299. else
  300. {
  301. txt.setText(txt.getText());
  302. }
  303. tmp2='=';// 存入tmp2做後續判斷
  304. }
  305. }
  306.  
  307. @Override
  308. public void windowClosing(WindowEvent e) {
  309. System.exit(0);
  310.  
  311. }
  312. }
  313. @Override
  314. public void actionPerformed(ActionEvent arg0) {
  315. // TODO Auto-generated method stub
  316.  
  317. }
  318.  
  319.  
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement