import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.Date; //--> Note: Comments starting with //--> are meta-comments about how to write //--> OO Java code and wouldn't appear in a production environment. /** * Class representing a security logging UI used by system * operations staff. */ public class LogMonitor extends JFrame { private JTextArea _textArea; // Multiline area for log messages /** Constructor */ public LogMonitor() { super("Security Log"); //--> A quick and easy way to have a scrolling text display //--> is to use a JTextArea inside a JScrollPane. _textArea = new JTextArea(10,40); _textArea.setEditable(false); //--> Note that most GUIs do most of their work by //--> managing a collection of JDK class objects. JScrollPane areaScrollPane = new JScrollPane(_textArea); areaScrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); this.getContentPane().add(areaScrollPane); this.pack(); } /** * Logs a message to the logging UI. * @param msg a non-null string to be logged. */ public void log(String msg) { //assert (msg != null); this.show(); // Just in case it was hidden. //--> A Date with no arguments gives the current timestamp. Date d = new Date(); _textArea.append(d.toString() + ": "+msg + "\n"); } };