Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.event.*;
- class Calc implements ActionListener
- {
- JFrame Frame;
- JTextField TF;
- JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,badd,bclr;
- static int a=0,b=0,result=0;
- static int operator=0;
- Calc()
- {
- Frame = new JFrame("Calculator");
- TF=new JTextField();
- b1=new JButton("1");
- b2=new JButton("2");
- b3=new JButton("3");
- b4=new JButton("4");
- b5=new JButton("5");
- b6=new JButton("6");
- b7=new JButton("7");
- b8=new JButton("8");
- b9=new JButton("9");
- b0=new JButton("0");
- badd=new JButton("+"); // For addition button
- bclr=new JButton("Clear"); // For clear button
- TF.setBounds(30,40,280,30); // TextFIelds Axis
- b1.setBounds(40,240,50,40);
- b2.setBounds(110,240,50,40);
- b3.setBounds(180,240,50,40);
- b4.setBounds(40,170,50,40);
- b5.setBounds(110,170,50,40);
- b6.setBounds(180,170,50,40);
- b7.setBounds(40,100,50,40);
- b8.setBounds(110,100,50,40);
- b9.setBounds(180,100,50,40);
- b0.setBounds(110,310,50,40);
- badd.setBounds(50,300,50,50);
- bclr.setBounds(180,380,100,40);
- Frame.add(TF); // TextField
- Frame.add(b1); // Digits
- Frame.add(b2);
- Frame.add(b3);
- Frame.add(b4);
- Frame.add(b5);
- Frame.add(b6);
- Frame.add(b7);
- Frame.add(b8);
- Frame.add(b9);
- Frame.add(b0); // Digits
- Frame.add(badd); // AdditionButton
- Frame.add(bclr);
- Frame.setLayout(null);
- Frame.setVisible(true);
- Frame.setSize(350,500);
- Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- Frame.setResizable(false);
- b1.addActionListener(this);
- b2.addActionListener(this);
- b3.addActionListener(this);
- b4.addActionListener(this);
- b5.addActionListener(this);
- b6.addActionListener(this);
- b7.addActionListener(this);
- b8.addActionListener(this);
- b9.addActionListener(this);
- b0.addActionListener(this);
- badd.addActionListener(this);
- bclr.addActionListener(this);
- }
- public void actionPerformed(ActionEvent e)
- {
- if(e.getSource()==b1)
- TF.setText(TF.getText().concat("1"));
- if(e.getSource()==b2)
- TF.setText(TF.getText().concat("2"));
- if(e.getSource()==b3)
- TF.setText(TF.getText().concat("3"));
- if(e.getSource()==b4)
- TF.setText(TF.getText().concat("4"));
- if(e.getSource()==b5)
- TF.setText(TF.getText().concat("5"));
- if(e.getSource()==b6)
- TF.setText(TF.getText().concat("6"));
- if(e.getSource()==b7)
- TF.setText(TF.getText().concat("7"));
- if(e.getSource()==b8)
- TF.setText(TF.getText().concat("8"));
- if(e.getSource()==b9)
- TF.setText(TF.getText().concat("9"));
- if(e.getSource()==b0)
- TF.setText(TF.getText().concat("0"));
- /*if(e.getSource()==badd)
- {
- a=Double.parseDouble(t.getText());
- operator=1;
- t.setText("");
- }*/
- if(e.getSource()==badd)
- {
- a=Integer.parseInt(TF.getText());
- b=Integer.parseInt(TF.getText());
- TF.setText("");
- switch(operator)
- {
- case 1: result=a+b;
- break;
- default: result=0;
- }
- TF.setText(""+result);
- }
- if(e.getSource()==bclr)
- TF.setText("");
- }
- public static void main(String []args)
- {
- new Calc();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement