JScrollPane is not added to JTextArea

后端 未结 2 2043
无人及你
无人及你 2021-01-17 06:18

I saw a few questions like this question, but I couldn\'t get this solved. I cannot get a JScrollPane visible on JTextArea. Can anyone please point

2条回答
  •  不要未来只要你来
    2021-01-17 06:55

    When you use JTextArea component with JSrcollPane component you should set size and position to the last component and not to the first one, and when you add the created elements to the JFrame you should only add JScrollPane component because it is considered as container to the JTextArea component, try this code:

    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    
    public class Main extends JFrame
    {
        JTextArea tarea;
        JScrollPane pan;
        public Main()
        {
            tarea = new JTextArea();
            pan = new JScrollPane(tarea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            pan.setBounds(100, 100, 200, 200);
    
            add(pan);
    
            setLayout(null);
            setSize(600, 600);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
        }
    
        public static void main(String[] aegs)
        {
            Main f = new Main();
        }
    }
    

提交回复
热议问题