/* * MyText.java is a 1.4 application that demos a simple editable text area. */ import javax.swing.*; import javax.swing.text.*; import java.awt.*; //for layout managers and more import java.awt.event.*; //for action events import java.net.URL; import java.io.IOException; public class MyText extends JPanel implements ActionListener { public MyText() { setLayout(new BorderLayout()); //Lay out the text controls and the labels. JPanel textControlsPane = new JPanel(); //Create a text area. JTextArea textArea = new JTextArea( "This is an editable JTextArea. " + "A text area is a \"plain\" text component, " + "which means that although it can display text " + "in any font, all of the text is in the same font." ); textArea.setFont(new Font("Serif", Font.ITALIC, 16)); textArea.setLineWrap(true); textArea.setWrapStyleWord(true); JScrollPane areaScrollPane = new JScrollPane(textArea); areaScrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); areaScrollPane.setPreferredSize(new Dimension(250, 250)); areaScrollPane.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("Plain Text"), BorderFactory.createEmptyBorder(5,5,5,5)), areaScrollPane.getBorder())); //Put everything together. textControlsPane.add(areaScrollPane); add(textControlsPane); } public void actionPerformed(ActionEvent e) { // This action listener is required, even if not used. } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { //Make sure we have nice window decorations. JFrame.setDefaultLookAndFeelDecorated(true); //Create and set up the window. JFrame frame = new JFrame("MyText"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. JComponent newContentPane = new MyText(); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }