好的,所以我正在学习手工编写和使用布局,而不是使用Netbeans的GUI Builder。我知道我的问题对你来说可能很简单,但对于刚认识的人来说却不那么简单。我的问题是,即使我设置gbc.gridx = 10和/或gbc.gridy = 2,它也不会更进一步。它看起来只是将它限制在1或2位置,但不会更进一步。即使gridheight = 2或任何N也不会生效。我在这做错了什么?请帮忙。提前致谢。
package gridbaglayoutsample;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
public class GridBagLayoutSample {
JFrame myFrame = new JFrame();
JPanel myPanel_1 = new JPanel(new GridBagLayout());
JButton myButton_1 = new JButton("Button 1");
JButton myButton_2 = new JButton("Button 2");
JButton myButton_3 = new JButton("Button 3");
public GridBagLayoutSample(){
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(3,3,3,3); //insets are simple spacing for TOP LEFT BOTTOM RIGHT or PADDING
Border myLineBorder = BorderFactory.createLineBorder(Color.yellow,3);
myFrame.setLayout(new GridBagLayout());
myFrame.setPreferredSize(new Dimension(500, 500));
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setTitle("Grid Bag Layout Sample");
myFrame.setVisible(true);
myFrame.pack();
myFrame.setLocationRelativeTo(null);
myPanel_1.setBorder(myLineBorder);
gbc.gridx = 10; // set x position (STEP 1)
gbc.gridy = 0; //set y position (STEP 2)
myPanel_1.add(myButton_1,gbc); // after setting x and y, you can add (STEP 3)
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridheight = 2;
myPanel_1.add(myButton_2,gbc);
myFrame.add(myPanel_1);
}
public static void main(String[] args) {
new GridBagLayoutSample();
}
}