如何根据swing中的JPanel(表)行数增加JFrame大小 [英] how to increase JFrame size according to number of JPanel(table) rows in swing

查看:141
本文介绍了如何根据swing中的JPanel(表)行数增加JFrame大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的swing应用程序:

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();
}

sortDataInputComponent是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?

推荐答案

实现 可滚动 界面添加行并覆盖 getPreferredScrollableViewportSize( ) 此处所示 JTable 此处 JList 。在封闭的窗口上调用 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."

这篇关于如何根据swing中的JPanel(表)行数增加JFrame大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆