我有 Swing 应用程序,它执行以下操作:
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
jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.Y_AXIS));
里面有另一个 JPanel
jPanel.setLayout(new GridLayout(0,numberOfColumns));
哪里
private final int numberOfColumns = 3;
按下按钮时内部 jpanel 会增长:
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随着内部JPanel的增长而增长?
最佳答案
实现Scrollable
您添加行并覆盖的组件中的接口(interface) getPreferredScrollableViewportSize()
如图here对于 JTable
和 here对于JList
。在封闭的 Window
上调用 pack()
将导致其“调整大小以适应其子组件的首选大小和布局。”
关于java - 如何根据 swing 中的 JPanel(table) 行数增加 JFrame 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33959998/