简体   繁体   中英

JTextArea to ScrollPane not working

The Scrollbar does not appear in the Frame and the TextArea is somehow not editable, please help, thanks :)

import javax.swing.*;
import java.awt.*;

public class Test extends JFrame{
    Container c;
    JTextArea jT;
    JScrollPane scroll;
    public Test(){
        c = getContentPane();
        c.setLayout(new GridLayout(1,1));
        jT = new JTextArea();
        scroll = new JScrollPane();  //creating JScrollPane
        scroll.add(jT);              // adding jT to scroll
        c.add(scroll);
    }
    public static void main(String[] args){
        Test fenster = new Test();
        fenster.setLocationRelativeTo(null);
        fenster.setTitle("Test");
        fenster.setSize(200, 200);
        fenster.setVisible(true);
        fenster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

You need initialize the Scroll Pane with the component for which you need to display the scroll bars.

    scroll = new JScrollPane(jT);  //creating JScrollPane; Do this
//  scroll.add(jT);                // don't do this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM