Advertisement
Guest User

calculator

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