Advertisement
KechevD

Calculator

Jun 24th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. import java.awt.EventQueue;
  2.  
  3. import javax.swing.JFrame;
  4. import javax.swing.JTextField;
  5. import javax.swing.JLabel;
  6. import javax.swing.JButton;
  7. import java.awt.event.ActionListener;
  8. import java.awt.event.ActionEvent;
  9. import javax.swing.SwingConstants;
  10. import java.awt.Font;
  11. import java.awt.Color;
  12. import java.awt.SystemColor;
  13.  
  14. public class CalculatorUKTC {
  15.  
  16. private JFrame frame;
  17. private JTextField num1Field;
  18. private JTextField num2Field;
  19.  
  20. /**
  21. * Launch the application.
  22. */
  23. public static void main(String[] args) {
  24. EventQueue.invokeLater(new Runnable() {
  25. public void run() {
  26. try {
  27. CalculatorUKTC window = new CalculatorUKTC();
  28. window.frame.setVisible(true);
  29. } catch (Exception e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. });
  34. }
  35.  
  36. /**
  37. * Create the application.
  38. */
  39. public CalculatorUKTC() {
  40. initialize();
  41. }
  42.  
  43. /**
  44. * Initialize the contents of the frame.
  45. */
  46. private void initialize() {
  47. frame = new JFrame();
  48. frame.getContentPane().setBackground(SystemColor.menu);
  49. frame.getContentPane().setForeground(Color.BLACK);
  50. frame.setBounds(100, 100, 450, 300);
  51. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  52. frame.getContentPane().setLayout(null);
  53.  
  54. num1Field = new JTextField();
  55. num1Field.setBounds(169, 44, 86, 20);
  56. frame.getContentPane().add(num1Field);
  57. num1Field.setColumns(10);
  58.  
  59. num2Field = new JTextField();
  60. num2Field.setBounds(169, 87, 86, 20);
  61. frame.getContentPane().add(num2Field);
  62. num2Field.setColumns(10);
  63.  
  64. JLabel resultLbl = new JLabel("Result:");
  65. resultLbl.setFont(new Font("Tahoma", Font.PLAIN, 18));
  66. resultLbl.setHorizontalAlignment(SwingConstants.LEFT);
  67. resultLbl.setBounds(47, 210, 149, 27);
  68. frame.getContentPane().add(resultLbl);
  69.  
  70. JLabel lblNewLabel = new JLabel("Number 1:");
  71. lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
  72. lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
  73. lblNewLabel.setBounds(29, 44, 108, 17);
  74. frame.getContentPane().add(lblNewLabel);
  75.  
  76. JLabel lblNewLabel_1 = new JLabel("Number 2:");
  77. lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 16));
  78. lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
  79. lblNewLabel_1.setBounds(29, 87, 108, 17);
  80. frame.getContentPane().add(lblNewLabel_1);
  81.  
  82. JButton clearBtn = new JButton("C");
  83. clearBtn.addActionListener(new ActionListener() {
  84. public void actionPerformed(ActionEvent arg0) {
  85. num1Field.setText("");
  86. num2Field.setText("");
  87. resultLbl.setText("Result:");
  88. }
  89. });
  90. clearBtn.setBounds(29, 140, 49, 44);
  91. frame.getContentPane().add(clearBtn);
  92.  
  93. JButton multiplyBtn = new JButton("X");
  94. multiplyBtn.addActionListener(new ActionListener() {
  95. public void actionPerformed(ActionEvent e) {
  96. try {
  97. double num1 = Double.parseDouble(num1Field.getText());
  98. double num2 = Double.parseDouble(num2Field.getText());
  99. double result = num1 * num2;
  100. resultLbl.setText("Result: " + result);
  101. } catch (NumberFormatException e1) {
  102. resultLbl.setText("Invalid input!");
  103. num1Field.setText("");
  104. num2Field.setText("");
  105. }
  106. }
  107. });
  108. multiplyBtn.setBounds(88, 140, 49, 44);
  109. frame.getContentPane().add(multiplyBtn);
  110.  
  111. JButton devideBtn = new JButton("/");
  112. devideBtn.addActionListener(new ActionListener() {
  113. public void actionPerformed(ActionEvent e) {
  114. try {
  115. double num1 = Double.parseDouble(num1Field.getText());
  116. double num2 = Double.parseDouble(num2Field.getText());
  117. double result = num1 / num2;
  118. resultLbl.setText("Result: " + result);
  119. } catch (NumberFormatException e1) {
  120. resultLbl.setText("Invalid input!");
  121. num1Field.setText("");
  122. num2Field.setText("");
  123. }
  124. }
  125. });
  126. devideBtn.setBounds(147, 140, 49, 44);
  127. frame.getContentPane().add(devideBtn);
  128.  
  129. JButton sumBtn = new JButton("+");
  130. sumBtn.addActionListener(new ActionListener() {
  131. public void actionPerformed(ActionEvent e) {
  132. try {
  133. double num1 = Double.parseDouble(num1Field.getText());
  134. double num2 = Double.parseDouble(num2Field.getText());
  135. double result = num1 + num2;
  136. resultLbl.setText("Result: " + result);
  137. } catch (NumberFormatException e1) {
  138. resultLbl.setText("Invalid input!");
  139. num1Field.setText("");
  140. num2Field.setText("");
  141. }
  142. }
  143. });
  144. sumBtn.setBounds(206, 140, 49, 44);
  145. frame.getContentPane().add(sumBtn);
  146.  
  147. JButton button_2 = new JButton("-");
  148. button_2.addActionListener(new ActionListener() {
  149. public void actionPerformed(ActionEvent e) {
  150. try {
  151. double num1 = Double.parseDouble(num1Field.getText());
  152. double num2 = Double.parseDouble(num2Field.getText());
  153. double result = num1 - num2;
  154. resultLbl.setText("Result: " + result);
  155. } catch (NumberFormatException e1) {
  156. resultLbl.setText("Invalid input!");
  157. num1Field.setText("");
  158. num2Field.setText("");
  159. }
  160. }
  161. });
  162. button_2.setBounds(265, 140, 49, 44);
  163. frame.getContentPane().add(button_2);
  164.  
  165. }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement