0
我正在使用帶有GridBagLayout的JDialog。由於此佈局自動確定容器和組件的大小,所以我沒有在任何東西上使用setSize。但是,當繪製GUI時,似乎不必要地拉伸容器。GridBagLayout如何確定容器的大小
爲什麼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);
}
}
工作就像一個魅力。謝謝 ! – Sandhya 2012-08-15 07:00:50