Advertisement
NB52053

JAVA Assignment LAB

Dec 8th, 2016
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.09 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3.  
  4.  
  5. class Calc implements ActionListener
  6. {
  7.     JFrame f;
  8.     JTextField t;
  9.     JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bdiv,bmul,bsub,badd,beq,bclr;
  10.  
  11.     public int a=0,b=0,result=0;
  12.     public int operator=0;
  13.  
  14.     Calc()
  15.     {
  16.         f=new JFrame("Calculator");
  17.         t=new JTextField();
  18.         b1=new JButton("1");
  19.         b2=new JButton("2");
  20.         b3=new JButton("3");
  21.         b4=new JButton("4");
  22.         b5=new JButton("5");
  23.         b6=new JButton("6");
  24.         b7=new JButton("7");
  25.         b8=new JButton("8");
  26.         b9=new JButton("9");
  27.         b0=new JButton("0");
  28.         bdiv=new JButton("/");
  29.         bmul=new JButton("*");
  30.         bsub=new JButton("-");
  31.         badd=new JButton("+");
  32.      
  33.         beq=new JButton("=");
  34.         bclr=new JButton("Clear");
  35.        
  36.         t.setBounds(20,40,200,30);     // X axis, y axis, Height, WIdth
  37.        
  38.         b1.setBounds(30,240,50,40);
  39.         b2.setBounds(95,240,50,40);
  40.         b3.setBounds(160,240,50,40);
  41.         b4.setBounds(30,170,50,40);
  42.         b5.setBounds(95,170,50,40);
  43.         b6.setBounds(160,170,50,40);
  44.         b7.setBounds(30,100,50,40);
  45.         b8.setBounds(95,100,50,40);
  46.         b9.setBounds(160,100,50,40);
  47.         b0.setBounds(100,300,50,40);
  48.        
  49.         badd.setBounds(250,120,50,40);
  50.         bdiv.setBounds(250,200,50,40);          //
  51.        
  52.         bsub.setBounds(250,160,50,40);    //
  53.         bmul.setBounds(250,240,50,40);        
  54.         bclr.setBounds(20,350,100,40);                   //
  55.         beq.setBounds(120,350,100,40);
  56.        
  57.         f.add(t);
  58.        
  59.         f.add(bdiv);
  60.         f.add(b1);
  61.         f.add(b2);
  62.         f.add(b3);
  63.         f.add(b4);
  64.         f.add(b5);
  65.         f.add(b6);
  66.         f.add(b7);
  67.         f.add(b8);
  68.         f.add(b9);
  69.         f.add(b0);
  70.         f.add(bmul);
  71.         f.add(bsub);
  72.         f.add(beq);
  73.         f.add(badd);
  74.         f.add(bclr);
  75.        
  76.        
  77.        
  78.        
  79.         f.setLayout(null);
  80.         f.setVisible(true);
  81.         f.setSize(320,450);
  82.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  83.         f.setResizable(false);
  84.        
  85.         b1.addActionListener(this);
  86.         b2.addActionListener(this);
  87.         b3.addActionListener(this);
  88.         b4.addActionListener(this);
  89.         b5.addActionListener(this);
  90.         b6.addActionListener(this);
  91.         b7.addActionListener(this);
  92.         b8.addActionListener(this);
  93.         b9.addActionListener(this);
  94.         b0.addActionListener(this);
  95.         badd.addActionListener(this);
  96.         bdiv.addActionListener(this);
  97.         bmul.addActionListener(this);
  98.         bsub.addActionListener(this);
  99.        
  100.         beq.addActionListener(this);
  101.        
  102.         bclr.addActionListener(this);
  103.     }
  104.  
  105.     public void actionPerformed(ActionEvent e)
  106.     {
  107.         if(e.getSource()==b1)
  108.             t.setText(t.getText().concat("1"));
  109.        
  110.         if(e.getSource()==b2)
  111.             t.setText(t.getText().concat("2"));
  112.        
  113.         if(e.getSource()==b3)
  114.             t.setText(t.getText().concat("3"));
  115.        
  116.         if(e.getSource()==b4)
  117.             t.setText(t.getText().concat("4"));
  118.        
  119.         if(e.getSource()==b5)
  120.             t.setText(t.getText().concat("5"));
  121.        
  122.         if(e.getSource()==b6)
  123.             t.setText(t.getText().concat("6"));
  124.        
  125.         if(e.getSource()==b7)
  126.             t.setText(t.getText().concat("7"));
  127.        
  128.         if(e.getSource()==b8)
  129.             t.setText(t.getText().concat("8"));
  130.        
  131.         if(e.getSource()==b9)
  132.             t.setText(t.getText().concat("9"));
  133.        
  134.         if(e.getSource()==b0)
  135.             t.setText(t.getText().concat("0"));
  136.        
  137.        
  138.         if(e.getSource()==badd)
  139.         {
  140.             a=Integer.parseInt(t.getText());
  141.             operator=1;
  142.             t.setText("");
  143.         }
  144.        
  145.         if(e.getSource()==bsub)
  146.         {
  147.             a=Integer.parseInt(t.getText());
  148.             operator=2;
  149.             t.setText("");
  150.         }
  151.        
  152.         if(e.getSource()==bmul)
  153.         {
  154.             a=Integer.parseInt(t.getText());
  155.             operator=3;
  156.             t.setText("");
  157.         }
  158.        
  159.         if(e.getSource()==bdiv)
  160.         {
  161.             a=Integer.parseInt(t.getText());
  162.             operator=4;
  163.             t.setText("");
  164.         }
  165.        
  166.         if(e.getSource()==beq)
  167.         {
  168.             b=Integer.parseInt(t.getText());
  169.        
  170.             switch(operator)
  171.             {
  172.                 case 1: result=a+b;
  173.                     break;
  174.        
  175.                 case 2: result=a-b;
  176.                     break;
  177.        
  178.                 case 3: result=a*b;
  179.                     break;
  180.        
  181.                 case 4: result=a/b;
  182.                     break;
  183.        
  184.                 default: result=0;
  185.             }
  186.        
  187.             t.setText(""+result);
  188.         }
  189.        
  190.         if(e.getSource()==bclr)
  191.             t.setText("");
  192.        
  193.      
  194.     }
  195.  
  196.     public static void main(String []args)
  197.     {
  198.         new Calc();
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement