Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package calculator;
- import java.awt.*;
- import java.awt.Font;
- import java.awt.event.*;
- public class Calculator implements ActionListener{
- //Declaring Objects
- Frame f=new Frame();
- Label nom1=new Label("First Number");
- Label nom2=new Label("Second Number");
- Label result=new Label("Result");
- TextField t1=new TextField();
- TextField t2=new TextField();
- TextField t3=new TextField();
- Button add=new Button("Add");
- Button sub=new Button("Sub");
- Button mul=new Button("Mul");
- Button div=new Button("Div");
- Button cancel=new Button("Cancel");
- private Object label;
- Calculator()
- {
- //Adding font
- Font myFont= new Font("Serif",Font.PLAIN,15);
- nom1.setFont(myFont);
- Font myFont2= new Font("Helvetica",Font.PLAIN,15);
- nom2.setFont(myFont2);
- Font myFont3= new Font("Serif",Font.PLAIN,15);
- result.setFont(myFont3);
- //Giving Coordinates
- nom1.setBounds(50,100,100,20);
- nom2.setBounds(50,140,120,20);
- result.setBounds(50,180,100,20);
- t1.setBounds(200,100,100,20);
- t2.setBounds(200,140,100,20);
- t3.setBounds(200,180,100,20);
- add.setBounds(50,250,50,20);
- sub.setBounds(110,250,50,20);
- mul.setBounds(170,250,50,20);
- div.setBounds(230,250,50,20);
- cancel.setBounds(290,250,50,20);
- //Adding Components to the Frame
- f.add(nom1);
- f.add(nom2);
- f.add(result);
- f.add(t1);
- f.add(t2);
- f.add(t3);
- f.add(add);
- f.add(sub);
- f.add(mul);
- f.add(div);
- f.add(cancel);
- f.setBackground(Color.ORANGE);
- add.addActionListener(this);
- sub.addActionListener(this);
- mul.addActionListener(this);
- div.addActionListener(this);
- cancel.addActionListener(this);
- f.setLayout(null);
- f.setVisible(true);
- f.setSize(400,350);
- f.setLocation(500,200);
- }
- public void actionPerformed(ActionEvent e)
- {
- int n1=Integer.parseInt(t1.getText());
- int n2=Integer.parseInt(t2.getText());
- if(e.getSource()==add)
- {
- t3.setText(String.valueOf(n1+n2));
- }
- if(e.getSource()==sub)
- {
- t3.setText(String.valueOf(n1-n2));
- }
- if(e.getSource()==mul)
- {
- t3.setText(String.valueOf(n1*n2));
- }
- if(e.getSource()==div)
- {
- t3.setText(String.valueOf(n1/n2));
- }
- if(e.getSource()==cancel)
- {
- System.exit(0);
- }
- }
- public static void main(String[] args) {
- new Calculator();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement