Advertisement
jbozhich

Calculator

Apr 11th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. public class Calculator extends JFrame{
  5.    
  6.     JButton add, subtract, multiply, divide;
  7.     JTextField num1, num2;
  8.     JLabel result, enter1, enter2;
  9.    
  10.    
  11.     public Calculator(){
  12.         setLayout(new GridBagLayout());
  13.         GridBagConstraints c = new GridBagConstraints();
  14.        
  15.         enter1 = new JLabel("1st:");
  16.         c.fill = GridBagConstraints.HORIZONTAL;
  17.         c.gridx = 0;
  18.         c.gridy = 0;
  19.         c.gridwidth = 0;
  20.         add(enter1, c);
  21.        
  22.         num1 = new JTextField(" ");
  23.         c.fill = GridBagConstraints.HORIZONTAL;
  24.         c.gridx = 1;
  25.         c.gridy = 0;
  26.         c.gridwidth = 3;
  27.         add(num1, c);
  28.        
  29.         enter2 = new JLabel("2nd: ");
  30.         c.fill = GridBagConstraints.HORIZONTAL;
  31.         c.gridx = 0;
  32.         c.gridy = 1;
  33.         c.gridwidth = 1;
  34.         add(enter2, c);
  35.        
  36.         num2 = new JTextField(" ");
  37.         c.fill = GridBagConstraints.HORIZONTAL;
  38.         c.gridx = 1;
  39.         c.gridy = 1;
  40.         c.gridwidth = 3;
  41.         add(num2, c);
  42.        
  43.         add = new JButton(" + ");
  44.         c.fill = GridBagConstraints.HORIZONTAL;
  45.         c.gridx = 0;
  46.         c.gridy = 2;
  47.         c.gridwidth = 1;
  48.         add(add, c);
  49.        
  50.         subtract = new JButton(" - ");
  51.         c.fill = GridBagConstraints.HORIZONTAL;
  52.         c.gridx = 1;
  53.         c.gridy = 2;
  54.         c.gridwidth = 0;
  55.         add(subtract, c);
  56.        
  57.         multiply = new JButton(" * ");
  58.         c.fill = GridBagConstraints.HORIZONTAL;
  59.         c.gridx = 2;
  60.         c.gridy = 2;
  61.         c.gridwidth = 0;
  62.         add(multiply, c);
  63.        
  64.         divide = new JButton(" / ");
  65.         c.fill = GridBagConstraints.HORIZONTAL;
  66.         c.gridx = 3;
  67.         c.gridy = 2;
  68.         c.gridwidth = 0;
  69.         add(divide, c);
  70.        
  71.         result = new JLabel (" ");
  72.         c.fill = GridBagConstraints.HORIZONTAL;
  73.         c.gridx = 0;
  74.         c.gridy = 4;
  75.         c.gridwidth = 4;
  76.         add(result, c);
  77.        
  78.         event a = new event();
  79.         add.addActionListener(a);
  80.         subtract.addActionListener(a);
  81.         multiply.addActionListener(a);
  82.         divide.addActionListener(a);
  83.     }
  84.     public class event implements ActionListener{
  85.         public void ActionPerformed(ActionEvent a){
  86.             double number1, number2;
  87.            
  88.             try{
  89.                 number1 = Double.parseDouble(num1.getText());
  90.             }catch (NumberFormatException e){
  91.                 result.setText("Illegal data in 1st field!");
  92.                 return;
  93.                 }
  94.            
  95.             try{
  96.                 number2 = Double.parseDouble(num2.getText());
  97.             }catch (NumberFormatException e){
  98.                 result.setText("Illegal data in 2nd field!");
  99.                 return;
  100.                 }
  101.            
  102.             String operation = a.getActionCommand();
  103.            
  104.         if(operation.equals(" + ")){
  105.                 double sum = number1 + number2;
  106.                 result.setText(number1 + "+" + number2 + "=" + sum);
  107.         }else if(operation.equals(" - ")){
  108.             double diff = number1 - number2;
  109.             result.setText(number1 + " - " + number2 + "=" + diff);
  110.         }else if(operation.equals(" * ")){
  111.             double factor = number1 * number2;
  112.             result.setText(number1 + " * " + number2 + "=" + factor);
  113.         }else if(operation.equals(" / ")){
  114.             if(number2 == 0){
  115.                 result.setText("Cannot divide by zero");
  116.             }else{
  117.                 double quotient = number1/number2;
  118.                 result.setText(number1 + "/" + number2 + "=" + quotient);
  119. }
  120.         }
  121.         }
  122.     }
  123. public static void main(String args[]){
  124.     Calculator gui = new Calculator();
  125.     gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  126.     gui.setVisible(true);
  127.     gui.setSize(250, 175);
  128.     gui.setTitle("My First Calculator");}
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement