This is for a high school class I'm taking. Any tips/improvements?
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class JavaCalculator
{
public static boolean isMemorized;
public static String memory;
public static void main (String[] args)
{
JTextField answerField = new JTextField();
answerField.setBounds (6, 6, 280, 48);
answerField.setFont (new Font ("Arial", Font.PLAIN, 26));
answerField.setColumns (1);
answerField.setHorizontalAlignment (JTextField.RIGHT);
answerField.setEditable (false);
JButton button_one = new JButton ("1");
button_one.setBounds (6, 92, 70, 38);
button_one.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
answerField.setText (answerField.getText() + "1");
}
});
JButton button_two = new JButton ("2");
button_two.setBounds (76, 92, 70, 38);
button_two.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
answerField.setText (answerField.getText() + "2");
}
});
JButton button_three = new JButton ("3");
button_three.setBounds (146, 92, 70, 38);
button_three.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
answerField.setText (answerField.getText() + "3");
}
});
JButton button_four = new JButton ("4");
button_four.setBounds (6, 128, 70, 38);
button_four.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
answerField.setText (answerField.getText() + "4");
}
});
JButton button_five = new JButton ("5");
button_five.setBounds (76, 128, 70, 38);
button_five.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
answerField.setText (answerField.getText() + "5");
}
});
JButton button_six = new JButton ("6");
button_six.setBounds (146, 128, 70, 38);
button_six.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
answerField.setText (answerField.getText() + "6");
}
});
JButton button_seven = new JButton ("7");
button_seven.setBounds (6, 164, 70, 38);
button_seven.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
answerField.setText (answerField.getText() + "7");
}
});
JButton button_eight = new JButton ("8");
button_eight.setBounds (76, 164, 70, 38);
button_eight.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
answerField.setText (answerField.getText() + "8");
}
});
JButton button_nine = new JButton ("9");
button_nine.setBounds (146, 164, 70, 38);
button_nine.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
answerField.setText (answerField.getText() + "9");
}
});
JButton button_zero = new JButton ("0");
button_zero.setBounds (6, 200, 140, 50);
button_zero.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
answerField.setText (answerField.getText() + "0");
}
});
JButton button_period = new JButton (".");
button_period.setBounds (146, 200, 70, 50);
button_period.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
answerField.setText (answerField.getText() + ".");
}
});
JButton button_plus = new JButton ("+");
button_plus.setBounds (216, 128, 70, 38);
button_plus.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
answerField.setText (answerField.getText() + " + ");
}
});
JButton button_minus = new JButton ("-");
button_minus.setBounds (216, 92, 70, 38);
button_minus.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
answerField.setText (answerField.getText() + " - ");
}
});
JButton button_times = new JButton ("*");
button_times.setBounds (146, 56, 70, 38);
button_times.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
answerField.setText (answerField.getText() + " * ");
}
});
JButton button_divide = new JButton ("/");
button_divide.setBounds(216, 56, 70, 38);
button_divide.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
answerField.setText (answerField.getText() + " / ");
}
});
JButton button_equals = new JButton ("=");
button_equals.setBounds (216, 164, 70, 88);
button_equals.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName ("JavaScript");
try
{
answerField.setText (answerField.getText() + " = " + engine.eval (answerField.getText()));
}
catch (ScriptException exception)
{
exception.printStackTrace();
}
}
});
JButton button_memory = new JButton ("M");
button_memory.setBounds (76, 56, 70, 38);
button_memory.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
if (!isMemorized)
{
memory = answerField.getText();
button_memory.setText ("R");
isMemorized = true;
}
else if (isMemorized)
{
answerField.setText (answerField.getText() + memory);
}
}
});
JButton button_clear = new JButton ("C");
button_clear.setBounds (6, 56, 70, 38);
button_clear.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
answerField.setText ("");
button_memory.setText ("M");
isMemorized = false;
}
});
JPanel panel = new JPanel();
panel.setLayout (null);
panel.add (answerField);
panel.add (button_one);
panel.add (button_two);
panel.add (button_three);
panel.add (button_four);
panel.add (button_five);
panel.add (button_six);
panel.add (button_seven);
panel.add (button_eight);
panel.add (button_nine);
panel.add (button_zero);
panel.add (button_period);
panel.add (button_plus);
panel.add (button_minus);
panel.add (button_times);
panel.add (button_divide);
panel.add (button_equals);
panel.add (button_memory);
panel.add (button_clear);
JFrame frame = new JFrame ("Java Calculator");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setSize (300, 278);
frame.setResizable (false);
frame.getContentPane().add (panel);
frame.setVisible (true);
}
}