开发者

JScrollPane Contents Not Showing

开发者 https://www.devze.com 2023-03-09 08:47 出处:网络
I have a JTextArea inside of a JPanel that is then placed into the JScrollP开发者_如何学编程ane. When the JPanel that contains the JScrollPane is first show the JScrollPane shows up but not the conten

I have a JTextArea inside of a JPanel that is then placed into the JScrollP开发者_如何学编程ane. 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);


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. 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. The solution is to add

frame.validate();

Where frame is the parent window of panel.


new JScrollPane(panel);

I believe that you need to add the panel to the scroll pane constructor.

0

精彩评论

暂无评论...
验证码 换一张
取 消