Ivan_Bochev

GUI-Calculator

Mar 28th, 2019 (edited)
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.89 KB | None | 0 0
  1. package other;
  2.  
  3. import java.awt.EventQueue;
  4.  
  5. import javax.swing.JFrame;
  6. import javax.swing.JLabel;
  7. import javax.swing.JTextField;
  8. import javax.swing.JButton;
  9. import java.awt.Color;
  10. import java.awt.Font;
  11. import java.awt.event.ActionListener;
  12. import java.awt.event.ActionEvent;
  13.  
  14. public class GUICalculator {
  15.  
  16.     private JFrame frame;
  17.     private JTextField Num1;
  18.     private JTextField Num2;
  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.                     GUICalculator window = new GUICalculator();
  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 GUICalculator() {
  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(new Color(144, 238, 144));
  49.         frame.setBounds(100, 100, 785, 607);
  50.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  51.         frame.getContentPane().setLayout(null);
  52.  
  53.         JLabel Num1Field = new JLabel("Number 1:");
  54.         Num1Field.setFont(new Font("Tahoma", Font.PLAIN, 14));
  55.         Num1Field.setBounds(53, 37, 78, 23);
  56.         frame.getContentPane().add(Num1Field);
  57.  
  58.         JLabel lblNewLabel_1 = new JLabel("Number 2:");
  59.         lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
  60.         lblNewLabel_1.setBounds(53, 123, 78, 23);
  61.         frame.getContentPane().add(lblNewLabel_1);
  62.  
  63.         JLabel ResultL = new JLabel("Result:");
  64.         ResultL.setFont(new Font("Tahoma", Font.PLAIN, 14));
  65.         ResultL.setBounds(135, 430, 163, 33);
  66.         frame.getContentPane().add(ResultL);
  67.  
  68.         Num1 = new JTextField();
  69.         Num1.setBounds(141, 37, 71, 27);
  70.         frame.getContentPane().add(Num1);
  71.         Num1.setColumns(10);
  72.  
  73.         Num2 = new JTextField();
  74.         Num2.setBounds(141, 123, 71, 27);
  75.         frame.getContentPane().add(Num2);
  76.         Num2.setColumns(10);
  77.  
  78.         JButton btnNewButton = new JButton("C");
  79.         btnNewButton.addActionListener(new ActionListener() {
  80.             public void actionPerformed(ActionEvent e) {
  81.                 Num1.setText("");
  82.                 Num2.setText("");
  83.                 ResultL.setText("Result:");
  84.             }
  85.         });
  86.         btnNewButton.setBounds(25, 272, 78, 64);
  87.         frame.getContentPane().add(btnNewButton);
  88.  
  89.         JButton btnNewButton_1 = new JButton("*");
  90.         btnNewButton_1.addActionListener(new ActionListener() {
  91.             public void actionPerformed(ActionEvent e) {
  92.                 try {
  93.                     double num1 = Double.parseDouble(Num1.getText());
  94.                     double num2 = Double.parseDouble(Num2.getText());
  95.                     double res = num1 * num2;
  96.                     ResultL.setText(String.format("Result: %.3f", res));
  97.                 } catch (NumberFormatException b) {
  98.                     ResultL.setText("Invalid input!");
  99.                     Num1.setText("");
  100.                     Num2.setText("");
  101.                 }
  102.             }
  103.         });
  104.  
  105.         btnNewButton_1.setBounds(275, 272, 71, 64);
  106.         frame.getContentPane().add(btnNewButton_1);
  107.  
  108.         JButton btnNewButton_2 = new JButton("+");
  109.         btnNewButton_2.addActionListener(new ActionListener() {
  110.             public void actionPerformed(ActionEvent arg0) {
  111.                 try {
  112.                     double num1 = Double.parseDouble(Num1.getText());
  113.                     double num2 = Double.parseDouble(Num2.getText());
  114.                     double res = num1 + num2;
  115.                     ResultL.setText(String.format("Result: %.3f", res));
  116.                 } catch (NumberFormatException e) {
  117.                     ResultL.setText("Invalid input!");
  118.                     Num1.setText("");
  119.                     Num2.setText("");
  120.                 }
  121.             }
  122.         });
  123.         btnNewButton_2.setBounds(113, 272, 71, 64);
  124.         frame.getContentPane().add(btnNewButton_2);
  125.  
  126.         JButton btnNewButton_3 = new JButton("/");
  127.         btnNewButton_3.addActionListener(new ActionListener() {
  128.             public void actionPerformed(ActionEvent e) {
  129.                 try {
  130.                     double num1 = Double.parseDouble(Num1.getText());
  131.                     double num2 = Double.parseDouble(Num2.getText());
  132.                     double res = num1 / num2;
  133.                     ResultL.setText(String.format("Result: %.3f", res));
  134.                 } catch (NumberFormatException c) {
  135.                     ResultL.setText("Invalid input!");
  136.                     Num1.setText("");
  137.                     Num2.setText("");
  138.                 }
  139.             }
  140.         });
  141.         btnNewButton_3.setBounds(356, 272, 71, 64);
  142.         frame.getContentPane().add(btnNewButton_3);
  143.  
  144.         JButton btnNewButton_4 = new JButton("-");
  145.         btnNewButton_4.addActionListener(new ActionListener() {
  146.             public void actionPerformed(ActionEvent e) {
  147.                 try {
  148.                     double num1 = Double.parseDouble(Num1.getText());
  149.                     double num2 = Double.parseDouble(Num2.getText());
  150.                     double res = num1 - num2;
  151.                     ResultL.setText(String.format("Result: %.3f", res));
  152.                 } catch (NumberFormatException a) {
  153.                     ResultL.setText("Invalid input!");
  154.                     Num1.setText("");
  155.                     Num2.setText("");
  156.                 }
  157.  
  158.             }
  159.         });
  160.         btnNewButton_4.setBounds(194, 272, 71, 64);
  161.         frame.getContentPane().add(btnNewButton_4);
  162.  
  163.         JButton btnNewButton_5 = new JButton("Min");
  164.         btnNewButton_5.addActionListener(new ActionListener() {
  165.             public void actionPerformed(ActionEvent e) {
  166.                 try {
  167.                     double num1 = Double.parseDouble(Num1.getText());
  168.                     double num2 = Double.parseDouble(Num2.getText());
  169.                     double res = Math.min(num1, num2);
  170.                     ResultL.setText(String.format("Result: %.3f", res));
  171.  
  172.                 } catch (NumberFormatException f) {
  173.                     ResultL.setText("Invalid input!");
  174.                     Num1.setText("");
  175.                     Num2.setText("");
  176.                 }
  177.             }
  178.         });
  179.         btnNewButton_5.setBounds(512, 272, 78, 64);
  180.         frame.getContentPane().add(btnNewButton_5);
  181.  
  182.         JButton btnNewButton_6 = new JButton("Max");
  183.         btnNewButton_6.addActionListener(new ActionListener() {
  184.             public void actionPerformed(ActionEvent e) {
  185.                 try {
  186.                     double num1 = Double.parseDouble(Num1.getText());
  187.                     double num2 = Double.parseDouble(Num2.getText());
  188.                     double res = Math.max(num1, num2);
  189.                     ResultL.setText(String.format("Result: %.3f", res));
  190.  
  191.                 } catch (NumberFormatException f) {
  192.                     ResultL.setText("Invalid input!");
  193.                     Num1.setText("");
  194.                     Num2.setText("");
  195.                 }
  196.             }
  197.         });
  198.  
  199.         btnNewButton_6.setBounds(608, 272, 78, 64);
  200.         frame.getContentPane().add(btnNewButton_6);
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment