Java Textarea ScrollPane

后端 未结 4 1901
旧巷少年郎
旧巷少年郎 2021-01-28 23:24

I have created a textarea, and i need a scrollbar applied to the textarea when necessary (when the text gets too long down and it cant be read anymore).

this is the code

4条回答
  •  忘了有多久
    2021-01-28 23:52

    see this

     import javax.swing.*;
    
        public class TestFrame extends JFrame
    
    {
        JTextAreaWithScroll textArea;
    
        public TestFrame ()
        {
            super ("Test Frame");
    
            setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            setSize (300, 300);
    
            textArea = new JTextAreaWithScroll (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                                                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    
            add (textArea.getScrollPane ());
        }
    
        public static void main (String[] args)
        {
            SwingUtilities.invokeLater (new Runnable()
            {
                public void run ()
                {
                    TestFrame f = new TestFrame ();
                    f.setVisible (true);
                }
            });
        }
    }
    
    
    class JTextAreaWithScroll extends JTextArea
    {
        private JScrollPane scrollPane;
    
        public JTextAreaWithScroll (int vsbPolicy, int hsbPolicy)
        {
            scrollPane = new JScrollPane (this, vsbPolicy, hsbPolicy);
        }
    
        public JScrollPane getScrollPane ()
        {
            return scrollPane;
        }
    }
    

    from http://forum.html.it/forum/showthread/t-1035892.html

提交回复
热议问题