A gridbag layout is the most sophisticated layout manager in Java's UI toolkit. This example demonstrates how to create and set a gridbag layout and how to set gridbag constraints on components added to the layout. See other examples for code that demonstrates the usage of available gridbag constraints. JFrame frame = new JFrame();
Container container = frame.getContentPane();
// Create the layout
GridBagLayout gbl = new GridBagLayout();
// Set layout on container
container.setLayout(gbl);
// Place a component at cell location (1,1)
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 1;
// Add other gridbag constraints here
// Associate the gridbag constraints with the component
gbl.setConstraints(component, gbc);
// Add the component to the container
container.add(component);
// Show the frame
frame.pack();
frame.setVisible(true);
Container container = frame.getContentPane();
// Create the layout
GridBagLayout gbl = new GridBagLayout();
// Set layout on container
container.setLayout(gbl);
// Place a component at cell location (1,1)
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 1;
// Add other gridbag constraints here
// Associate the gridbag constraints with the component
gbl.setConstraints(component, gbc);
// Add the component to the container
container.add(component);
// Show the frame
frame.pack();
frame.setVisible(true);
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货