Advertisement
jasonpogi1669

Calculator GUI Java NetBeans

Mar 26th, 2021
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.71 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package main;
  7.  
  8. /**
  9.  *
  10.  * @author markjasongalang
  11.  */
  12. import java.awt.Color;
  13. import java.awt.Font;
  14. import java.awt.GridLayout;
  15. import java.awt.event.ActionEvent;
  16. import java.awt.event.ActionListener;
  17. import javax.swing.JButton;
  18. import javax.swing.JFrame;
  19. import javax.swing.JPanel;
  20. import javax.swing.JTextField;
  21.  
  22. class Calculator implements ActionListener {
  23.    
  24.     JFrame frame;
  25.     JTextField textfield;
  26.     JButton[] number_buttons = new JButton[10];
  27.     JButton[] function_buttons = new JButton[8];
  28.     JButton add_button, sub_button, mul_button, div_button;
  29.     JButton dec_button, equ_button, del_button, clr_button;
  30.     JPanel panel;
  31.    
  32.     Font my_front = new Font("Arial", Font.BOLD, 30);
  33.    
  34.     double num1 = 0;
  35.     double num2 = 0;
  36.     double result = 0;
  37.     char operator;
  38.    
  39.     Calculator() {
  40.         frame = new JFrame("Calculator");
  41.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  42.         frame.setSize(420, 550);
  43.         frame.setLayout(null);
  44.        
  45.         textfield = new JTextField();
  46.         textfield.setBounds(50, 25, 300, 50);
  47.         textfield.setFont(my_front);
  48.         textfield.setEditable(false);
  49.        
  50.         add_button = new JButton("+");
  51.         sub_button = new JButton("-");
  52.         mul_button = new JButton("*");
  53.         div_button = new JButton("/");
  54.         dec_button = new JButton(".");
  55.         equ_button = new JButton("=");
  56.         del_button = new JButton("Delete");
  57.         clr_button = new JButton("Clear");
  58.        
  59.         function_buttons[0] = add_button;
  60.         function_buttons[1] = sub_button;
  61.         function_buttons[2] = mul_button;
  62.         function_buttons[3] = div_button;
  63.         function_buttons[4] = dec_button;
  64.         function_buttons[5] = equ_button;
  65.         function_buttons[6] = del_button;
  66.         function_buttons[7] = clr_button;
  67.        
  68.         for (int i = 0; i < 8; i++) {
  69.             function_buttons[i].addActionListener(this);
  70.             function_buttons[i].setFont(my_front);
  71.             function_buttons[i].setFocusable(false);
  72.         }
  73.        
  74.         for (int i = 0; i < 10; i++) {
  75.             number_buttons[i] = new JButton(String.valueOf(i));
  76.             number_buttons[i].addActionListener(this);
  77.             number_buttons[i].setFont(my_front);
  78.             number_buttons[i].setFocusable(false);
  79.         }
  80.        
  81.         del_button.setBounds(50, 430, 145, 50);
  82.         clr_button.setBounds(205, 430, 145, 50);
  83.        
  84.         panel = new JPanel();
  85.         panel.setBounds(50, 100, 300, 300);
  86.         panel.setLayout(new GridLayout(4, 4, 10, 10));
  87.        
  88.         panel.add(number_buttons[1]);
  89.         panel.add(number_buttons[2]);
  90.         panel.add(number_buttons[3]);
  91.         panel.add(add_button);
  92.         panel.add(number_buttons[4]);
  93.         panel.add(number_buttons[5]);
  94.         panel.add(number_buttons[6]);
  95.         panel.add(sub_button);
  96.         panel.add(number_buttons[7]);
  97.         panel.add(number_buttons[8]);
  98.         panel.add(number_buttons[9]);
  99.         panel.add(mul_button);
  100.         panel.add(dec_button);
  101.         panel.add(number_buttons[0]);
  102.         panel.add(equ_button);
  103.         panel.add(div_button);
  104.        
  105.         frame.add(panel);
  106.         frame.add(del_button);
  107.         frame.add(clr_button);
  108.         frame.add(textfield);
  109.         frame.setVisible(true);
  110.     }
  111.    
  112.     public void actionPerformed(ActionEvent e) {
  113.        
  114.     }
  115. }
  116.  
  117. public class Main {
  118.     public static void main(String[] args) {
  119.         Calculator calc = new Calculator();
  120.     }
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement