package calculator; import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.io.File; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JTextField; public class CalcView2 extends JFrame { private static final long serialVersionUID = -8730447125113729547L; private JMenuBar menuBar; private JMenu fileMenu; private JMenuItem openItem; private JMenuItem saveItem; private JTextField userValueText; private JTextField calcText; private JButton sumButton; private JButton subtractButton; private JButton multiplyButton; private JButton divideButton; private JButton clearButton; private final JFileChooser fileDialog; public CalcView2() { super("Simple Calculator"); this.menuBar = new JMenuBar(); this.setJMenuBar(this.menuBar); this.fileMenu = new JMenu("File"); this.menuBar.add(this.fileMenu); this.openItem = new JMenuItem("Open File"); this.fileMenu.add(this.openItem); this.saveItem = new JMenuItem("Save File"); this.fileMenu.add(this.saveItem); 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.fileDialog = new JFileChooser(); 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); } /** * Shows a file chooser for the user to select the file to open. * * @return The file chosen by the user, or null if no file was chosen. */ public File getOpenFile() { int returnVal = this.fileDialog.showOpenDialog(this); if(returnVal == JFileChooser.APPROVE_OPTION) { return this.fileDialog.getSelectedFile(); } return null; } /** * Shows a file chooser for the user to select the file to save. * * @return The file chosen by the user, or null if no file was chosen. */ public File getSaveFile() { int returnVal = this.fileDialog.showSaveDialog(this); if(returnVal == JFileChooser.APPROVE_OPTION) { return this.fileDialog.getSelectedFile(); } return null; } /** * 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); } /** * Add a listener for the open menu item. * * @param t the action listener */ public void addOpenListener(ActionListener t) { this.openItem.addActionListener(t); } /** * Add a listener for the save menu item. * * @param t the action listener */ public void addSaveListener(ActionListener t) { this.saveItem.addActionListener(t); } }