package calculator; import java.awt.FlowLayout; import java.awt.event.ActionListener; import javax.swing.AbstractButton; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class CalcView extends JFrame { private static final long serialVersionUID = -8730447125113729547L; private JTextField userValueText; private JTextField calcText; private JButton sumButton; private JButton subtractButton; private JButton multiplyButton; private JButton divideButton; private JButton clearButton; public CalcView(CalcController controller) { super("Simple Calculator"); this.userValueText = new JTextField(5); this.calcText = new JTextField(20); this.calcText.setText("0"); this.calcText.setEditable(false); this.sumButton = new JButton("Add"); this.setCommand(this.sumButton, CalcController.SUM, controller); this.subtractButton = new JButton("Subtract"); this.setCommand(this.subtractButton, CalcController.SUBTRACT, controller); this.multiplyButton = new JButton("Multiply"); this.setCommand(this.multiplyButton, CalcController.MULTIPLY, controller); this.divideButton = new JButton("Divide"); this.setCommand(this.divideButton, CalcController.DIVIDE, controller); this.clearButton = new JButton("Clear"); this.setCommand(this.clearButton, CalcController.CLEAR, controller); this.getContentPane().setLayout(new FlowLayout()); this.add(new JLabel("Calculated Value")); this.add(this.calcText); this.add(new JLabel("Input")); this.add(this.userValueText); this.add(this.sumButton); this.add(this.subtractButton); this.add(this.multiplyButton); this.add(this.divideButton); this.add(this.clearButton); this.pack(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } /** * Get the string value of the user input text field. * * @return The string in the user input text field. */ public String getUserValue() { return this.userValueText.getText(); } /** * Set the string for the user input text field. * * @param value * The new value for the user input text field. * @pre. value is not null */ public void setUserValue(String value) { this.userValueText.setText(value); } /** * Set the string for the calculated value text field. * * @param value * The new value for the calculated value text field. * @pre. value is not null */ public void setCalcValue(String value) { this.calcText.setText(value); } /** * Sets the action listener and command for a button or menu item. * * @param button * A button or menu item. * @param commandName * The name of the command that the button invokes. * @param actionListener * The receiver of the action event. */ private void setCommand(AbstractButton button, String commandName, ActionListener actionListener) { button.addActionListener(actionListener); button.setActionCommand(commandName); } }