login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > JS > Rekenmachines > Rekenmachine

Rekenmachine

Auteur: Geen idee - 05 februari 2005 - 11:36 - Gekeurd door: Joel - Hits: 5365 - Aantal punten: 3.00 (1 stem)





Een rekenmachine.

Code:
  1. import java.applet.Applet;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. public class AdderApplet extends Applet
  7. implements ActionListener
  8. {
  9.  
  10. public void init()
  11. {
  12. setLayout(new BorderLayout());
  13. Panel textPanel = new Panel();
  14. textPanel.setBackground(Color.gray);
  15. textPanel.setLayout(new FlowLayout());
  16. inputOutputField = new TextField("0", 20);
  17. inputOutputField.setBackground(Color.purple);
  18. textPanel.add(inputOutputField);
  19. add(textPanel, "North");
  20. Panel platformPanel = new Panel();
  21. platformPanel.setLayout(new GridLayout(4, 5));
  22. Panel button1Panel = new Panel();
  23. button1Panel.setBackground(Color.black);
  24. Button sevenButton = new Button(" 7 ");
  25. sevenButton.addActionListener(this);
  26. button1Panel.add(sevenButton);
  27. Button eightButton = new Button(" 8 ");
  28. eightButton.addActionListener(this);
  29. button1Panel.add(eightButton);
  30. Button nineButton = new Button(" 9 ");
  31. nineButton.addActionListener(this);
  32. button1Panel.add(nineButton);
  33. Button divideButton = new Button(" / ");
  34. divideButton.addActionListener(this);
  35. button1Panel.add(divideButton);
  36. platformPanel.add(button1Panel);
  37. Panel button2Panel = new Panel();
  38. button2Panel.setBackground(Color.black);
  39. Button fourButton = new Button(" 4 ");
  40. fourButton.addActionListener(this);
  41. button2Panel.add(fourButton);
  42. Button fiveButton = new Button(" 5 ");
  43. fiveButton.addActionListener(this);
  44. button2Panel.add(fiveButton);
  45. Button sixButton = new Button(" 6 ");
  46. sixButton.addActionListener(this);
  47. button2Panel.add(sixButton);
  48. Button multiplyButton = new Button(" * ");
  49. multiplyButton.addActionListener(this);
  50. button2Panel.add(multiplyButton);
  51. platformPanel.add(button2Panel);
  52. Panel button3Panel = new Panel();
  53. button3Panel.setBackground(Color.black);
  54. Button oneButton = new Button(" 1 ");
  55. oneButton.addActionListener(this);
  56. button3Panel.add(oneButton);
  57. Button twoButton = new Button(" 2 ");
  58. twoButton.addActionListener(this);
  59. button3Panel.add(twoButton);
  60. Button threeButton = new Button(" 3 ");
  61. threeButton.addActionListener(this);
  62. button3Panel.add(threeButton);
  63. Button minusButton = new Button(" - ");
  64. minusButton.addActionListener(this);
  65. button3Panel.add(minusButton);
  66. platformPanel.add(button3Panel);
  67. Panel button4Panel = new Panel();
  68. button4Panel.setBackground(Color.black);
  69. Button zeroButton = new Button(" 0 ");
  70. zeroButton.addActionListener(this);
  71. button4Panel.add(zeroButton);
  72. Button dotButton = new Button(" . ");
  73. dotButton.addActionListener(this);
  74. button4Panel.add(dotButton);
  75. Button addButton = new Button(" + ");
  76. addButton.addActionListener(this);
  77. button4Panel.add(addButton);
  78. Button equalsButton = new Button(" = ");
  79. equalsButton.addActionListener(this);
  80. button4Panel.add(equalsButton);
  81. platformPanel.add(button4Panel);
  82. add(platformPanel, "Center");
  83. Panel clearPanel = new Panel();
  84. clearPanel.setBackground(Color.blue);
  85. Button clearButton = new Button(" CLEAR ");
  86. clearButton.addActionListener(this);
  87. clearPanel.add(clearButton);
  88. add(clearPanel, "South");
  89. }
  90.  
  91. public void actionPerformed(ActionEvent e)
  92. {
  93. if(e.getActionCommand().equals(" 7 "))
  94. {
  95. numString = numString + "7";
  96. inputOutputField.setText(numString);
  97. flag = "y";
  98. } else
  99. if(e.getActionCommand().equals(" 8 "))
  100. {
  101. numString = numString + "8";
  102. inputOutputField.setText(numString);
  103. flag = "y";
  104. } else
  105. if(e.getActionCommand().equals(" 9 "))
  106. {
  107. numString = numString + "9";
  108. inputOutputField.setText(numString);
  109. flag = "y";
  110. } else
  111. if(e.getActionCommand().equals(" 4 "))
  112. {
  113. numString = numString + "4";
  114. inputOutputField.setText(numString);
  115. flag = "y";
  116. } else
  117. if(e.getActionCommand().equals(" 5 "))
  118. {
  119. numString = numString + "5";
  120. inputOutputField.setText(numString);
  121. flag = "y";
  122. } else
  123. if(e.getActionCommand().equals(" 6 "))
  124. {
  125. numString = numString + "6";
  126. inputOutputField.setText(numString);
  127. flag = "y";
  128. } else
  129. if(e.getActionCommand().equals(" 1 "))
  130. {
  131. numString = numString + "1";
  132. inputOutputField.setText(numString);
  133. flag = "y";
  134. } else
  135. if(e.getActionCommand().equals(" 2 "))
  136. {
  137. numString = numString + "2";
  138. inputOutputField.setText(numString);
  139. flag = "y";
  140. } else
  141. if(e.getActionCommand().equals(" 3 "))
  142. {
  143. numString = numString + "3";
  144. inputOutputField.setText(numString);
  145. flag = "y";
  146. } else
  147. if(e.getActionCommand().equals(" - "))
  148. {
  149. equalBuffer = "";
  150. calcIt();
  151. buttonBuffer = "-";
  152. } else
  153. if(e.getActionCommand().equals(" / "))
  154. {
  155. equalBuffer = "";
  156. calcIt();
  157. buttonBuffer = "/";
  158. } else
  159. if(e.getActionCommand().equals(" * "))
  160. {
  161. equalBuffer = "";
  162. calcIt();
  163. buttonBuffer = "*";
  164. } else
  165. if(e.getActionCommand().equals(" 0 "))
  166. {
  167. numString = numString + "0";
  168. inputOutputField.setText(numString);
  169. } else
  170. if(e.getActionCommand().equals(" . "))
  171. {
  172. numString = numString + ".";
  173. inputOutputField.setText(numString);
  174. } else
  175. if(e.getActionCommand().equals(" + "))
  176. {
  177. equalBuffer = "";
  178. calcIt();
  179. buttonBuffer = "+";
  180. } else
  181. if(e.getActionCommand().equals(" = "))
  182. {
  183. calcIt();
  184. buttonBuffer = buttonBuffer;
  185. equalBuffer = "=";
  186. } else
  187. if(e.getActionCommand().equals(" CLEAR "))
  188. {
  189. numString = "";
  190. inputOutputField.setText("0");
  191. buttonBuffer = "";
  192. numBuffer = 0.0D;
  193. equalBuffer = "";
  194. equalString = "";
  195. } else
  196. {
  197. inputOutputField.setText("Error in adder code.");
  198. }
  199. repaint();
  200. }
  201.  
  202. private void doTheMath()
  203. {
  204. if(buttonBuffer == "+")
  205. {
  206. if(numString == "")
  207. numString = "0";
  208. numBuffer = numBuffer + stringToDouble(numString);
  209. inputOutputField.setText(Double.toString(numBuffer));
  210. } else
  211. if(buttonBuffer == "-")
  212. {
  213. if(numString == "")
  214. numString = "0";
  215. numBuffer = numBuffer - stringToDouble(numString);
  216. inputOutputField.setText(Double.toString(numBuffer));
  217. } else
  218. if(buttonBuffer == "*")
  219. {
  220. if(numString == "")
  221. numString = "1";
  222. numBuffer = numBuffer * stringToDouble(numString);
  223. inputOutputField.setText(Double.toString(numBuffer));
  224. } else
  225. if(buttonBuffer == "/")
  226. {
  227. if(numString == "")
  228. numString = "1";
  229. numBuffer = numBuffer / stringToDouble(numString);
  230. inputOutputField.setText(Double.toString(numBuffer));
  231. }
  232. }
  233.  
  234. private void doEqualMath()
  235. {
  236. if(buttonBuffer == "+")
  237. {
  238. numBuffer = numBuffer + stringToDouble(equalString);
  239. inputOutputField.setText(Double.toString(numBuffer));
  240. } else
  241. if(buttonBuffer == "-")
  242. {
  243. numBuffer = numBuffer - stringToDouble(equalString);
  244. inputOutputField.setText(Double.toString(numBuffer));
  245. } else
  246. if(buttonBuffer == "*")
  247. {
  248. numBuffer = numBuffer * stringToDouble(equalString);
  249. inputOutputField.setText(Double.toString(numBuffer));
  250. } else
  251. if(buttonBuffer == "/")
  252. {
  253. numBuffer = numBuffer / stringToDouble(equalString);
  254. inputOutputField.setText(Double.toString(numBuffer));
  255. }
  256. }
  257.  
  258. private void calcIt()
  259. {
  260. numString = inputOutputField.getText();
  261. if(flag == "y")
  262. {
  263. equalString = inputOutputField.getText();
  264. doTheMath();
  265. numBuffer = stringToDouble(inputOutputField.getText());
  266. numString = "";
  267. }
  268. if(flag == "n")
  269. {
  270. if(equalBuffer == "=")
  271. doEqualMath();
  272. numBuffer = stringToDouble(inputOutputField.getText());
  273. if(equalBuffer != "=")
  274. buttonBuffer = "";
  275. numString = "";
  276. }
  277. flag = "n";
  278. }
  279.  
  280. private static double stringToDouble(String stringObject)
  281. {
  282. return Double.valueOf(stringObject.trim()).doubleValue();
  283. }
  284.  
  285. public AdderApplet()
  286. {
  287. buttonBuffer = "";
  288. equalBuffer = "";
  289. numBuffer = 0.0D;
  290. numString = "";
  291. equalString = "";
  292. flag = "n";
  293. }
  294.  
  295. private TextField inputOutputField;
  296. private String buttonBuffer;
  297. private String equalBuffer;
  298. private double numBuffer;
  299. private String numString;
  300. private String equalString;
  301. private String flag;
  302. }


  1. <APPLET CODE="AdderApplet.class" WIDTH="175" HEIGHT="175">
  2. </APPLET>
Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (1)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.049s