Advertisement
Farjana_akter

Untitled

Feb 20th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.96 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 lenovo
  10. */
  11. public class calculatordesign extends javax.swing.JFrame {
  12.  
  13. double firstnum;
  14. double secondnum;
  15. double result=0;
  16. String operate="";
  17. String answer="";
  18. String takein="";
  19. int offon=0;
  20. public calculatordesign() {
  21. initComponents();
  22. }
  23.  
  24. /**
  25. * This method is called from within the constructor to initialize the form.
  26. * WARNING: Do NOT modify this code. The content of this method is always
  27. * regenerated by the Form Editor.
  28. */
  29. @SuppressWarnings("unchecked")
  30. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  31. private void initComponents() {
  32.  
  33. jButton1 = new javax.swing.JButton();
  34. key1 = new javax.swing.JButton();
  35. key2 = new javax.swing.JButton();
  36. key3 = new javax.swing.JButton();
  37. key4 = new javax.swing.JButton();
  38. key5 = new javax.swing.JButton();
  39. key6 = new javax.swing.JButton();
  40. key7 = new javax.swing.JButton();
  41. key8 = new javax.swing.JButton();
  42. key9 = new javax.swing.JButton();
  43. key0 = new javax.swing.JButton();
  44. plussign = new javax.swing.JButton();
  45. minussign = new javax.swing.JButton();
  46. multisign = new javax.swing.JButton();
  47. divsign = new javax.swing.JButton();
  48. modsign = new javax.swing.JButton();
  49. txtdisplay = new javax.swing.JTextField();
  50. equalsign = new javax.swing.JButton();
  51. dotsign = new javax.swing.JButton();
  52. clearsign = new javax.swing.JButton();
  53. onbutton = new javax.swing.JButton();
  54. offbutton = new javax.swing.JButton();
  55.  
  56. jButton1.setText("jButton1");
  57.  
  58. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  59.  
  60. key1.setText("1");
  61. key1.addActionListener(new java.awt.event.ActionListener() {
  62. public void actionPerformed(java.awt.event.ActionEvent evt) {
  63. key1ActionPerformed(evt);
  64. }
  65. });
  66.  
  67. key2.setText("2");
  68. key2.addActionListener(new java.awt.event.ActionListener() {
  69. public void actionPerformed(java.awt.event.ActionEvent evt) {
  70. key2ActionPerformed(evt);
  71. }
  72. });
  73.  
  74. key3.setText("3");
  75. key3.addActionListener(new java.awt.event.ActionListener() {
  76. public void actionPerformed(java.awt.event.ActionEvent evt) {
  77. key3ActionPerformed(evt);
  78. }
  79. });
  80.  
  81. key4.setText("4");
  82. key4.addActionListener(new java.awt.event.ActionListener() {
  83. public void actionPerformed(java.awt.event.ActionEvent evt) {
  84. key4ActionPerformed(evt);
  85. }
  86. });
  87.  
  88. key5.setText("5");
  89. key5.addActionListener(new java.awt.event.ActionListener() {
  90. public void actionPerformed(java.awt.event.ActionEvent evt) {
  91. key5ActionPerformed(evt);
  92. }
  93. });
  94.  
  95. key6.setText("6");
  96. key6.addActionListener(new java.awt.event.ActionListener() {
  97. public void actionPerformed(java.awt.event.ActionEvent evt) {
  98. key6ActionPerformed(evt);
  99. }
  100. });
  101.  
  102. key7.setText("7");
  103. key7.addActionListener(new java.awt.event.ActionListener() {
  104. public void actionPerformed(java.awt.event.ActionEvent evt) {
  105. key7ActionPerformed(evt);
  106. }
  107. });
  108.  
  109. key8.setText("8");
  110. key8.addActionListener(new java.awt.event.ActionListener() {
  111. public void actionPerformed(java.awt.event.ActionEvent evt) {
  112. key8ActionPerformed(evt);
  113. }
  114. });
  115.  
  116. key9.setText("9");
  117. key9.addActionListener(new java.awt.event.ActionListener() {
  118. public void actionPerformed(java.awt.event.ActionEvent evt) {
  119. key9ActionPerformed(evt);
  120. }
  121. });
  122.  
  123. key0.setText("0");
  124. key0.addActionListener(new java.awt.event.ActionListener() {
  125. public void actionPerformed(java.awt.event.ActionEvent evt) {
  126. key0ActionPerformed(evt);
  127. }
  128. });
  129.  
  130. plussign.setText("+");
  131. plussign.addActionListener(new java.awt.event.ActionListener() {
  132. public void actionPerformed(java.awt.event.ActionEvent evt) {
  133. plussignActionPerformed(evt);
  134. }
  135. });
  136.  
  137. minussign.setText("-");
  138. minussign.addActionListener(new java.awt.event.ActionListener() {
  139. public void actionPerformed(java.awt.event.ActionEvent evt) {
  140. minussignActionPerformed(evt);
  141. }
  142. });
  143.  
  144. multisign.setText("*");
  145. multisign.addActionListener(new java.awt.event.ActionListener() {
  146. public void actionPerformed(java.awt.event.ActionEvent evt) {
  147. multisignActionPerformed(evt);
  148. }
  149. });
  150.  
  151. divsign.setText("/");
  152. divsign.addActionListener(new java.awt.event.ActionListener() {
  153. public void actionPerformed(java.awt.event.ActionEvent evt) {
  154. divsignActionPerformed(evt);
  155. }
  156. });
  157.  
  158. modsign.setText("%");
  159. modsign.addActionListener(new java.awt.event.ActionListener() {
  160. public void actionPerformed(java.awt.event.ActionEvent evt) {
  161. modsignActionPerformed(evt);
  162. }
  163. });
  164.  
  165. txtdisplay.addActionListener(new java.awt.event.ActionListener() {
  166. public void actionPerformed(java.awt.event.ActionEvent evt) {
  167. txtdisplayActionPerformed(evt);
  168. }
  169. });
  170.  
  171. equalsign.setText("=");
  172. equalsign.addActionListener(new java.awt.event.ActionListener() {
  173. public void actionPerformed(java.awt.event.ActionEvent evt) {
  174. equalsignActionPerformed(evt);
  175. }
  176. });
  177.  
  178. dotsign.setText(".");
  179. dotsign.addActionListener(new java.awt.event.ActionListener() {
  180. public void actionPerformed(java.awt.event.ActionEvent evt) {
  181. dotsignActionPerformed(evt);
  182. }
  183. });
  184.  
  185. clearsign.setText("C");
  186. clearsign.addActionListener(new java.awt.event.ActionListener() {
  187. public void actionPerformed(java.awt.event.ActionEvent evt) {
  188. clearsignActionPerformed(evt);
  189. }
  190. });
  191.  
  192. onbutton.setText("ON");
  193. onbutton.addActionListener(new java.awt.event.ActionListener() {
  194. public void actionPerformed(java.awt.event.ActionEvent evt) {
  195. onbuttonActionPerformed(evt);
  196. }
  197. });
  198.  
  199. offbutton.setText("OFF");
  200. offbutton.addActionListener(new java.awt.event.ActionListener() {
  201. public void actionPerformed(java.awt.event.ActionEvent evt) {
  202. offbuttonActionPerformed(evt);
  203. }
  204. });
  205.  
  206. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  207. getContentPane().setLayout(layout);
  208. layout.setHorizontalGroup(
  209. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  210. .addGroup(layout.createSequentialGroup()
  211. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  212. .addGroup(layout.createSequentialGroup()
  213. .addGap(24, 24, 24)
  214. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  215. .addComponent(key7)
  216. .addComponent(key1)
  217. .addComponent(key4)
  218. .addComponent(key0))
  219. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  220. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  221. .addGroup(layout.createSequentialGroup()
  222. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  223. .addComponent(key8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  224. .addComponent(dotsign, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  225. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  226. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  227. .addGroup(layout.createSequentialGroup()
  228. .addComponent(key9)
  229. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  230. .addComponent(multisign)
  231. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  232. .addComponent(offbutton, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
  233. .addGroup(layout.createSequentialGroup()
  234. .addComponent(equalsign)
  235. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  236. .addComponent(divsign)
  237. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  238. .addComponent(modsign))))
  239. .addGroup(layout.createSequentialGroup()
  240. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  241. .addGroup(layout.createSequentialGroup()
  242. .addComponent(key5)
  243. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  244. .addComponent(key6)
  245. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  246. .addComponent(minussign))
  247. .addGroup(layout.createSequentialGroup()
  248. .addComponent(key2)
  249. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  250. .addComponent(key3)
  251. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  252. .addComponent(plussign)))
  253. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  254. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  255. .addComponent(onbutton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  256. .addComponent(clearsign, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
  257. .addGroup(layout.createSequentialGroup()
  258. .addContainerGap()
  259. .addComponent(txtdisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)))
  260. .addContainerGap(13, Short.MAX_VALUE))
  261. );
  262. layout.setVerticalGroup(
  263. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  264. .addGroup(layout.createSequentialGroup()
  265. .addGap(21, 21, 21)
  266. .addComponent(txtdisplay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  267. .addGap(39, 39, 39)
  268. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  269. .addComponent(key1)
  270. .addComponent(key2)
  271. .addComponent(key3)
  272. .addComponent(plussign)
  273. .addComponent(clearsign))
  274. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  275. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  276. .addGroup(layout.createSequentialGroup()
  277. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  278. .addComponent(key6)
  279. .addComponent(minussign)
  280. .addComponent(onbutton))
  281. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  282. .addComponent(offbutton))
  283. .addGroup(layout.createSequentialGroup()
  284. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  285. .addComponent(key5)
  286. .addComponent(key4))
  287. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  288. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  289. .addComponent(key7)
  290. .addComponent(key8)
  291. .addComponent(key9)
  292. .addComponent(multisign))
  293. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  294. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  295. .addComponent(key0)
  296. .addComponent(equalsign)
  297. .addComponent(divsign)
  298. .addComponent(dotsign)
  299. .addComponent(modsign))))
  300. .addContainerGap(30, Short.MAX_VALUE))
  301. );
  302.  
  303. pack();
  304. }// </editor-fold>
  305.  
  306. private void txtdisplayActionPerformed(java.awt.event.ActionEvent evt) {
  307. // TODO add your handling code here:
  308. }
  309.  
  310. private void key8ActionPerformed(java.awt.event.ActionEvent evt) {
  311. if(offon==1){
  312. takein = txtdisplay.getText() + key8.getText();
  313. txtdisplay.setText(takein);}
  314. }
  315.  
  316. private void key1ActionPerformed(java.awt.event.ActionEvent evt) {
  317. if(offon==1){
  318. takein = txtdisplay.getText() + key1.getText();
  319. txtdisplay.setText(takein);}
  320. }
  321.  
  322. private void key2ActionPerformed(java.awt.event.ActionEvent evt) {
  323. if(offon==1){
  324. takein = txtdisplay.getText() + key2.getText();
  325. txtdisplay.setText(takein);}
  326. }
  327.  
  328. private void key3ActionPerformed(java.awt.event.ActionEvent evt) {
  329. if(offon==1){
  330. takein = txtdisplay.getText() + key3.getText();
  331. txtdisplay.setText(takein);}
  332. }
  333.  
  334. private void key4ActionPerformed(java.awt.event.ActionEvent evt) {
  335. if(offon==1){
  336. takein = txtdisplay.getText() + key4.getText();
  337. txtdisplay.setText(takein);}
  338. }
  339.  
  340. private void key5ActionPerformed(java.awt.event.ActionEvent evt) {
  341. if(offon==1){
  342. takein = txtdisplay.getText() + key5.getText();
  343. txtdisplay.setText(takein);}
  344. }
  345.  
  346. private void key6ActionPerformed(java.awt.event.ActionEvent evt) {
  347. if(offon==1){
  348. takein = txtdisplay.getText() + key6.getText();
  349. txtdisplay.setText(takein);}
  350. }
  351.  
  352. private void key7ActionPerformed(java.awt.event.ActionEvent evt) {
  353. if(offon==1){
  354. takein = txtdisplay.getText() + key7.getText();
  355. txtdisplay.setText(takein);}
  356. }
  357.  
  358. private void key9ActionPerformed(java.awt.event.ActionEvent evt) {
  359. if(offon==1){
  360. takein = txtdisplay.getText() + key9.getText();
  361. txtdisplay.setText(takein);}
  362. }
  363.  
  364. private void key0ActionPerformed(java.awt.event.ActionEvent evt) {
  365. if(offon==1){
  366. takein = txtdisplay.getText() + key0.getText();
  367. txtdisplay.setText(takein);}
  368. }
  369.  
  370. private void dotsignActionPerformed(java.awt.event.ActionEvent evt) {
  371.  
  372. if(offon==1){
  373. takein = txtdisplay.getText() + dotsign.getText();
  374. txtdisplay.setText(takein);}
  375. }
  376.  
  377. private void clearsignActionPerformed(java.awt.event.ActionEvent evt) {
  378. txtdisplay.setText("");
  379. firstnum = 0;
  380. secondnum = 0;
  381. }
  382.  
  383. private void plussignActionPerformed(java.awt.event.ActionEvent evt) {
  384. firstnum = Double.parseDouble(txtdisplay.getText());
  385. txtdisplay.setText("");
  386. operate = "+";
  387. }
  388.  
  389. private void minussignActionPerformed(java.awt.event.ActionEvent evt) {
  390. firstnum = Double.parseDouble(txtdisplay.getText());
  391. txtdisplay.setText("");
  392. operate = "-";
  393. }
  394.  
  395. private void multisignActionPerformed(java.awt.event.ActionEvent evt) {
  396. firstnum = Double.parseDouble(txtdisplay.getText());
  397. txtdisplay.setText("");
  398. operate = "*";
  399. }
  400.  
  401. private void divsignActionPerformed(java.awt.event.ActionEvent evt) {
  402. firstnum = Double.parseDouble(txtdisplay.getText());
  403. txtdisplay.setText("");
  404. operate = "/";
  405. }
  406.  
  407. private void modsignActionPerformed(java.awt.event.ActionEvent evt) {
  408. firstnum = Double.parseDouble(txtdisplay.getText());
  409. txtdisplay.setText("");
  410. operate = "%";
  411. }
  412.  
  413. private void equalsignActionPerformed(java.awt.event.ActionEvent evt) {
  414. secondnum = Double.parseDouble(txtdisplay.getText());
  415. if(operate == "+"){
  416. result = firstnum + secondnum;
  417. answer = String.format("%.0f", result);
  418. txtdisplay.setText(answer);
  419. answer="";
  420. takein="";
  421. result=0;
  422. }
  423. else if(operate == "-"){
  424. result = firstnum - secondnum;
  425. answer = String.format("%.0f", result);
  426. txtdisplay.setText(answer);
  427. answer="";
  428. takein="";
  429. result=0;
  430. }
  431. else if(operate == "*"){
  432. result = firstnum * secondnum;
  433. answer = String.format("%.0f", result);
  434. txtdisplay.setText(answer);
  435. answer="";
  436. takein="";
  437. result=0;
  438. }
  439. else if(operate == "/"){
  440. result = firstnum / secondnum;
  441. answer = String.format("%.0f", result);
  442. txtdisplay.setText(answer);
  443. answer="";
  444. takein="";
  445. result=0;
  446. }
  447. else if(operate == "%"){
  448. result = firstnum % secondnum;
  449. answer = String.format("%.0f", result);
  450. txtdisplay.setText(answer);
  451. answer="";
  452. takein="";
  453. result=0;
  454. }
  455.  
  456. }
  457.  
  458. private void onbuttonActionPerformed(java.awt.event.ActionEvent evt) {
  459. txtdisplay.setText("0");
  460. offon=1;
  461. takein="";
  462. }
  463.  
  464. private void offbuttonActionPerformed(java.awt.event.ActionEvent evt) {
  465. txtdisplay.setText("");
  466. takein="";
  467. offon=0;
  468. }
  469.  
  470. /**
  471. * @param args the command line arguments
  472. */
  473. public static void main(String args[]) {
  474. /* Set the Nimbus look and feel */
  475. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  476. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  477. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  478. */
  479. try {
  480. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  481. if ("Nimbus".equals(info.getName())) {
  482. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  483. break;
  484. }
  485. }
  486. } catch (ClassNotFoundException ex) {
  487. java.util.logging.Logger.getLogger(calculatordesign.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  488. } catch (InstantiationException ex) {
  489. java.util.logging.Logger.getLogger(calculatordesign.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  490. } catch (IllegalAccessException ex) {
  491. java.util.logging.Logger.getLogger(calculatordesign.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  492. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  493. java.util.logging.Logger.getLogger(calculatordesign.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  494. }
  495. //</editor-fold>
  496.  
  497. /* Create and display the form */
  498. java.awt.EventQueue.invokeLater(new Runnable() {
  499. public void run() {
  500. new calculatordesign().setVisible(true);
  501. }
  502. });
  503. }
  504.  
  505. // Variables declaration - do not modify
  506. private javax.swing.JButton clearsign;
  507. private javax.swing.JButton divsign;
  508. private javax.swing.JButton dotsign;
  509. private javax.swing.JButton equalsign;
  510. private javax.swing.JButton jButton1;
  511. private javax.swing.JButton key0;
  512. private javax.swing.JButton key1;
  513. private javax.swing.JButton key2;
  514. private javax.swing.JButton key3;
  515. private javax.swing.JButton key4;
  516. private javax.swing.JButton key5;
  517. private javax.swing.JButton key6;
  518. private javax.swing.JButton key7;
  519. private javax.swing.JButton key8;
  520. private javax.swing.JButton key9;
  521. private javax.swing.JButton minussign;
  522. private javax.swing.JButton modsign;
  523. private javax.swing.JButton multisign;
  524. private javax.swing.JButton offbutton;
  525. private javax.swing.JButton onbutton;
  526. private javax.swing.JButton plussign;
  527. private javax.swing.JTextField txtdisplay;
  528. // End of variables declaration
  529. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement