login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > JS > Rekenmachines > Rekenmachine

Rekenmachine

Auteur: Duka.dk - 05 februari 2005 - 18:55 - Gekeurd door: Maarten - Hits: 5239 - Aantal punten: 2.70 (5 stemmen)




Gewoon deze class invoegen waar je hem wilt.
  1. <applet code="AdderApplet.class" codebase = "" width="32" height="32">
  2. </applet>

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

Download dit script!
 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Geen reacties (0)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.063s