Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.60 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6.  
  7. /**
  8. *
  9. * @author SHS
  10. */
  11. public class Calculator extends javax.swing.JFrame {
  12.  
  13. double firstnum;
  14. double secondnum;
  15. double result;
  16. String operations;
  17. String answer;
  18. /**
  19. * Creates new form Calculator
  20. */
  21. public Calculator() {
  22. initComponents();
  23. }
  24.  
  25. /**
  26. * This method is called from within the constructor to initialize the form.
  27. * WARNING: Do NOT modify this code. The content of this method is always
  28. * regenerated by the Form Editor.
  29. */
  30. @SuppressWarnings("unchecked")
  31. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  32. private void initComponents() {
  33.  
  34. jTextField1 = new javax.swing.JTextField();
  35. jButton1 = new javax.swing.JButton();
  36. jButton2 = new javax.swing.JButton();
  37. jButton3 = new javax.swing.JButton();
  38. jButton4 = new javax.swing.JButton();
  39. jButton5 = new javax.swing.JButton();
  40. jButton6 = new javax.swing.JButton();
  41. jButton7 = new javax.swing.JButton();
  42. jButton8 = new javax.swing.JButton();
  43. jButton9 = new javax.swing.JButton();
  44. jButton10 = new javax.swing.JButton();
  45. jButton13 = new javax.swing.JButton();
  46. jButton14 = new javax.swing.JButton();
  47. jButton15 = new javax.swing.JButton();
  48. jButton18 = new javax.swing.JButton();
  49. jButton19 = new javax.swing.JButton();
  50. jButton20 = new javax.swing.JButton();
  51. jButton11 = new javax.swing.JButton();
  52.  
  53. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  54.  
  55. jButton1.setText("7");
  56. jButton1.addActionListener(new java.awt.event.ActionListener() {
  57. public void actionPerformed(java.awt.event.ActionEvent evt) {
  58. jButton1ActionPerformed(evt);
  59. }
  60. });
  61.  
  62. jButton2.setText("8");
  63. jButton2.addActionListener(new java.awt.event.ActionListener() {
  64. public void actionPerformed(java.awt.event.ActionEvent evt) {
  65. jButton2ActionPerformed(evt);
  66. }
  67. });
  68.  
  69. jButton3.setText("9");
  70. jButton3.addActionListener(new java.awt.event.ActionListener() {
  71. public void actionPerformed(java.awt.event.ActionEvent evt) {
  72. jButton3ActionPerformed(evt);
  73. }
  74. });
  75.  
  76. jButton4.setText("4");
  77. jButton4.addActionListener(new java.awt.event.ActionListener() {
  78. public void actionPerformed(java.awt.event.ActionEvent evt) {
  79. jButton4ActionPerformed(evt);
  80. }
  81. });
  82.  
  83. jButton5.setText("5");
  84. jButton5.addActionListener(new java.awt.event.ActionListener() {
  85. public void actionPerformed(java.awt.event.ActionEvent evt) {
  86. jButton5ActionPerformed(evt);
  87. }
  88. });
  89.  
  90. jButton6.setText("6");
  91. jButton6.addActionListener(new java.awt.event.ActionListener() {
  92. public void actionPerformed(java.awt.event.ActionEvent evt) {
  93. jButton6ActionPerformed(evt);
  94. }
  95. });
  96.  
  97. jButton7.setText("1");
  98. jButton7.addActionListener(new java.awt.event.ActionListener() {
  99. public void actionPerformed(java.awt.event.ActionEvent evt) {
  100. jButton7ActionPerformed(evt);
  101. }
  102. });
  103.  
  104. jButton8.setText("2");
  105. jButton8.addActionListener(new java.awt.event.ActionListener() {
  106. public void actionPerformed(java.awt.event.ActionEvent evt) {
  107. jButton8ActionPerformed(evt);
  108. }
  109. });
  110.  
  111. jButton9.setText("3");
  112. jButton9.addActionListener(new java.awt.event.ActionListener() {
  113. public void actionPerformed(java.awt.event.ActionEvent evt) {
  114. jButton9ActionPerformed(evt);
  115. }
  116. });
  117.  
  118. jButton10.setText("=");
  119. jButton10.addActionListener(new java.awt.event.ActionListener() {
  120. public void actionPerformed(java.awt.event.ActionEvent evt) {
  121. jButton10ActionPerformed(evt);
  122. }
  123. });
  124.  
  125. jButton13.setText("+");
  126. jButton13.addActionListener(new java.awt.event.ActionListener() {
  127. public void actionPerformed(java.awt.event.ActionEvent evt) {
  128. jButton13ActionPerformed(evt);
  129. }
  130. });
  131.  
  132. jButton14.setText("-");
  133. jButton14.addActionListener(new java.awt.event.ActionListener() {
  134. public void actionPerformed(java.awt.event.ActionEvent evt) {
  135. jButton14ActionPerformed(evt);
  136. }
  137. });
  138.  
  139. jButton15.setText("*");
  140. jButton15.addActionListener(new java.awt.event.ActionListener() {
  141. public void actionPerformed(java.awt.event.ActionEvent evt) {
  142. jButton15ActionPerformed(evt);
  143. }
  144. });
  145.  
  146. jButton18.setText("/");
  147. jButton18.addActionListener(new java.awt.event.ActionListener() {
  148. public void actionPerformed(java.awt.event.ActionEvent evt) {
  149. jButton18ActionPerformed(evt);
  150. }
  151. });
  152.  
  153. jButton19.setText("0");
  154. jButton19.addActionListener(new java.awt.event.ActionListener() {
  155. public void actionPerformed(java.awt.event.ActionEvent evt) {
  156. jButton19ActionPerformed(evt);
  157. }
  158. });
  159.  
  160. jButton20.setText(".");
  161. jButton20.addActionListener(new java.awt.event.ActionListener() {
  162. public void actionPerformed(java.awt.event.ActionEvent evt) {
  163. jButton20ActionPerformed(evt);
  164. }
  165. });
  166.  
  167. jButton11.setText("C");
  168. jButton11.addActionListener(new java.awt.event.ActionListener() {
  169. public void actionPerformed(java.awt.event.ActionEvent evt) {
  170. jButton11ActionPerformed(evt);
  171. }
  172. });
  173.  
  174. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  175. getContentPane().setLayout(layout);
  176. layout.setHorizontalGroup(
  177. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  178. .addComponent(jTextField1)
  179. .addGroup(layout.createSequentialGroup()
  180. .addContainerGap()
  181. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  182. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  183. .addComponent(jButton19, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  184. .addGroup(layout.createSequentialGroup()
  185. .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
  186. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  187. .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
  188. .addGroup(layout.createSequentialGroup()
  189. .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
  190. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  191. .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)))
  192. .addGroup(layout.createSequentialGroup()
  193. .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
  194. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  195. .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)))
  196. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  197. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  198. .addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
  199. .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  200. .addComponent(jButton9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  201. .addComponent(jButton20, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  202. .addGap(22, 22, 22)
  203. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
  204. .addComponent(jButton13, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)
  205. .addComponent(jButton14, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  206. .addComponent(jButton15, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  207. .addComponent(jButton18, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  208. .addComponent(jButton10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  209. .addComponent(jButton11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  210. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  211. );
  212. layout.setVerticalGroup(
  213. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  214. .addGroup(layout.createSequentialGroup()
  215. .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
  216. .addGap(18, 18, 18)
  217. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  218. .addGroup(layout.createSequentialGroup()
  219. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  220. .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
  221. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  222. .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 62, Short.MAX_VALUE)
  223. .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
  224. .addGap(16, 16, 16)
  225. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
  226. .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
  227. .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  228. .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  229. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  230. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  231. .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
  232. .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
  233. .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))
  234. .addGap(0, 0, Short.MAX_VALUE))
  235. .addGroup(layout.createSequentialGroup()
  236. .addComponent(jButton11, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
  237. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  238. .addComponent(jButton18, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
  239. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  240. .addComponent(jButton15, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
  241. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  242. .addComponent(jButton14, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
  243. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  244. .addComponent(jButton13, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  245. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  246. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  247. .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
  248. .addComponent(jButton19, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
  249. .addComponent(jButton20, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))))
  250. .addContainerGap())
  251. );
  252.  
  253. pack();
  254. }// </editor-fold>
  255.  
  256. private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
  257. // TODO add your handling code here:
  258. String Enternumber=jTextField1.getText()+jButton7.getText();
  259. jTextField1.setText(Enternumber);
  260. }
  261.  
  262. private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
  263. // TODO add your handling code here:
  264. String Enternumber=jTextField1.getText()+jButton8.getText();
  265. jTextField1.setText(Enternumber);
  266. }
  267.  
  268. private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
  269. // TODO add your handling code here:
  270. String Enternumber=jTextField1.getText()+jButton9.getText();
  271. jTextField1.setText(Enternumber);
  272. }
  273.  
  274. private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
  275. // TODO add your handling code here:
  276. String Enternumber=jTextField1.getText()+jButton4.getText();
  277. jTextField1.setText(Enternumber);
  278. }
  279.  
  280. private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
  281. // TODO add your handling code here:
  282. String Enternumber=jTextField1.getText()+jButton5.getText();
  283. jTextField1.setText(Enternumber);
  284. }
  285.  
  286. private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
  287. // TODO add your handling code here:
  288. String Enternumber=jTextField1.getText()+jButton6.getText();
  289. jTextField1.setText(Enternumber);
  290. }
  291.  
  292. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  293. // TODO add your handling code here:
  294. String Enternumber=jTextField1.getText()+jButton1.getText();
  295. jTextField1.setText(Enternumber);
  296. }
  297.  
  298. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
  299. // TODO add your handling code here:
  300. String Enternumber=jTextField1.getText()+jButton2.getText();
  301. jTextField1.setText(Enternumber);
  302. }
  303.  
  304. private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
  305. // TODO add your handling code here:
  306. String Enternumber=jTextField1.getText()+jButton3.getText();
  307. jTextField1.setText(Enternumber);
  308. }
  309.  
  310. private void jButton19ActionPerformed(java.awt.event.ActionEvent evt) {
  311. // TODO add your handling code here:
  312. String Enternumber=jTextField1.getText()+jButton19.getText();
  313. jTextField1.setText(Enternumber);
  314. }
  315.  
  316. private void jButton18ActionPerformed(java.awt.event.ActionEvent evt) {
  317. // TODO add your handling code here:
  318. firstnum = Double.parseDouble(jTextField1.getText());
  319. jTextField1.setText("");
  320. operations = "/";
  321. }
  322.  
  323. private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {
  324. // TODO add your handling code here:
  325. firstnum = Double.parseDouble(jTextField1.getText());
  326. jTextField1.setText("");
  327. operations = "*";
  328. }
  329.  
  330. private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {
  331. // TODO add your handling code here:
  332. firstnum = Double.parseDouble(jTextField1.getText());
  333. jTextField1.setText("");
  334. operations = "-";
  335. }
  336.  
  337. private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {
  338. // TODO add your handling code here:
  339. firstnum = Double.parseDouble(jTextField1.getText());
  340. jTextField1.setText("");
  341. operations = "+";
  342. }
  343.  
  344. private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
  345. // TODO add your handling code here:
  346.  
  347. secondnum=Double.parseDouble(jTextField1.getText());
  348. if (operations.equals("+"))
  349. result=firstnum+secondnum;
  350. if (operations.equals("-"))
  351. result=firstnum-secondnum;
  352. if (operations.equals("*"))
  353. result=firstnum*secondnum;
  354. if (operations.equals("/"))
  355. result=firstnum/secondnum;
  356.  
  357.  
  358.  
  359. {}
  360. jTextField1.setText(result+ "");
  361.  
  362.  
  363. }
  364.  
  365. private void jButton20ActionPerformed(java.awt.event.ActionEvent evt) {
  366. // TODO add your handling code here:
  367. String Enternumber=jTextField1.getText()+jButton8.getText();
  368. jTextField1.setText("");
  369.  
  370. }
  371.  
  372. private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {
  373. // TODO add your handling code here:
  374. String Enternumber=jTextField1.getText()+jButton11.getText();
  375. jTextField1.setText("");
  376.  
  377. }
  378.  
  379. /**
  380. * @param args the command line arguments
  381. */
  382. public static void main(String args[]) {
  383. /* Set the Nimbus look and feel */
  384. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  385. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  386. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  387. */
  388. try {
  389. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  390. if ("Nimbus".equals(info.getName())) {
  391. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  392. break;
  393. }
  394. }
  395. } catch (ClassNotFoundException ex) {
  396. java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  397. } catch (InstantiationException ex) {
  398. java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  399. } catch (IllegalAccessException ex) {
  400. java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  401. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  402. java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  403. }
  404. //</editor-fold>
  405.  
  406. /* Create and display the form */
  407. java.awt.EventQueue.invokeLater(new Runnable() {
  408. public void run() {
  409. new Calculator().setVisible(true);
  410. }
  411. });
  412. }
  413.  
  414. // Variables declaration - do not modify
  415. private javax.swing.JButton jButton1;
  416. private javax.swing.JButton jButton10;
  417. private javax.swing.JButton jButton11;
  418. private javax.swing.JButton jButton13;
  419. private javax.swing.JButton jButton14;
  420. private javax.swing.JButton jButton15;
  421. private javax.swing.JButton jButton18;
  422. private javax.swing.JButton jButton19;
  423. private javax.swing.JButton jButton2;
  424. private javax.swing.JButton jButton20;
  425. private javax.swing.JButton jButton3;
  426. private javax.swing.JButton jButton4;
  427. private javax.swing.JButton jButton5;
  428. private javax.swing.JButton jButton6;
  429. private javax.swing.JButton jButton7;
  430. private javax.swing.JButton jButton8;
  431. private javax.swing.JButton jButton9;
  432. private javax.swing.JTextField jTextField1;
  433. // End of variables declaration
  434. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement