Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package main;
- /**
- *
- * @author markjasongalang
- */
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- class Calculator implements ActionListener {
- JFrame frame;
- JTextField textfield;
- JButton[] number_buttons = new JButton[10];
- JButton[] function_buttons = new JButton[8];
- JButton add_button, sub_button, mul_button, div_button;
- JButton dec_button, equ_button, del_button, clr_button;
- JPanel panel;
- Font my_front = new Font("Arial", Font.BOLD, 30);
- double num1 = 0;
- double num2 = 0;
- double result = 0;
- char operator;
- Calculator() {
- frame = new JFrame("Calculator");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(420, 550);
- frame.setLayout(null);
- textfield = new JTextField();
- textfield.setBounds(50, 25, 300, 50);
- textfield.setFont(my_front);
- textfield.setEditable(false);
- add_button = new JButton("+");
- sub_button = new JButton("-");
- mul_button = new JButton("*");
- div_button = new JButton("/");
- dec_button = new JButton(".");
- equ_button = new JButton("=");
- del_button = new JButton("Delete");
- clr_button = new JButton("Clear");
- function_buttons[0] = add_button;
- function_buttons[1] = sub_button;
- function_buttons[2] = mul_button;
- function_buttons[3] = div_button;
- function_buttons[4] = dec_button;
- function_buttons[5] = equ_button;
- function_buttons[6] = del_button;
- function_buttons[7] = clr_button;
- for (int i = 0; i < 8; i++) {
- function_buttons[i].addActionListener(this);
- function_buttons[i].setFont(my_front);
- function_buttons[i].setFocusable(false);
- }
- for (int i = 0; i < 10; i++) {
- number_buttons[i] = new JButton(String.valueOf(i));
- number_buttons[i].addActionListener(this);
- number_buttons[i].setFont(my_front);
- number_buttons[i].setFocusable(false);
- }
- del_button.setBounds(50, 430, 145, 50);
- clr_button.setBounds(205, 430, 145, 50);
- panel = new JPanel();
- panel.setBounds(50, 100, 300, 300);
- panel.setLayout(new GridLayout(4, 4, 10, 10));
- panel.add(number_buttons[1]);
- panel.add(number_buttons[2]);
- panel.add(number_buttons[3]);
- panel.add(add_button);
- panel.add(number_buttons[4]);
- panel.add(number_buttons[5]);
- panel.add(number_buttons[6]);
- panel.add(sub_button);
- panel.add(number_buttons[7]);
- panel.add(number_buttons[8]);
- panel.add(number_buttons[9]);
- panel.add(mul_button);
- panel.add(dec_button);
- panel.add(number_buttons[0]);
- panel.add(equ_button);
- panel.add(div_button);
- frame.add(panel);
- frame.add(del_button);
- frame.add(clr_button);
- frame.add(textfield);
- frame.setVisible(true);
- }
- public void actionPerformed(ActionEvent e) {
- }
- }
- public class Main {
- public static void main(String[] args) {
- Calculator calc = new Calculator();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement