且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将JScrollPane添加到JPanel

更新时间:2023-12-05 19:30:04

您遇到的主要问题是默认布局管理器的布局设置为FlowLayout,这意味着JScrollPane将要使用它的首选尺寸进行布局,这样可能无法填满整个面板.

The primary problem you're having is the fact that the default layout manager's layout is set to FlowLayout, which means that the JScrollPane will want to use it's preferred size to be layout with, which may not fill the entire panel.

相反,请使用BorderLayout

final JPanel info = new JPanel(new BorderLayout()); // <-- Change me :D
final JScrollPane infoS = new JScrollPane(info,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// These are bad ideas, setLocation and setSize won't work, as the panel should be
// under the control of a layout manager
//info.setLocation(10,78);
//info.setSize(420,490);
//infoS.setPreferredSize(new Dimension(600, 600));
gui.add(infoS);