简体   繁体   English

调整 JTable/滚动窗格的大小

[英]Resize JTable/Scrollpane

It's actually an question about this .这实际上是一个关于的问题。 How do I resize this now?我现在如何调整它的大小? I used this, I was able to resize my JPanel, but I can't find how to resize the table or scrollpane.我使用了这个,我能够调整我的 JPanel 的大小,但我找不到如何调整表格或滚动窗格的大小。 I have 10 columns and the table just stays the same size.我有 10 列,表格的大小保持不变。

Thanks in advance提前致谢

I was able to resize my JPanel, but I can't find how to resize the table or scrollpane.我能够调整 JPanel 的大小,但找不到如何调整表格或滚动窗格的大小。

The problem is that the JPanel uses a FlowLayout.问题是 JPanel 使用 FlowLayout。 A FlowLayout will respect the preferred size of any component added to the panel. FlowLayout 将尊重添加到面板的任何组件的首选大小。 So in the example code the table is displayed at its default preferred size.因此,在示例代码中,表格以其默认的首选大小显示。

A couple of solutions:几个解决方案:

  1. There is no need for the panel.不需要面板。 Just add the scrollpane directly to the frame.只需将滚动窗格直接添加到框架中即可。 The table will grow/shrink as the frame size changes.该表将随着帧大小的变化而增长/缩小。
  2. Change the layout manager of the panel to one that will allow components added to it to grow to fill the available space.将面板的布局管理器更改为允许添加到其中的组件增长以填充可用空间的布局管理器。 Again the table will grow/shrink as required.同样,表格将根据需要增长/缩小。
  3. Override the getPreferredScrollableViewportSize(...) method of the JTable .覆盖JTablegetPreferredScrollableViewportSize(...)方法。 This will override the default preferred size of the table.这将覆盖表的默认首选大小。 The table may grow/shrink depending of the layout manager used.根据所使用的布局管理器,表格可能会增大/缩小。

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

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