【问题标题】:how to increase JFrame size according to number of JPanel(table) rows in swing如何根据摇摆中的JPanel(表)行数增加JFrame大小
【发布时间】:2016-03-01 19:26:38
【问题描述】:

我有以下摆动应用程序:

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的增长而增长?

【问题讨论】:

    标签: java swing jframe window jpanel


    【解决方案1】:

    在要添加行的组件中实现Scrollable 接口并覆盖getPreferredScrollableViewportSize(),如here 用于JTablehere 用于JList。在封闭的Window 上调用pack() 将导致它“调整大小以适合其子组件的首选大小和布局。”

    【讨论】:

      猜你喜欢
      • 2022-11-10
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-16
      • 2013-09-02
      • 1970-01-01
      相关资源
      最近更新 更多