import java.awt.GridLayout; 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; /** * This is the view for the Unit Converter application. */ public class UnitConvView extends JFrame { private JMenuBar menuBar; private JMenu fileMenu; private JMenuItem saveItem; private JMenuItem resetItem; private JTextField inputValue; private JTextField resultValue; private JButton cmToInches; private JButton inchesToCm; private JButton cToF; private JButton fToC; private JButton lbsToKg; private JButton kgToLbs; private final JFileChooser fileDialog; /** * Initializes this view. * @param model the model to associate with this view */ public UnitConvView(UnitConvModel model) { super("Unit Converter"); model.clear(); this.menuBar = new JMenuBar(); this.setJMenuBar(this.menuBar); this.fileMenu = new JMenu("File"); this.menuBar.add(this.fileMenu); this.resetItem = new JMenuItem("Reset"); this.fileMenu.add(resetItem); this.saveItem = new JMenuItem("Save..."); this.fileMenu.add(saveItem); this.inputValue = new JTextField(20); this.resultValue = new JTextField(20); this.resultValue.setText("" + model.getResultValue()); this.resultValue.setEditable(false); this.cToF = new JButton("\u00B0C to \u00B0F"); this.fToC = new JButton("\u00B0F to \u00B0C"); this.cmToInches = new JButton("cm to inches"); this.inchesToCm = new JButton("inches to cm"); this.kgToLbs = new JButton("kg to lbs"); this.lbsToKg = new JButton("lbs to kg"); this.fileDialog = new JFileChooser(); this.setLayout(new GridLayout(5, 2)); this.add(new JLabel("Input Value")); this.add(this.inputValue); this.add(new JLabel("Result Value")); this.add(this.resultValue); this.add(this.cToF); this.add(this.fToC); this.add(this.cmToInches); this.add(this.inchesToCm); this.add(this.kgToLbs); this.add(this.lbsToKg); this.pack(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); UnitConvController controller = new UnitConvController(model, this); } /** * Get the string value of the result text field. * * @return The string in the result text field. */ public String getResultValue() { return this.resultValue.getText(); } /** * Get the string value of the user input text field. * * @return The string in the user input text field. */ public String getInputValue() { // TODO (1 line of code): Return the contents of the text field. // Use getResultValue as a guide. ; } /** * 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 setInputValue(String value) { this.inputValue.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 setResultValue(String value) { // TODO (1 line of code): Set the contents of the text field. // Use setInputValue as a guide. ; } /** * 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 reset menu item. * * @param t the action listener */ public void addResetListener(ActionListener t) { this.resetItem.addActionListener(t); } /** * Add a listener for the save menu item. * * @param t the action listener */ public void addSaveListener(ActionListener t) { this.saveItem.addActionListener(t); } /** * Add a listener for the "C to F" button. * * @param t the action listener */ public void addCtoFListener(ActionListener t) { this.cToF.addActionListener(t); } /** * Add a listener for the "F to C" button. * * @param t the action listener */ public void addFtoCListener(ActionListener t) { // TODO (1 line of code): add the listener to the fToC button. // Use the above listeners as a guide. ; } /** * Add a listener for the "cm to inches" button. * * @param t the action listener */ public void addCmToInchesListener(ActionListener t) { // TODO (1 line of code): add the listener to the cmToInches button. // Use the above listeners as a guide. ; } /** * Add a listener for the "inches to cm" button. * * @param t the action listener */ public void addInchesToCmListener(ActionListener t) { // TODO (1 line of code): add the listener to the inchesToCm button. // Use the above listeners as a guide. ; } /** * Add a listener for the "kg to lbs" button. * * @param t the action listener */ public void addKgToLbsListener(ActionListener t) { // TODO (1 line of code): add the listener to the kgToLbs button. // Use the above listeners as a guide. ; } /** * Add a listener for the "lbs to kg" button. * * @param t the action listener */ public void addLbsToKgListener(ActionListener t) { // TODO (1 line of code): add the listener to the lbsToKg button. // Use the above listeners as a guide. ; } }