Advertisement
Dar954826

Calculator.java

Mar 7th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.12 KB | None | 0 0
  1.  
  2. import java.awt.EventQueue;
  3. import javax.swing.JFrame;
  4. import javax.swing.JPanel;
  5. import javax.swing.border.EmptyBorder;
  6. import javax.swing.JTextField;
  7. import javax.swing.JButton;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.Font;
  11. import java.awt.event.MouseAdapter;
  12. import java.awt.event.MouseEvent;
  13.  
  14. public class Calculator extends JFrame {
  15.     private static final long serialVersionUID = 1L;
  16.     public static float a = 0;
  17.     public static float b = 0;
  18.     protected static int op = 0;
  19.     private JPanel contentPane;
  20.     private JTextField textField;
  21.     public static void main(String[] args) {
  22.         EventQueue.invokeLater(new Runnable() {
  23.             public void run() {
  24.                 try {
  25.                     Calculator frame = new Calculator();
  26.                     frame.setVisible(true);
  27.                 } catch (Exception e) {
  28.                     e.printStackTrace();
  29.                 }
  30.             }
  31.         });
  32.     }
  33.     public Calculator() {
  34.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  35.         setBounds(100, 100, 450, 300);
  36.         contentPane = new JPanel();
  37.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  38.         setContentPane(contentPane);
  39.         contentPane.setLayout(null);
  40.  
  41.         textField = new JTextField();
  42.         textField.setEditable(false);
  43.         textField.setBounds(10, 11, 198, 20);
  44.         contentPane.add(textField);
  45.         textField.setColumns(10);
  46.  
  47.         JButton btnNewButton = new JButton("C");
  48.         btnNewButton.addMouseListener(new MouseAdapter() {
  49.             @Override
  50.             public void mouseClicked(MouseEvent e) {
  51.                 textField.setText("");
  52.             }
  53.         });
  54.         btnNewButton.addActionListener(new ActionListener() {
  55.             public void actionPerformed(ActionEvent e) {
  56.             }
  57.         });
  58.         btnNewButton.setBounds(302, 10, 48, 23);
  59.         contentPane.add(btnNewButton);
  60.  
  61.         JButton btnAc = new JButton("AC");
  62.         btnAc.addMouseListener(new MouseAdapter() {
  63.             @Override
  64.             public void mouseClicked(MouseEvent e) {
  65.                 Calculator.a = (float) 0.0;
  66.                 Calculator.b = (float) 0.0;
  67.                 Calculator.b = 0;
  68.                 textField.setText("");
  69.  
  70.             }
  71.         });
  72.         btnAc.setBounds(360, 10, 64, 23);
  73.         contentPane.add(btnAc);
  74.  
  75.         JButton btnNewButton_1 = new JButton("7");
  76.         btnNewButton_1.addMouseListener(new MouseAdapter() {
  77.             @Override
  78.             public void mouseClicked(MouseEvent e) {
  79.                 textField.setText(textField.getText() + "7");
  80.             }
  81.         });
  82.         btnNewButton_1.setFont(new Font("Tahoma", Font.PLAIN, 20));
  83.         btnNewButton_1.setBounds(10, 42, 48, 48);
  84.         contentPane.add(btnNewButton_1);
  85.  
  86.         JButton button = new JButton("8");
  87.         button.addMouseListener(new MouseAdapter() {
  88.             @Override
  89.             public void mouseClicked(MouseEvent e) {
  90.                 textField.setText(textField.getText() + "8");
  91.             }
  92.         });
  93.         button.setFont(new Font("Tahoma", Font.PLAIN, 20));
  94.         button.setBounds(76, 42, 48, 48);
  95.         contentPane.add(button);
  96.  
  97.         JButton button_1 = new JButton("9");
  98.         button_1.addMouseListener(new MouseAdapter() {
  99.             @Override
  100.             public void mouseClicked(MouseEvent e) {
  101.                 textField.setText(textField.getText() + "9");
  102.             }
  103.         });
  104.         button_1.setFont(new Font("Tahoma", Font.PLAIN, 20));
  105.         button_1.setBounds(150, 42, 48, 48);
  106.         contentPane.add(button_1);
  107.  
  108.         JButton button_2 = new JButton("6");
  109.         button_2.addMouseListener(new MouseAdapter() {
  110.             @Override
  111.             public void mouseClicked(MouseEvent e) {
  112.                 textField.setText(textField.getText() + "6");
  113.             }
  114.         });
  115.         button_2.setFont(new Font("Tahoma", Font.PLAIN, 20));
  116.         button_2.setBounds(150, 101, 48, 48);
  117.         contentPane.add(button_2);
  118.  
  119.         JButton button_3 = new JButton("5");
  120.         button_3.addMouseListener(new MouseAdapter() {
  121.             @Override
  122.             public void mouseClicked(MouseEvent e) {
  123.                 textField.setText(textField.getText() + "5");
  124.             }
  125.         });
  126.         button_3.setFont(new Font("Tahoma", Font.PLAIN, 20));
  127.         button_3.setBounds(76, 101, 48, 48);
  128.         contentPane.add(button_3);
  129.  
  130.         JButton button_4 = new JButton("4");
  131.         button_4.addMouseListener(new MouseAdapter() {
  132.             @Override
  133.             public void mouseClicked(MouseEvent e) {
  134.                 textField.setText(textField.getText() + "4");
  135.             }
  136.         });
  137.         button_4.setFont(new Font("Tahoma", Font.PLAIN, 20));
  138.         button_4.setBounds(10, 101, 48, 48);
  139.         contentPane.add(button_4);
  140.  
  141.         JButton button_5 = new JButton("3");
  142.         button_5.addMouseListener(new MouseAdapter() {
  143.             @Override
  144.             public void mouseClicked(MouseEvent e) {
  145.                 textField.setText(textField.getText() + "3");
  146.             }
  147.         });
  148.         button_5.setFont(new Font("Tahoma", Font.PLAIN, 20));
  149.         button_5.setBounds(150, 160, 48, 48);
  150.         contentPane.add(button_5);
  151.  
  152.         JButton button_6 = new JButton("2");
  153.         button_6.addMouseListener(new MouseAdapter() {
  154.             @Override
  155.             public void mouseClicked(MouseEvent e) {
  156.                 textField.setText(textField.getText() + "2");
  157.             }
  158.         });
  159.         button_6.setFont(new Font("Tahoma", Font.PLAIN, 20));
  160.         button_6.setBounds(76, 160, 48, 48);
  161.         contentPane.add(button_6);
  162.  
  163.         JButton button_7 = new JButton("1");
  164.         button_7.addMouseListener(new MouseAdapter() {
  165.             @Override
  166.             public void mouseClicked(MouseEvent e) {
  167.                 textField.setText(textField.getText() + "1");
  168.             }
  169.         });
  170.         button_7.addActionListener(new ActionListener() {
  171.             public void actionPerformed(ActionEvent e) {
  172.             }
  173.         });
  174.         button_7.setFont(new Font("Tahoma", Font.PLAIN, 20));
  175.         button_7.setBounds(10, 160, 48, 48);
  176.         contentPane.add(button_7);
  177.  
  178.         JButton button_8 = new JButton("\u221A");
  179.         button_8.addMouseListener(new MouseAdapter() {
  180.             @Override
  181.             public void mouseClicked(MouseEvent e) {
  182.                 Calculator.a = Float.parseFloat(textField.getText());
  183.                 Calculator.op = 6;
  184.                 textField.setText("");
  185.             }
  186.         });
  187.         button_8.setFont(new Font("Tahoma", Font.PLAIN, 20));
  188.         button_8.setBounds(333, 44, 66, 48);
  189.         contentPane.add(button_8);
  190.  
  191.         JButton button_9 = new JButton("^");
  192.         button_9.addMouseListener(new MouseAdapter() {
  193.             @Override
  194.             public void mouseClicked(MouseEvent e) {
  195.                 Calculator.a = Float.parseFloat(textField.getText());
  196.                 Calculator.op = 5;
  197.                 textField.setText("");
  198.             }
  199.         });
  200.         button_9.setFont(new Font("Tahoma", Font.PLAIN, 20));
  201.         button_9.setBounds(257, 44, 66, 48);
  202.         contentPane.add(button_9);
  203.  
  204.         JButton button_10 = new JButton("/");
  205.         button_10.addMouseListener(new MouseAdapter() {
  206.             @Override
  207.             public void mouseClicked(MouseEvent e) {
  208.                 Calculator.a = Float.parseFloat(textField.getText());
  209.                 Calculator.op = 4;
  210.                 textField.setText("");
  211.             }
  212.         });
  213.         button_10.setFont(new Font("Tahoma", Font.PLAIN, 20));
  214.         button_10.setBounds(333, 103, 66, 48);
  215.         contentPane.add(button_10);
  216.  
  217.         JButton button_11 = new JButton("*");
  218.         button_11.addMouseListener(new MouseAdapter() {
  219.             @Override
  220.             public void mouseClicked(MouseEvent e) {
  221.                 Calculator.a = Float.parseFloat(textField.getText());
  222.                 Calculator.op = 3;
  223.                 textField.setText("");
  224.             }
  225.         });
  226.         button_11.setFont(new Font("Tahoma", Font.PLAIN, 20));
  227.         button_11.setBounds(257, 103, 66, 48);
  228.         contentPane.add(button_11);
  229.  
  230.         JButton button_12 = new JButton("-");
  231.         button_12.addMouseListener(new MouseAdapter() {
  232.             @Override
  233.             public void mouseClicked(MouseEvent e) {
  234.                 Calculator.a = Float.parseFloat(textField.getText());
  235.                 Calculator.op = 2;
  236.                 textField.setText("");
  237.             }
  238.         });
  239.         button_12.setFont(new Font("Tahoma", Font.PLAIN, 20));
  240.         button_12.setBounds(333, 162, 66, 48);
  241.         contentPane.add(button_12);
  242.  
  243.         JButton button_13 = new JButton("+");
  244.         button_13.addMouseListener(new MouseAdapter() {
  245.             @Override
  246.             public void mouseClicked(MouseEvent e) {
  247.                 Calculator.a = Float.parseFloat(textField.getText());
  248.                 Calculator.op = 1;
  249.                 textField.setText("");
  250.             }
  251.         });
  252.         button_13.setFont(new Font("Tahoma", Font.PLAIN, 20));
  253.         button_13.setBounds(257, 162, 66, 48);
  254.         contentPane.add(button_13);
  255.  
  256.         JButton btnNewButton_2 = new JButton("=");
  257.         btnNewButton_2.addMouseListener(new MouseAdapter() {
  258.             @Override
  259.             public void mouseClicked(MouseEvent e) {
  260.                 if (Calculator.b == 0) {
  261.                     Calculator.b = Float.parseFloat(textField.getText());
  262.                 }
  263.                 if (Calculator.op == 1) {
  264.                     textField.setText(Float.toString(Calculator.a + Calculator.b));
  265.                     Calculator.a = Calculator.a + Calculator.b;
  266.                 }
  267.                 if (Calculator.op == 2) {
  268.                     textField.setText(Float.toString(Calculator.a - Calculator.b));
  269.                     Calculator.a = Calculator.a - Calculator.b;
  270.                 }
  271.                 if (Calculator.op == 3) {
  272.                     textField.setText(Float.toString(Calculator.a * Calculator.b));
  273.                     Calculator.a = Calculator.a * Calculator.b;
  274.                 }
  275.                 if (Calculator.op == 4) {
  276.                     textField.setText(Float.toString(Calculator.a / Calculator.b));
  277.                     Calculator.a = Calculator.a / Calculator.b;
  278.                 }
  279.                 if (Calculator.op == 5) {
  280.                     textField.setText(Float.toString((int) Math.pow(Calculator.a,
  281.                             Calculator.b)));
  282.                     Calculator.a = (int) Math.pow(Calculator.a, Calculator.b);
  283.                 }
  284.                 if (Calculator.op == 6) {
  285.                     float h = 1 / Calculator.b;
  286.                     textField.setText(Float.toString((float) Math.pow(Calculator.a,
  287.                             h)));
  288.                     Calculator.a = (float) Math.pow(Calculator.a, h);
  289.  
  290.                 }
  291.             }
  292.         });
  293.         btnNewButton_2.setBounds(257, 214, 142, 37);
  294.         contentPane.add(btnNewButton_2);
  295.  
  296.         JButton btnDel = new JButton("DEL");
  297.         btnDel.addMouseListener(new MouseAdapter() {
  298.             @Override
  299.             public void mouseClicked(MouseEvent e) {
  300.                 StringBuilder j = new StringBuilder();
  301.                 j.append(textField.getText());
  302.                 textField.setText(j.substring(0,
  303.                         textField.getText().length() - 1));
  304.             }
  305.         });
  306.         btnDel.setBounds(218, 10, 74, 23);
  307.         contentPane.add(btnDel);
  308.        
  309.         JButton button_14 = new JButton("0");
  310.         button_14.addMouseListener(new MouseAdapter() {
  311.             @Override
  312.             public void mouseClicked(MouseEvent e) {textField.setText(textField.getText() + "0");
  313.             }
  314.         });
  315.         button_14.setFont(new Font("Tahoma", Font.PLAIN, 20));
  316.         button_14.setBounds(76, 214, 48, 48);
  317.         contentPane.add(button_14);
  318.        
  319.         JButton button_15 = new JButton(".");
  320.         button_15.addMouseListener(new MouseAdapter() {
  321.             @Override
  322.             public void mouseClicked(MouseEvent e) {
  323.                 boolean g=true;
  324.                 String b=".";
  325.                 for(int a=0;a<textField.getText().length();a++){
  326.                     if(textField.getText().charAt(a)==b.charAt(0)){g=false;}
  327.                 }
  328.                 if(g){
  329.                 textField.setText(textField.getText() + ".");}
  330.             }
  331.         });
  332.         button_15.setFont(new Font("Tahoma", Font.PLAIN, 20));
  333.         button_15.setBounds(10, 214, 48, 48);
  334.         contentPane.add(button_15);
  335.     }
  336. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement