/* * Proj1GUI.java * * Created on October 10, 2002, 1:57 AM */ /** Create a GUI for testing a date investigator * The window has a text field where the user can type in a date * There is a text area where the results of the investigation are displayed * * @author dickey */ import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JScrollPane; import javax.swing.JViewport; import java.awt.Container; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.Point; import java.awt.Rectangle; class Proj1GUI extends JFrame { private final static int defaultWindowWidth = 500; private final static int defaultWindowHeight = 500; /** do little in the constructor */ public Proj1GUI() { super("Date Discoverer (default name)"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } //end constructor /** build the GUI frame and display it *There will be a label and two textareas: one for input, one for output */ public void buildUI(IDateInvestigator dateInvestigator) { this.setSize(defaultWindowWidth, defaultWindowHeight); this.setTitle(dateInvestigator.getAuthor() + "'s " + dateInvestigator.getDescription()); Container contents = this.getContentPane(); contents.removeAll(); //in case the method is called more than once //use a border layout (should be the default anyway) contents.setLayout(new BorderLayout()); //create a label, place it at the top String instructions = " Instructions: type in a date " + "and press Enter"; contents.add(new JLabel(instructions), BorderLayout.NORTH); //create a big output textarea JTextArea outputArea = new JTextArea("Results will appear here"); outputArea.setEnabled(false); //don't let user type in JScrollPane outputPane = new JScrollPane(outputArea); Dimension areaSize = new Dimension(2*defaultWindowWidth/3, 3*defaultWindowHeight/4); outputPane.setPreferredSize(areaSize); contents.add(outputPane, BorderLayout.SOUTH); //create a small text input field JTextField inputField = new JTextField("enter the date here"); inputField.addActionListener(new TextListener(dateInvestigator, outputArea)); inputField.setEnabled(true); //it's the default anyway inputField.setSelectionStart(0); inputField.setSelectionEnd(inputField.getText().length()); Dimension fieldSize = new Dimension(defaultWindowWidth/3, defaultWindowHeight/7); inputField.setPreferredSize(fieldSize); contents.add(inputField, BorderLayout.CENTER); this.show(); } //end buildUI } //end Proj1GUI /** A listener for when the user hits enter on the text field */ class TextListener implements ActionListener { IDateInvestigator theInvestigator; JTextArea theTextArea; public TextListener (IDateInvestigator di, JTextArea ta) { theInvestigator = di; theTextArea = ta; } //don't want this constructor called! private TextListener() { } /**method called when user presses the Enter key **/ public void actionPerformed(ActionEvent e) { //the source had darn well better be the text field! JTextField theField = (JTextField) e.getSource(); String usersInput = theField.getText(); System.out.println(usersInput); //for debug //call the method which processes text boolean dateStatus = theInvestigator.setDate(usersInput); String dateInfo = theInvestigator.dateInformation(); // assert dateInfo != null; //comment out for Java 1.3 and earlier if (dateStatus == true) { theTextArea.setText(" [Date was reported as valid]\n" + dateInfo); } else { theTextArea.setText(" [Date was reported as invalid]\n" + dateInfo); } theTextArea.setCaretPosition(0); } //end actionPerformed }