BorderLayout.CENTER 内的 JScrollPane 不调整大小
这是下面的代码,问题是 JScrollPane 保持其首选大小,并且不随 BorderLayout.CENTER 区域调整大小。当然,我希望它填充该区域......并使用它来滚动其中表格的内容。
frame = new JFrame();
frame.setBounds(100, 100, 500, 400);
JPanel panelCenter = new JPanel();
FlowLayout flowLayout = (FlowLayout) panelCenter.getLayout();
flowLayout.setAlignment(FlowLayout.LEFT);
panelCenter.setPreferredSize(new Dimension(300, 400));
panelRight = new ActorDrawer();
frame.getContentPane().add(panelRight, BorderLayout.EAST);
panelRight.setLayout(null);
panelRight.setPreferredSize(new Dimension(200, 400));
frame.getContentPane().add(panelCenter, BorderLayout.CENTER);
table = new JTable(new MyTableModel());
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
panelCenter.add(scrollPane);
This is the code below, the problem is that the JScrollPane stays at its preferred size and doesn't resize with the BorderLayout.CENTER region. I want it to fill that region, of course.. and use it to scroll the contents of the table inside it.
frame = new JFrame();
frame.setBounds(100, 100, 500, 400);
JPanel panelCenter = new JPanel();
FlowLayout flowLayout = (FlowLayout) panelCenter.getLayout();
flowLayout.setAlignment(FlowLayout.LEFT);
panelCenter.setPreferredSize(new Dimension(300, 400));
panelRight = new ActorDrawer();
frame.getContentPane().add(panelRight, BorderLayout.EAST);
panelRight.setLayout(null);
panelRight.setPreferredSize(new Dimension(200, 400));
frame.getContentPane().add(panelCenter, BorderLayout.CENTER);
table = new JTable(new MyTableModel());
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
panelCenter.add(scrollPane);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要使用
BorderLayout
将JScrollPane
添加到面板的中心:您可以使用FlowLayout
将其添加到另一个面板,并且该面板被添加到中心。删除panelCenter
。You don't add the
JScrollPane
to the center of the panel with theBorderLayout
: you add it to another panel, with aFlowLayout
, and this panel is added to the center. Remove thepanelCenter
.