package calculator; import java.awt.FlowLayout; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class CalcView2 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 CalcView2() { 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.subtractButton = new JButton("Subtract"); this.multiplyButton = new JButton("Multiply"); this.divideButton = new JButton("Divide"); this.clearButton = new JButton("Clear"); this.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); } /** * Add a listener for the sum button. * * @param t the action listener */ public void addSumListener(ActionListener t) { this.sumButton.addActionListener(t); } /** * Add a listener for the subtract button. * * @param t the action listener */ public void addSubtractListener(ActionListener t) { this.subtractButton.addActionListener(t); } /** * Add a listener for the multiply button. * * @param t the action listener */ public void addMultiplyListener(ActionListener t) { this.multiplyButton.addActionListener(t); } /** * Add a listener for the divide button. * * @param t the action listener */ public void addDivideListener(ActionListener t) { this.divideButton.addActionListener(t); } /** * Add a listener for the clear button. * * @param t the action listener */ public void addClearListener(ActionListener t) { this.clearButton.addActionListener(t); } }