how to increase JFrame size according to number of JPanel(table) rows in swing(如何根据摇摆中的JPanel(表)行数增加JFrame大小)
问题描述
我有以下摆动应用程序:
I have swing application which does following:
public void init() {
jFrame = new JFrame();
...
jFrame.add(sortingDataInputComponent.asComponent());
...
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.pack();
jFrame.setVisible(true);
jFrame.toFront();
}
那里的sortingDataInputComponent是JPanel
there sortingDataInputComponent is JPanel with
jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.Y_AXIS));
里面有另一个JPanel
inside of which there is another JPanel with
jPanel.setLayout(new GridLayout(0,numberOfColumns));
在哪里
private final int numberOfColumns = 3;
并且内部 jpanel 在按钮按下时增长:
and inner jpanel grows at button press:
jPanel.add(rowElement.getLabel(), insertionIndex);
jPanel.add(rowElement.getField(), insertionIndex + 1);
jPanel.add(rowElement.getDeleteButton(), insertionIndex + 2);
rowElements.add(rowElement);
jPanel.revalidate();
jPanel.repaint();
但主窗口不会随着 JPanel 一起增长.它具有相同的初始大小.如何让JFrame随着内部JPanels的增长而增长?
but the main windows does not grow with JPanel. It has the same initial size. How to make JFrame grow with the growth of inner JPanels?
推荐答案
实现 Scrollable
接口在您添加行并覆盖 getPreferredScrollableViewportSize()
如图这里 用于 JTable
和 这里 用于 JList
.在封闭的 Window
上调用 pack()
将导致它调整大小以适合其子组件的首选大小和布局."
Implement the Scrollable
interface in the component to which you add rows and override getPreferredScrollableViewportSize()
as shown here for JTable
and here for JList
. Invoking pack()
on the enclosing Window
will cause it to be "sized to fit the preferred size and layouts of its subcomponents."
这篇关于如何根据摇摆中的JPanel(表)行数增加JFrame大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何根据摇摆中的JPanel(表)行数增加JFrame大小


基础教程推荐
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01