import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class TextDisplay { protected JFrame frame; // The frame that holds the display and key panels protected JTextArea display; // This holds the text JScrollPane textPane; // JTextArea is wrapped in this to get scrolling /** * The constructor builds the window, prepares it for use, and displays it. */ public TextDisplay() { // Build the overall frame frame = new JFrame("Output Window"); frame.setBackground(Color.LIGHT_GRAY); frame.setSize(500,300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); // Text is displayed in a scrolling pane in the CENTER of the border layout display = new JTextArea(); display.setEditable(false); display.setLineWrap(true); display.setWrapStyleWord(true); display.setFont(new Font("Courier", Font.PLAIN, 14)); textPane = new JScrollPane(display); // Wrap TextArea in a ScrollPane // Finally, add everything to the frame and make it visible frame.add(textPane, BorderLayout.CENTER); frame.setVisible(true); } /** * Call this method to append text to the scrolling pane. No newlines are added, * so be sure to include a "\n" where desired. * * @param msg Text to add to the scrolling pane */ public void print(String msg) { display.append(msg); //textPane.getVerticalScrollBar().setValue(Integer.MAX_VALUE); } /** * Call this method to append text to the scrolling pane. A newline * is added at the end of the message. * * @param msg Text to add to the scrolling pane */ public void println(String msg) { display.append(msg+"\n"); //textPane.getVerticalScrollBar().setValue(Integer.MAX_VALUE); } }