JScrollPane 内容未显示
我在 JPanel 中有一个 JTextArea,然后将其放入 JScrollPane 中。当包含 JScrollPane 的 JPanel 首次显示时,JScrollPane 会显示,但不会显示内容。一旦调整 JFrame 的大小,内容就会显示出来。
JTextArea area = new JTextArea(6, 20);
area.setText("Some test text");
JPanel panel = new JPanel(new BorderLayout());
panel.add(area, BorderLayout.CENTER);
JScrollPane pane = new JScrollPane();
pane.setBounds(20, 20, WIDTH - 40, 300 - 40);
pane.setPreferredSize(new Dimension(WIDTH - 40, 300 - 40));
add(pane);
pane.setViewportView(panel);
I have a JTextArea inside of a JPanel that is then placed into the JScrollPane. When the JPanel that contains the JScrollPane is first show the JScrollPane shows up but not the contents. As soon as the JFrame is resized the contents show up.
JTextArea area = new JTextArea(6, 20);
area.setText("Some test text");
JPanel panel = new JPanel(new BorderLayout());
panel.add(area, BorderLayout.CENTER);
JScrollPane pane = new JScrollPane();
pane.setBounds(20, 20, WIDTH - 40, 300 - 40);
pane.setPreferredSize(new Dimension(WIDTH - 40, 300 - 40));
add(pane);
pane.setViewportView(panel);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这两行代码没有意义(尽管它们不是问题的原因),
当您使用“空布局”时,将使用第一行。
当您使用布局管理器时使用第二个。
它们不应该一起使用。
首选第二个,因为您应该使用布局管理器。
Those two lines of code doen't make sense (although they are not the cause of your problem)
The first line is used when you are using a "null layout".
The second is used when you are using layout managers.
They should not be used together.
The second is preferred since you should be using layout managers.
在应用程序中,不同的 JPanel 以类似于幻灯片放映的方式进行交换。因此,在代码中会发现类似这样的内容:
问题是第二张幻灯片(slide2)的所有内容都不会显示。解决办法是添加
Whereframe是panel的父窗口。
In the application different JPanels are swapped out in a manner similar to a slide-show. So something like this would be found in the code:
The problem being that all of the contents of the second slide, slide2, would not show up. The solution is to add
Where frame is the parent window of panel.
new JScrollPane(panel);
我相信您需要将面板添加到滚动窗格构造函数中。
new JScrollPane(panel);
I believe that you need to add the panel to the scroll pane constructor.