import javax.swing.JFrame;
class calculator{
public static void main(String[] args){
//Created Object for Window Class
window wo = new window();
//Allow Program to Close on Exit
wo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Program Window Settings
wo.setSize(600,600);
wo.setVisible(true);
}
}
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.awt.*;
public class window extends JFrame{
private JButton add, sub, mult, div, sqrt, expo, quad, comp;
public window(){
super("Calculator");
setLayout(new FlowLayout());
//Addition Button
add = new JButton("+");
add(add, BorderLayout.CENTER);
//Subtraction Button
sub = new JButton("-");
add(sub, BorderLayout.CENTER);
//Multiplication Button
add = new JButton("x");
add(mult, BorderLayout.CENTER);
//Division Button
add = new JButton("/");
add(div, BorderLayout.CENTER);
//Square Root Button
add = new JButton("Square Root");
add(sqrt, BorderLayout.CENTER);
//Exponential Button
add = new JButton("Exponential");
add(expo, BorderLayout.CENTER);
//Quadratic Button
add = new JButton("Quadratic");
add(quad, BorderLayout.CENTER);
//Compound Interest Button
add = new JButton("Compound Interest");
add(comp, BorderLayout.CENTER);
//Handler Class
HandlerClass handler = new HandlerClass();
add.addActionListener(handler);
sub.addActionListener(handler);
mult.addActionListener(handler);
div.addActionListener(handler);
sqrt.addActionListener(handler);
expo.addActionListener(handler);
quad.addActionListener(handler);
comp.addActionListener(handler);
}
private class HandlerClass implements ActionListener{
//actionPerformed is the only method to override
public void actionPerformed(ActionEvent event){
JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand()));
}
}
}
This runs perfectly fine with only 2 buttons.. An error occurs once the buttons mult, div, sqrt, expo, quad, comp are added.