Advertisement
nurnobishanto73

SimpleCalculator

Oct 16th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.27 KB | None | 0 0
  1. import java.awt.EventQueue;
  2.  
  3. import javax.swing.JFrame;
  4. import javax.swing.JTextField;
  5. import javax.swing.SwingConstants;
  6. import java.awt.Color;
  7. import java.awt.Font;
  8. import javax.swing.JButton;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.ActionEvent;
  11. import javax.swing.border.MatteBorder;
  12. import javax.swing.UIManager;
  13. import javax.swing.border.BevelBorder;
  14. import javax.swing.border.EmptyBorder;
  15. import javax.swing.border.LineBorder;
  16. import java.awt.Component;
  17. import javax.swing.JRadioButton;
  18.  
  19. public class SimpleCalculator {
  20.  
  21.     private JFrame frame;
  22.     private JTextField textR;
  23.     double n1;
  24.     double n2;
  25.     double result;
  26.     String Op;
  27.     String Ans;
  28.    
  29.  
  30.     /**
  31.      * Launch the application.
  32.      */
  33.     public static void main(String[] args) {
  34.         EventQueue.invokeLater(new Runnable() {
  35.             public void run() {
  36.                 try {
  37.                     SimpleCalculator window = new SimpleCalculator();
  38.                     window.frame.setVisible(true);
  39.                 } catch (Exception e) {
  40.                     e.printStackTrace();
  41.                 }
  42.             }
  43.         });
  44.     }
  45.  
  46.     /**
  47.      * Create the application.
  48.      */
  49.     public SimpleCalculator() {
  50.         initialize();
  51.     }
  52.  
  53.     /**
  54.      * Initialize the contents of the frame.
  55.      */
  56.     private void initialize() {
  57.         frame = new JFrame();
  58.         frame.getContentPane().setFont(new Font("Tahoma", Font.PLAIN, 18));
  59.         frame.getContentPane().setForeground(Color.GRAY);
  60.         frame.getContentPane().setBackground(Color.DARK_GRAY);
  61.         frame.setBounds(100, 100, 370, 555);
  62.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  63.         frame.getContentPane().setLayout(null);
  64.        
  65.         textR = new JTextField();
  66.         textR.setFont(new Font("Tahoma", Font.PLAIN, 20));
  67.         textR.setForeground(Color.BLACK);
  68.         textR.setBackground(Color.WHITE);
  69.         textR.setHorizontalAlignment(SwingConstants.RIGHT);
  70.         textR.setBounds(15, 15, 327, 60);
  71.         frame.getContentPane().add(textR);
  72.         textR.setColumns(10);
  73.        
  74.         ///---------ROW 1-------------
  75.         JButton btnAC = new JButton("CLEAR");
  76.         btnAC.addActionListener(new ActionListener() {
  77.             public void actionPerformed(ActionEvent e) {
  78.                 textR.setText(null);
  79.             }
  80.         });
  81.        
  82.         btnAC.setAlignmentX(Component.CENTER_ALIGNMENT);
  83.         btnAC.setBorder(null);
  84.         btnAC.setFont(new Font("Tahoma", Font.PLAIN, 20));
  85.         btnAC.setForeground(Color.BLACK);
  86.         btnAC.setBounds(15, 85, 158, 70);
  87.         frame.getContentPane().add(btnAC);
  88.        
  89.         JButton btnOps = new JButton("+/-");
  90.         btnOps.addActionListener(new ActionListener() {
  91.             public void actionPerformed(ActionEvent e) {
  92.                 double ops =Double.parseDouble(String.valueOf(textR.getText()));
  93.                 ops = ops*(-1);
  94.                 textR.setText(String.valueOf(ops));
  95.                
  96.             }
  97.         });
  98.         btnOps.setFont(new Font("Tahoma", Font.PLAIN, 20));
  99.         btnOps.setBounds(185, 88, 70, 70);
  100.         frame.getContentPane().add(btnOps);
  101.        
  102.         JButton btnD = new JButton("/");
  103.        
  104.         btnD.addActionListener(new ActionListener() {
  105.             public void actionPerformed(ActionEvent e) {
  106.                 n1 = Double.parseDouble(textR.getText());
  107.                 textR.setText("");
  108.                 Op="/";
  109.                        
  110.             }
  111.         });
  112.         btnD.setFont(new Font("Tahoma", Font.PLAIN, 20));
  113.         btnD.setBounds(270, 85, 70, 70);
  114.         frame.getContentPane().add(btnD);
  115.        
  116.         //-----------ROW 2------------
  117.         JButton btn7 = new JButton("7");
  118.         btn7.addActionListener(new ActionListener() {
  119.             public void actionPerformed(ActionEvent e) {
  120.                 String EnterNumber = textR.getText() + btn7.getText();
  121.                 textR.setText(EnterNumber);
  122.             }
  123.         });
  124.         btn7.setFont(new Font("Tahoma", Font.PLAIN, 20));
  125.         btn7.setBounds(15, 170, 70, 70);
  126.         frame.getContentPane().add(btn7);
  127.        
  128.         JButton btn8 = new JButton("8");
  129.         btn8.addActionListener(new ActionListener() {
  130.             public void actionPerformed(ActionEvent e) {
  131.                 String EnterNumber = textR.getText() + btn8.getText();
  132.                 textR.setText(EnterNumber);
  133.             }
  134.         });
  135.         btn8.setFont(new Font("Tahoma", Font.PLAIN, 20));
  136.         btn8.setBounds(100, 170, 70, 70);
  137.         frame.getContentPane().add(btn8);
  138.        
  139.         JButton btn9 = new JButton("9");
  140.         btn9.addActionListener(new ActionListener() {
  141.             public void actionPerformed(ActionEvent e) {
  142.                 String EnterNumber = textR.getText() + btn9.getText();
  143.                 textR.setText(EnterNumber);
  144.             }
  145.         });
  146.         btn9.setFont(new Font("Tahoma", Font.PLAIN, 20));
  147.         btn9.setBounds(185, 170, 70, 70);
  148.         frame.getContentPane().add(btn9);
  149.        
  150.         JButton btnMulti = new JButton("*");
  151.        
  152.         btnMulti.addActionListener(new ActionListener() {
  153.             public void actionPerformed(ActionEvent e) {
  154.                 n1 = Double.parseDouble(textR.getText());
  155.                 textR.setText("");
  156.                 Op="*";
  157.                        
  158.             }
  159.         });
  160.         btnMulti.setFont(new Font("Tahoma", Font.PLAIN, 20));
  161.         btnMulti.setBounds(270, 170, 70, 70);
  162.         frame.getContentPane().add(btnMulti);
  163.        
  164.         //-----------ROW 3------------
  165.         JButton btn4 = new JButton("4");
  166.         btn4.addActionListener(new ActionListener() {
  167.             public void actionPerformed(ActionEvent e) {
  168.                 String EnterNumber = textR.getText() + btn4.getText();
  169.                 textR.setText(EnterNumber);
  170.             }
  171.         });
  172.         btn4.setFont(new Font("Tahoma", Font.PLAIN, 20));
  173.         btn4.setBounds(15, 255, 70, 70);
  174.         frame.getContentPane().add(btn4);
  175.        
  176.         JButton btn5 = new JButton("5");
  177.         btn5.addActionListener(new ActionListener() {
  178.             public void actionPerformed(ActionEvent e) {
  179.                 String EnterNumber = textR.getText() + btn5.getText();
  180.                 textR.setText(EnterNumber);
  181.             }
  182.         });
  183.         btn5.setFont(new Font("Tahoma", Font.PLAIN, 20));
  184.         btn5.setBounds(100, 255, 70, 70);
  185.         frame.getContentPane().add(btn5);
  186.        
  187.         JButton btn6 = new JButton("6");
  188.         btn6.addActionListener(new ActionListener() {
  189.             public void actionPerformed(ActionEvent e) {
  190.                 String EnterNumber = textR.getText() + btn6.getText();
  191.                 textR.setText(EnterNumber);
  192.             }
  193.         });
  194.         btn6.setFont(new Font("Tahoma", Font.PLAIN, 20));
  195.         btn6.setBounds(185, 255, 70, 70);
  196.         frame.getContentPane().add(btn6);
  197.        
  198.         JButton btnSub = new JButton("-");
  199.        
  200.         btnSub.addActionListener(new ActionListener() {
  201.             public void actionPerformed(ActionEvent e) {
  202.                 n1 = Double.parseDouble(textR.getText());
  203.                 textR.setText("");
  204.                 Op="-";
  205.                        
  206.             }
  207.         });
  208.         btnSub.setFont(new Font("Tahoma", Font.PLAIN, 25));
  209.         btnSub.setBounds(270, 255, 70, 70);
  210.         frame.getContentPane().add(btnSub);
  211.        
  212.        
  213.         //-----------ROW 4------------
  214.         JButton btn1 = new JButton("1");
  215.         btn1.addActionListener(new ActionListener() {
  216.             public void actionPerformed(ActionEvent e) {
  217.                 String EnterNumber = textR.getText() + btn1.getText();
  218.                 textR.setText(EnterNumber);
  219.             }
  220.         });
  221.         btn1.setFont(new Font("Tahoma", Font.PLAIN, 20));
  222.         btn1.setBounds(15, 340, 70, 70);
  223.         frame.getContentPane().add(btn1);
  224.        
  225.         JButton btn2 = new JButton("2");
  226.         btn2.addActionListener(new ActionListener() {
  227.             public void actionPerformed(ActionEvent e) {
  228.                 String EnterNumber = textR.getText() + btn2.getText();
  229.                 textR.setText(EnterNumber);
  230.             }
  231.         });
  232.         btn2.setFont(new Font("Tahoma", Font.PLAIN, 20));
  233.         btn2.setBounds(100, 340, 70, 70);
  234.         frame.getContentPane().add(btn2);
  235.        
  236.         JButton btn3 = new JButton("3");
  237.         btn3.addActionListener(new ActionListener() {
  238.             public void actionPerformed(ActionEvent e) {
  239.                 String EnterNumber = textR.getText() + btn3.getText();
  240.                 textR.setText(EnterNumber);
  241.             }
  242.         });
  243.         btn3.setFont(new Font("Tahoma", Font.PLAIN, 20));
  244.         btn3.setBounds(185, 340, 70, 70);
  245.         frame.getContentPane().add(btn3);
  246.        
  247.         JButton btnAdd = new JButton("+");
  248.        
  249.         btnAdd.addActionListener(new ActionListener() {
  250.             public void actionPerformed(ActionEvent e) {
  251.                 n1 = Double.parseDouble(textR.getText());
  252.                 textR.setText("");
  253.                 Op="+";
  254.                        
  255.                
  256.             }
  257.         });
  258.         btnAdd.setFont(new Font("Tahoma", Font.PLAIN, 20));
  259.         btnAdd.setBounds(270, 340, 70, 70);
  260.         frame.getContentPane().add(btnAdd);
  261.        
  262.        
  263.         //-----------ROW 5------------
  264.         JButton btn0 = new JButton("0");
  265.         btn0.addActionListener(new ActionListener() {
  266.             public void actionPerformed(ActionEvent e) {
  267.                 String EnterNumber = textR.getText() + btn0.getText();
  268.                 textR.setText(EnterNumber);
  269.             }
  270.            
  271.         });
  272.         btn0.setFont(new Font("Tahoma", Font.PLAIN, 20));
  273.         btn0.setBounds(15, 425, 155, 70);
  274.         frame.getContentPane().add(btn0);
  275.        
  276.    
  277.        
  278.         JButton btnP = new JButton(".");
  279.         btnP.addActionListener(new ActionListener() {
  280.             public void actionPerformed(ActionEvent e) {
  281.                 if(! textR.getText().contains("."))
  282.                   {
  283.                   textR.setText(textR.getText() + btnP.getText());
  284.                   }
  285.             }
  286.            
  287.         });
  288.         btnP.setFont(new Font("Tahoma", Font.BOLD, 30));
  289.         btnP.setBounds(185, 425, 70, 70);
  290.         frame.getContentPane().add(btnP);
  291.        
  292.         JButton btnE = new JButton("=");
  293.    
  294.         btnE.addActionListener(new ActionListener() {
  295.             public void actionPerformed(ActionEvent e) {
  296.                 n2= Double.parseDouble(textR.getText());
  297.                 if(Op == "+")
  298.                 {
  299.                     result = n1+n2;
  300.                     Ans = String.format("%.2f", result);
  301.                     textR.setText(Ans);
  302.                    
  303.                 }
  304.                 if(Op == "-")
  305.                 {
  306.                     result = n1-n2;
  307.                     Ans = String.format("%.2f", result);
  308.                     textR.setText(Ans);
  309.                    
  310.                 }
  311.                 if(Op == "*")
  312.                 {
  313.                     result = n1*n2;
  314.                     Ans = String.format("%.2f", result);
  315.                     textR.setText(Ans);
  316.                    
  317.                 }
  318.                 if(Op == "/")
  319.                 {
  320.                     result = n1/n2;
  321.                     Ans = String.format("%.4f", result);
  322.                     textR.setText(Ans);
  323.                    
  324.                 }
  325.                
  326.                
  327.             }
  328.         });
  329.         btnE.setForeground(Color.BLACK);
  330.         btnE.setFont(new Font("Tahoma", Font.PLAIN, 20));
  331.         btnE.setBounds(270, 425, 70, 70);
  332.         frame.getContentPane().add(btnE);
  333.     }
  334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement