目前,我正在尝试使JScrollPane与正在创建的GUI一起使用。我目前有一个名为consoleTextArea的文本区域,我希望能够滚动它。目前,我的代码是:

consoleTextArea = new JTextArea();
consoleTextArea.setBounds(10, 11, 546, 459);

JScrollPane scroller = new JScrollPane(consoleTextArea);
scroller.setBounds(0, 451, 551, -451);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
panel.add(scroller);


如果使用panel.add(scroller),我什至看不到textArea。
如果使用panel.add(consoleTextArea),我会看到文本区域,但无法滚动。

我在这里做错了什么?

最佳答案

您很有可能没有使用布局管理器。如果不使用布局管理器(absolute positioning),则会遇到这些类型的问题,其中组件的大小或放置不正确。因此,使用一个很好的理由。

读取:Using Layout Managers

10-07 13:04