Back to Examples
package edu.iuk.Calculator;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//Use the eval() function from JavaScript
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
i mport javax.script.ScriptException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class CalculatorFrame extends JFrame {
private final JButton keys[];
private final JPanel keyPadJPanel;
private final JTextField lcdJTextField;
// constructor sets up GUI
public CalculatorFrame() {
lcdJTextField = new JTextField(20); // create lcdJTextField
lcdJTextField.setEditable(true); // allow user input
keys = new JButton[20]; // array keys contains 16 JButtons
// initialize all digit key buttons
for (int i = 0; i <= 9; i++){
keys[i] = new JButton(String.valueOf(i));
}
// initialize all function key buttons
keys[10] = new JButton("/");
keys[11] = new JButton("*");
keys[12] = new JButton("-");
keys[13] = new JButton("+");
keys[14] = new JButton("=");
keys[15] = new JButton("C");
// set keyPadJPanel layout to grid layout
keyPadJPanel = new JPanel();
keyPadJPanel.setLayout(new GridLayout(4, 4));
// add buttons to keyPadJPanel panel
// 7, 8, 9, divide
for (int i = 7; i <= 10; i++){
keyPadJPanel.add(keys[i]);
}
// 4, 5, 6
for (int i = 4; i <= 6; i++){
keyPadJPanel.add(keys[i]);
}
// multiply
keyPadJPanel.add(keys[11]);
// 1, 2, 3
for (int i = 1; i <= 3; i++){
keyPadJPanel.add(keys[i]);
}
// subtract
keyPadJPanel.add(keys[12]);
// 0, C, =, +
keyPadJPanel.add(keys[0]);
keyPadJPanel.add(keys[15]);
keyPadJPanel.add(keys[14]);
keyPadJPanel.add(keys[13]);
// Write code that adds components to (default) border layout
// ...............
add("North", lcdJTextField);
add("Center", keyPadJPanel);
// Code Create button actions ..................
ClickAction handler = new ClickAction();
for (int i = 0; i <= 15; i++) {
keys[i].addActionListener(handler);
}
} // end CalculatorFrame constructor
// **************************************************************************************
// An action listener 'inner class' that creates event handlers for button
// clicks:
private class ClickAction implements ActionListener {
String textFieldInput = "";
@Override
public void actionPerformed(ActionEvent event) {
if (event.getSource() == keys[0]) {
textFieldInput += "0";
} else if (event.getSource() == keys[1]) {
textFieldInput += "1";
} else if (event.getSource() == keys[2]) {
textFieldInput += "2";
} else if (event.getSource() == keys[3]) {
textFieldInput += "3";
} else if (event.getSource() == keys[4]) {
textFieldInput += "4";
} else if (event.getSource() == keys[5]) {
textFieldInput += "5";
} else if (event.getSource() == keys[6]) {
textFieldInput += "6";
} else if (event.getSource() == keys[7]) {
textFieldInput += "7";
} else if (event.getSource() == keys[8]) {
textFieldInput += "8";
} else if (event.getSource() == keys[9]) {
textFieldInput += "9";
} else if (event.getSource() == keys[10]) {
textFieldInput += "/";
} else if (event.getSource() == keys[11]) {
textFieldInput += "*";
} else if (event.getSource() == keys[12]) {
textFieldInput += "-";
} else if (event.getSource() == keys[13]) {
textFieldInput += "+";
}
// This is the equals button
else if (event.getSource() == keys[14]) {
// create a script engine manager
ScriptEngineManager factory = new ScriptEngineManager();
// create a JavaScript engine
ScriptEngine engine = factory.getEngineByName("JavaScript");
// evaluate JavaScript code from String
try {
Object Text = engine.eval(textFieldInput);
textFieldInput = String.valueOf(Text);
} catch (ScriptException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// This is the clear button
else if (event.getSource() == keys[15]) {
textFieldInput = "";
}
lcdJTextField.setText(textFieldInput);
}// end actionPerformed
}// end ClickAction
} // end class CalculatorFrame
Back to Examples