Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package calculator2;
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JTextField;
- public class Calculator extends JFrame{
- private Container c;
- private JTextField tf1,tf2,tf3;
- private JButton b1,b2,b3,b4;
- Calculator()
- {
- lol();
- }
- public void lol()
- {
- c = this.getContentPane();
- c.setLayout(null);
- c.setBackground(Color.ORANGE);
- tf1 = new JTextField();
- tf1.setBounds(100,50,100,50);
- c.add(tf1);
- tf2 = new JTextField();
- tf2.setBounds(100,120,100,50);
- c.add(tf2);
- tf3 = new JTextField();
- tf3.setBounds(100,200,100,50);
- tf3.setEditable(false);
- c.add(tf3);
- b1 = new JButton("+");
- b1.setBounds(100,300,100,50);
- c.add(b1);
- b2 = new JButton("-");
- b2.setBounds(220,300,100,50);
- c.add(b2);
- b3 = new JButton("*");
- b3.setBounds(340,300,100,50);
- c.add(b3);
- b4 = new JButton("/");
- b4.setBounds(460,300,100,50);
- c.add(b4);
- Handler h = new Handler();
- b1.addActionListener(h);
- b2.addActionListener(h);
- b3.addActionListener(h);
- b4.addActionListener(h);
- }
- class Handler implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent e){
- String s1 = tf1.getText();
- String s2 = tf2.getText();
- int a = Integer.parseInt(s1);
- int b = Integer.parseInt(s2);
- int c = 0;
- if(e.getSource() == b1)
- {
- c=a+b;
- }
- else if(e.getSource() == b2)
- {
- c=a-b;
- }
- else if(e.getSource() == b3)
- {
- c=a*b;
- }
- else if(e.getSource() == b4)
- {
- c=a/b;
- }
- String result = String.valueOf(c);
- tf3.setText(result);
- }
- }
- public static void main(String[] args) {
- Calculator cal = new Calculator();
- cal.setVisible(true);
- cal.setBounds(200,100,600,450);
- cal.setTitle("CALCULATOR");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment