有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java GridBagLayout如何确定容器的大小

我正在使用带有GridBagLayout的JDialog。因为这个布局自动决定容器和组件的大小,所以我没有在任何东西上使用setSize。然而,当绘制GUI时,它似乎在不必要地拉伸容器

为什么GridBagLayout不将容器的大小调整为“所需大小”?基本上我希望对话框的大小和里面的桌子一样大。以下是代码片段:

public class GridBagLayoutTester  
{

public static void main(String[] args)

{

JDialog mDialog = new JDialog();

    JPanel panel1 = new JPanel();
    panel1.setLayout(new GridBagLayout());

    // Create a table to be added to the panel
    JTable table = new JTable(4,4);
    JScrollPane scrollpane = new JScrollPane(table);
    scrollpane.setBorder(BorderFactory.createLineBorder(Color.ORANGE, 5));

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = gbc.gridy = 0;
    gbc.fill = GridBagConstraints.NONE;
    gbc.anchor = GridBagConstraints.FIRST_LINE_START;

    // Add table to the panel
    panel1.add(scrollpane, gbc);

    mDialog.add(panel1, BorderLayout.CENTER);

    // Display the window.
    mDialog.pack();
    mDialog.setVisible(true);
  }  
}

共 (0) 个答案