67,542
社区成员
发帖
与我相关
我的任务
分享
import java.awt.*;
import javax.swing.JPanel;
public class GridBagEx22 extends JPanel {
protected void makebutton(String name,GridBagLayout gridbag,GridBagConstraints c) {
Button button = new Button(name);
gridbag.setConstraints(button, c);
add(button);
}
public void init() {
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setLayout(gridbag);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.gridwidth = GridBagConstraints.REMAINDER; //end row
makebutton("Button1", gridbag, c);
makebutton("Button2", gridbag, c);
makebutton("Button3", gridbag, c);
setSize(500, 300);
}
public static void main(String args[]) {
Frame f = new Frame("GridBag Layout Example");
GridBagEx22 ex1 = new GridBagEx22();
ex1.init();
f.add("Center", ex1);
f.setSize(500,400);
f.setVisible(true);
}
}