Create a graphical user interface (GUI) program that will perform basic standard
calculator operations. The students will not be penalized for having extra arithmetic operations
in their program.
package calculator;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Calculator extends JFrame implements ActionListener {
static JFrame frame1;
static JTextField field1;
String str0, str1, str2;
Calculator()
{
str0 = str1 = str2 = "";
}
public static void main(String args[])
{
frame1 = new JFrame("Calculator");
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
System.err.println(e.getMessage());
}
Calculator cal = new Calculator();
field1 = new JTextField(16);
field1.setEditable(false);
JButton button0, button1, button2, button3, button4;
JButton button5, button6, button7, button8, button9, buttona, buttons, buttond, buttonm;
JButton buttone, buttoneq, buttoneq1;
button0 = new JButton("0");
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
button7 = new JButton("7");
button8 = new JButton("8");
button9 = new JButton("9");
buttoneq1 = new JButton("=");
buttona = new JButton("+");
buttons = new JButton("-");
buttond = new JButton("/");
buttonm = new JButton("*");
buttoneq = new JButton("C");
buttone = new JButton(".");
JPanel panel = new JPanel();
// addding action listeners for each button
buttonm.addActionListener(cal);
buttond.addActionListener(cal);
buttons.addActionListener(cal);
buttona.addActionListener(cal);
button9.addActionListener(cal);
button8.addActionListener(cal);
button7.addActionListener(cal);
button6.addActionListener(cal);
button5.addActionListener(cal);
button4.addActionListener(cal);
button3.addActionListener(cal);
button2.addActionListener(cal);
button1.addActionListener(cal);
button0.addActionListener(cal);
buttone.addActionListener(cal);
buttoneq.addActionListener(cal);
buttoneq1.addActionListener(cal);
// adding the items to the panel
panel.add(field1);
panel.add(buttona);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(buttons);
panel.add(button4);
panel.add(button5);
panel.add(button6);
panel.add(buttonm);
panel.add(button7);
panel.add(button8);
panel.add(button9);
panel.add(buttond);
panel.add(buttone);
panel.add(button0);
panel.add(buttoneq);
panel.add(buttoneq1);
panel.setBackground(Color.cyan);
frame1.add(panel);
frame1.setSize(200, 220);
frame1.show();
}
public void actionPerformed(ActionEvent event)
{
String string = event.getActionCommand();
if ((string.charAt(0) >= '0' && string.charAt(0) <= '9') || string.charAt(0) == '.') {
if (!str1.equals(""))
str2 = str2 + string;
else
str0 = str0 + string;
field1.setText(str0 + str1 + str2);
}
else if (string.charAt(0) == 'C') {
str0 = str1 = str2 = "";
field1.setText(str0 + str1 + str2);
}
else if (string.charAt(0) == '=') {
double tem;
if (str1.equals("+"))
tem = (Double.parseDouble(str0) + Double.parseDouble(str2));
else if (str1.equals("-"))
tem = (Double.parseDouble(str0) - Double.parseDouble(str2));
else if (str1.equals("/"))
tem = (Double.parseDouble(str0) / Double.parseDouble(str2));
else
tem = (Double.parseDouble(str0) * Double.parseDouble(str2));
field1.setText(str0 + str1 +str2 + "=" + tem);
str0 = Double.toString(tem);
str1 = str2 = "";
}
else {
if (str1.equals("") || str2.equals(""))
str1 = string;
else {
double temp;
if (str1.equals("+"))
temp = (Double.parseDouble(str0) + Double.parseDouble(str2));
else if (str1.equals("-"))
temp = (Double.parseDouble(str0) - Double.parseDouble(str2));
else if (str1.equals("/"))
temp = (Double.parseDouble(str0) / Double.parseDouble(str2));
else
temp = (Double.parseDouble(str0) * Double.parseDouble(str2));
str0 = Double.toString(temp);
str1 = string;
str2 = "";
}
field1.setText(str0 + str1 + str2);
}
}
}
Comments
Leave a comment