如何根据swing中的JPanel(表)行数增加JFrame大小

时间:2015-11-27 14:56:30

标签: java swing jframe window jpanel

我有摆动应用程序,它执行以下操作:

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随着内部JPanels的增长而成长?

1 个答案:

答案 0 :(得分:2)

在您添加行的组件中实施Scrollable接口,并覆盖getPreferredScrollableViewportSize() here JTablehere JList pack() }。在封闭的Window上调用foo0 = {} foo1 = {} foo2 = {} for i in range(3): exec("foo{}['bar'] = 'baz'".format(i)) exec('print(foo{})'.format(i)) 将使其大小适合其子组件的首选大小和布局。"