简体   繁体   English

JScrollPane 内容不显示

[英]JScrollPane Contents Not Showing

I have a JTextArea inside of a JPanel that is then placed into the JScrollPane.我在 JPanel 中有一个 JTextArea,然后将其放入 JScrollPane 中。 When the JPanel that contains the JScrollPane is first show the JScrollPane shows up but not the contents.当包含 JScrollPane 的 JPanel 首次显示时,JScrollPane 会显示,但不会显示内容。 As soon as the JFrame is resized the contents show up.调整 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);
pane.setBounds(20, 20, WIDTH - 40, 300 - 40); 
pane.setPreferredSize(new Dimension(WIDTH - 40, 300 - 40)); 

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.第二个是首选,因为您应该使用布局管理器。

In the application different JPanels are swapped out in a manner similar to a slide-show.在应用程序中,不同的 JPanel 以类似于幻灯片的方式被换出。 So something like this would be found in the code:所以会在代码中找到这样的东西:

panel.remove(slide1);
panel.add(slide2);
panel.repaint();

The problem being that all of the contents of the second slide, slide2, would not show up.问题是第二张幻灯片 slide2 的所有内容都不会显示出来。 The solution is to add解决方案是添加

frame.validate();

Where frame is the parent window of panel.其中 frame 是面板的父 window。

new JScrollPane(panel);

I believe that you need to add the panel to the scroll pane constructor.我相信您需要将面板添加到滚动窗格构造函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM