Add a scroll bar to text area

隐身守侯 提交于 2019-12-04 05:20:19

问题


I use Eclipse Window Builder. When I click the button, something will be written on the screen. But since my prints are long, I want to use a scroll pane.

public class uyg2 extends JFrame {

private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                uyg2 frame = new uyg2();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public uyg2() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JButton btnNewButton = new JButton("New button");
    btnNewButton.setBounds(32, 29, 89, 23);
    contentPane.add(btnNewButton);

    JTextArea textArea = new JTextArea();
    textArea.setBounds(10, 63, 233, 173);
    contentPane.add(textArea);

    ScrollPane scrollPane = new ScrollPane();
    scrollPane.setBounds(249, 10, 173, 118);
    contentPane.add(scrollPane);
}

回答1:


So, based on...

public class uyg1 extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    uyg1 frame = new uyg1();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
    }

    /**
     * Create the frame.
     */
    public uyg1() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        JTextArea textArea = new JTextArea("Test");
        textArea.setSize(400, 400);
        JScrollPane scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        frame.getContentPane().add(scroll);
        frame.setVisible(true);
    }
}

textArea.setSize(400, 400); is irrelevant as the layout manager will deal with it. You can provide sizing hints via the JTextArea(String, int, int) constructer, but remember, this is the number of characters in width/height, not pixels.

The following are giving you issues...

frame.getContentPane().add(scroll);
frame.setVisible(true);

because frame is undefined. Since the class extends from JFrame, they are pointless, it should just be

getContentPane().add(scroll);
setVisible(true);

but, I'd add...

pack();
setLocationRelativeTo(null);

before it, as it will give you a generally better experience




回答2:


You need to add the TextArea to the ScrollPane. Don't add the textarea tho the contentpane.



来源:https://stackoverflow.com/questions/56801880/add-a-scroll-bar-to-text-area

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!