我直接在JFrame 窗体上添加了一个 GridBagLayout ,然后拖进去两个按钮,可是设计界面和运行后的界面怎么不一样。
窗体的设计界面如下图:
运行以后的界面如下图:
布局代码代码如下:
- Java code
/** * Create the frame. */ public testLayoutFrame() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 536, 407); GridBagLayout gridBagLayout = new GridBagLayout(); gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0}; gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE}; gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE}; getContentPane().setLayout(gridBagLayout); JButton btnNewButton = new JButton("New button"); GridBagConstraints gbc_btnNewButton = new GridBagConstraints(); gbc_btnNewButton.insets = new Insets(0, 0, 5, 5); gbc_btnNewButton.gridx = 7; gbc_btnNewButton.gridy = 5; getContentPane().add(btnNewButton, gbc_btnNewButton); JButton btnNewButton_1 = new JButton("New button"); GridBagConstraints gbc_btnNewButton_1 = new GridBagConstraints(); gbc_btnNewButton_1.gridx = 10; gbc_btnNewButton_1.gridy = 7; getContentPane().add(btnNewButton_1, gbc_btnNewButton_1); }
------解决方案--------------------
说的不是很准确,应该是内有内容的行和列将被忽略。
------解决方案--------------------
gridBagLayout.columnWidths 不能設為0