Advertisement
nurnobishanto73

SimpleCalculator

Oct 11th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.98 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.BLACK);
  60.         frame.getContentPane().setBackground(Color.BLACK);
  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("AC");
  76.         btnAC.addActionListener(new ActionListener() {
  77.             public void actionPerformed(ActionEvent e) {
  78.                 textR.setText(null);
  79.             }
  80.         });
  81.         btnAC.setBackground(new Color(255, 0, 0));
  82.         btnAC.setAlignmentX(Component.CENTER_ALIGNMENT);
  83.         btnAC.setBorder(null);
  84.         btnAC.setFont(new Font("Tahoma", Font.PLAIN, 20));
  85.         btnAC.setForeground(new Color(255, 255, 255));
  86.         btnAC.setBounds(15, 85, 70, 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(100, 85, 70, 70);
  100.         frame.getContentPane().add(btnOps);
  101.        
  102.         JButton btnD = new JButton("/");
  103.         btnD.setBackground(Color.ORANGE);
  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.         JButton btnMod = new JButton("%");
  117.         btnMod.addActionListener(new ActionListener() {
  118.             public void actionPerformed(ActionEvent e) {
  119.                 n1 = Double.parseDouble(textR.getText());
  120.                 textR.setText("");
  121.                 Op="%";
  122.                        
  123.             }
  124.         });
  125.         btnMod.setFont(new Font("Tahoma", Font.PLAIN, 20));
  126.         btnMod.setBounds(185, 85, 70, 70);
  127.         frame.getContentPane().add(btnMod);
  128.        
  129.         //-----------ROW 2------------
  130.         JButton btn7 = new JButton("7");
  131.         btn7.addActionListener(new ActionListener() {
  132.             public void actionPerformed(ActionEvent e) {
  133.                 String EnterNumber = textR.getText() + btn7.getText();
  134.                 textR.setText(EnterNumber);
  135.             }
  136.         });
  137.         btn7.setFont(new Font("Tahoma", Font.PLAIN, 20));
  138.         btn7.setBounds(15, 170, 70, 70);
  139.         frame.getContentPane().add(btn7);
  140.        
  141.         JButton btn8 = new JButton("8");
  142.         btn8.addActionListener(new ActionListener() {
  143.             public void actionPerformed(ActionEvent e) {
  144.                 String EnterNumber = textR.getText() + btn8.getText();
  145.                 textR.setText(EnterNumber);
  146.             }
  147.         });
  148.         btn8.setFont(new Font("Tahoma", Font.PLAIN, 20));
  149.         btn8.setBounds(100, 170, 70, 70);
  150.         frame.getContentPane().add(btn8);
  151.        
  152.         JButton btn9 = new JButton("9");
  153.         btn9.addActionListener(new ActionListener() {
  154.             public void actionPerformed(ActionEvent e) {
  155.                 String EnterNumber = textR.getText() + btn9.getText();
  156.                 textR.setText(EnterNumber);
  157.             }
  158.         });
  159.         btn9.setFont(new Font("Tahoma", Font.PLAIN, 20));
  160.         btn9.setBounds(185, 170, 70, 70);
  161.         frame.getContentPane().add(btn9);
  162.        
  163.         JButton btnMulti = new JButton("*");
  164.         btnMulti.setBackground(Color.ORANGE);
  165.         btnMulti.addActionListener(new ActionListener() {
  166.             public void actionPerformed(ActionEvent e) {
  167.                 n1 = Double.parseDouble(textR.getText());
  168.                 textR.setText("");
  169.                 Op="*";
  170.                        
  171.             }
  172.         });
  173.         btnMulti.setFont(new Font("Tahoma", Font.PLAIN, 20));
  174.         btnMulti.setBounds(270, 170, 70, 70);
  175.         frame.getContentPane().add(btnMulti);
  176.        
  177.         //-----------ROW 3------------
  178.         JButton btn4 = new JButton("4");
  179.         btn4.addActionListener(new ActionListener() {
  180.             public void actionPerformed(ActionEvent e) {
  181.                 String EnterNumber = textR.getText() + btn4.getText();
  182.                 textR.setText(EnterNumber);
  183.             }
  184.         });
  185.         btn4.setFont(new Font("Tahoma", Font.PLAIN, 20));
  186.         btn4.setBounds(15, 255, 70, 70);
  187.         frame.getContentPane().add(btn4);
  188.        
  189.         JButton btn5 = new JButton("5");
  190.         btn5.addActionListener(new ActionListener() {
  191.             public void actionPerformed(ActionEvent e) {
  192.                 String EnterNumber = textR.getText() + btn5.getText();
  193.                 textR.setText(EnterNumber);
  194.             }
  195.         });
  196.         btn5.setFont(new Font("Tahoma", Font.PLAIN, 20));
  197.         btn5.setBounds(100, 255, 70, 70);
  198.         frame.getContentPane().add(btn5);
  199.        
  200.         JButton btn6 = new JButton("6");
  201.         btn6.addActionListener(new ActionListener() {
  202.             public void actionPerformed(ActionEvent e) {
  203.                 String EnterNumber = textR.getText() + btn6.getText();
  204.                 textR.setText(EnterNumber);
  205.             }
  206.         });
  207.         btn6.setFont(new Font("Tahoma", Font.PLAIN, 20));
  208.         btn6.setBounds(185, 255, 70, 70);
  209.         frame.getContentPane().add(btn6);
  210.        
  211.         JButton btnSub = new JButton("-");
  212.         btnSub.setBackground(Color.ORANGE);
  213.         btnSub.addActionListener(new ActionListener() {
  214.             public void actionPerformed(ActionEvent e) {
  215.                 n1 = Double.parseDouble(textR.getText());
  216.                 textR.setText("");
  217.                 Op="-";
  218.                        
  219.             }
  220.         });
  221.         btnSub.setFont(new Font("Tahoma", Font.PLAIN, 25));
  222.         btnSub.setBounds(270, 255, 70, 70);
  223.         frame.getContentPane().add(btnSub);
  224.        
  225.        
  226.         //-----------ROW 4------------
  227.         JButton btn1 = new JButton("1");
  228.         btn1.addActionListener(new ActionListener() {
  229.             public void actionPerformed(ActionEvent e) {
  230.                 String EnterNumber = textR.getText() + btn1.getText();
  231.                 textR.setText(EnterNumber);
  232.             }
  233.         });
  234.         btn1.setFont(new Font("Tahoma", Font.PLAIN, 20));
  235.         btn1.setBounds(15, 340, 70, 70);
  236.         frame.getContentPane().add(btn1);
  237.        
  238.         JButton btn2 = new JButton("2");
  239.         btn2.addActionListener(new ActionListener() {
  240.             public void actionPerformed(ActionEvent e) {
  241.                 String EnterNumber = textR.getText() + btn2.getText();
  242.                 textR.setText(EnterNumber);
  243.             }
  244.         });
  245.         btn2.setFont(new Font("Tahoma", Font.PLAIN, 20));
  246.         btn2.setBounds(100, 340, 70, 70);
  247.         frame.getContentPane().add(btn2);
  248.        
  249.         JButton btn3 = new JButton("3");
  250.         btn3.addActionListener(new ActionListener() {
  251.             public void actionPerformed(ActionEvent e) {
  252.                 String EnterNumber = textR.getText() + btn3.getText();
  253.                 textR.setText(EnterNumber);
  254.             }
  255.         });
  256.         btn3.setFont(new Font("Tahoma", Font.PLAIN, 20));
  257.         btn3.setBounds(185, 340, 70, 70);
  258.         frame.getContentPane().add(btn3);
  259.        
  260.         JButton btnAdd = new JButton("+");
  261.         btnAdd.setBackground(Color.ORANGE);
  262.         btnAdd.addActionListener(new ActionListener() {
  263.             public void actionPerformed(ActionEvent e) {
  264.                 n1 = Double.parseDouble(textR.getText());
  265.                 textR.setText("");
  266.                 Op="+";
  267.                        
  268.                
  269.             }
  270.         });
  271.         btnAdd.setFont(new Font("Tahoma", Font.PLAIN, 20));
  272.         btnAdd.setBounds(270, 340, 70, 70);
  273.         frame.getContentPane().add(btnAdd);
  274.        
  275.        
  276.         //-----------ROW 5------------
  277.         JButton btn0 = new JButton("0");
  278.         btn0.addActionListener(new ActionListener() {
  279.             public void actionPerformed(ActionEvent e) {
  280.                 String EnterNumber = textR.getText() + btn0.getText();
  281.                 textR.setText(EnterNumber);
  282.             }
  283.            
  284.         });
  285.         btn0.setFont(new Font("Tahoma", Font.PLAIN, 20));
  286.         btn0.setBounds(15, 425, 155, 70);
  287.         frame.getContentPane().add(btn0);
  288.        
  289.    
  290.        
  291.         JButton btnP = new JButton(".");
  292.         btnP.addActionListener(new ActionListener() {
  293.             public void actionPerformed(ActionEvent e) {
  294.                 if(! textR.getText().contains("."))
  295.                   {
  296.                   textR.setText(textR.getText() + btnP.getText());
  297.                   }
  298.             }
  299.            
  300.         });
  301.         btnP.setFont(new Font("Tahoma", Font.BOLD, 30));
  302.         btnP.setBounds(185, 425, 70, 70);
  303.         frame.getContentPane().add(btnP);
  304.        
  305.         JButton btnE = new JButton("=");
  306.         btnE.setBackground(Color.ORANGE);
  307.         btnE.addActionListener(new ActionListener() {
  308.             public void actionPerformed(ActionEvent e) {
  309.                 n2= Double.parseDouble(textR.getText());
  310.                 if(Op == "+")
  311.                 {
  312.                     result = n1+n2;
  313.                     Ans = String.format("%.2f", result);
  314.                     textR.setText(Ans);
  315.                    
  316.                 }
  317.                 if(Op == "-")
  318.                 {
  319.                     result = n1-n2;
  320.                     Ans = String.format("%.2f", result);
  321.                     textR.setText(Ans);
  322.                    
  323.                 }
  324.                 if(Op == "*")
  325.                 {
  326.                     result = n1*n2;
  327.                     Ans = String.format("%.2f", result);
  328.                     textR.setText(Ans);
  329.                    
  330.                 }
  331.                 if(Op == "/")
  332.                 {
  333.                     result = n1/n2;
  334.                     Ans = String.format("%.4f", result);
  335.                     textR.setText(Ans);
  336.                    
  337.                 }
  338.                 if(Op == "%")
  339.                 {
  340.                     result = n1%n2;
  341.                     Ans = String.format("%.0f", result);
  342.                     textR.setText(Ans);
  343.                    
  344.                 }
  345.                
  346.             }
  347.         });
  348.         btnE.setForeground(Color.BLACK);
  349.         btnE.setFont(new Font("Tahoma", Font.PLAIN, 20));
  350.         btnE.setBounds(270, 425, 70, 70);
  351.         frame.getContentPane().add(btnE);
  352.     }
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement