| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | import javax.swing.*; import java.awt.Dimension; public class ScrollPaneExample{ public JPanel createContentPane (){ // As usual, we create our bottom-level panel. JPanel totalGUI = new JPanel(); // This is the story we took from Wikipedia. String story = "The Internet Foundation Classes (IFC) were a graphics "+ "library for Java originally developed by Netscape Communications "+ "Corporation and first released on December 16, 1996.\n\n"+ "On April 2, 1997, Sun Microsystems and Netscape Communications"+ " Corporation announced their intention to combine IFC with other"+ " technologies to form the Java Foundation Classes. In addition "+ "to the components originally provided by IFC, Swing introduced "+ "a mechanism that allowed the look and feel of every component "+ "in an application to be altered without making substantial "+ "changes to the application code. The introduction of support "+ "for a pluggable look and feel allowed Swing components to "+ "emulate the appearance of native components while still "+ "retaining the benefits of platform independence. This feature "+ "also makes it easy to have an individual application's appearance "+ "look very different from other native programs.\n\n"+ "Originally distributed as a separately downloadable library, "+ "Swing has been included as part of the Java Standard Edition "+ "since release 1.2. The Swing classes are contained in the "+ "javax.swing package hierarchy."; // We create the TextArea and pass the story in as an argument. // We also set it to be non-editable, and the line and word wraps set to true. JTextArea storyArea = new JTextArea(story); storyArea.setEditable(false); storyArea.setLineWrap(true); storyArea.setWrapStyleWord(true); // We create the ScrollPane and instantiate it with the TextArea as an argument // along with two constants that define the behaviour of the scrollbars. JScrollPane area = new JScrollPane(storyArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); // We then set the preferred size of the scrollpane. area.setPreferredSize(new Dimension(300, 200)); // and add it to the GUI. totalGUI.add(area); totalGUI.setOpaque(true); return totalGUI; } private static void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("[=] Embrace of the JScrollPane [=]"); ScrollPaneExample demo = new ScrollPaneExample(); frame.setContentPane(demo.createContentPane()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(350, 300); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } } |
| 34 35 36 37 | JTextArea storyArea = new JTextArea(story); storyArea.setEditable(false); storyArea.setLineWrap(true); storyArea.setWrapStyleWord(true); |
| 41 42 | JScrollPane area = new JScrollPane(storyArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); |
| 45 | area.setPreferredSize(new Dimension(300, 200)); |
| 48 | totalGUI.add(area); |
