JScrollPane

Hallo zusammen,

ich arbeite an einer GUI und programmiere von Hand, also ohne GUI-Builder. Leider komme ich mit dem JScrollPane nicht klar.

Folgender Code:

import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


public class Scrollpane {
    
    private JFrame frame;
    private JPanel panel;
    private JScrollPane jsp;
    private JTextArea jta;
    
    public void createMainFrame(){
        frame = new JFrame("Scrollpane");
        frame.setLayout(new FlowLayout());
        
        panel = new JPanel();        
        panel.setLayout(new BorderLayout());
        
        jta = new JTextArea();
        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.

"+
                       "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.

"+
                       "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.";
        jta.setPreferredSize(new Dimension(300, 150));
        
        jta.setText(story);        
        jta.setLineWrap(true);
        jta.setWrapStyleWord(true);
        
        jsp = new JScrollPane(jta);
        jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        
        
        panel.add(jsp, BorderLayout.CENTER);
        frame.add(panel);
        
        frame.setSize(400, 400);
        
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
        
    }
    
    public static void main(String [] args){
        Scrollpane s = new Scrollpane();
        s.createMainFrame();
        
    }    
}```

funktioniert leider nicht und ich verstehe nicht wieso. Die Textarea wird angezeigt, die Scrollbalken werden nur angezeigt, wenn ich sie setze. 
Ich kann jedoch nicht scrollen und der Text wird nicht vollständig angezeigt.

Danke im voraus für die Hilfe.

Viele Grüße
Thomas

Mach den Aufruf von setPreferredSize nicht auf dem JTextArea sondern auf dem ScrollPane. Dann sollte es funktionieren

Manche Dinge sind so einfach. Danke!