import java.awt.*;
import javax.swing.*;
class layout extends JFrame {
public layout(String title) {
GridBagLayout layout = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
this.setLayout(layout);
c.fill = GridBagConstraints.BOTH;
c.gridwidth = GridBagConstraints.REMAINDER;
this.addComponent("课程表", layout, c);
c.gridwidth = 1;
c.weightx = 1;
c.weighty = 1;
this.addComponent(" ", layout, c);
this.addComponent("星期一", layout, c);
this.addComponent("星期二", layout, c);
this.addComponent("星期三", layout, c);
this.addComponent("星期四", layout, c);
this.addComponent("星期五", layout, c);
c.gridwidth = GridBagConstraints.REMAINDER;
this.addComponent("星期六", layout, c);
c.gridwidth = 1;
this.addComponent("第一二节", layout, c);
this.addComponent(" ", layout, c);
this.addComponent(" ", layout, c);
this.addComponent(" ", layout, c);
this.addComponent(" ", layout, c);
this.addComponent(" ", layout, c);
c.gridwidth = GridBagConstraints.REMAINDER;
this.addComponent(" ", layout, c);
c.gridwidth = 1;
this.addComponent("第三四节", layout, c);
this.addComponent(" ", layout, c);
this.addComponent(" ", layout, c);
this.addComponent(" ", layout, c);
this.addComponent(" ", layout, c);
this.addComponent(" ", layout, c);
c.gridwidth = GridBagConstraints.REMAINDER;
this.addComponent(" ", layout, c);
c.weightx = 0;
c.weighty = 0;
this.addComponent("午休", layout, c);
c.weightx = 1;
c.weighty = 1;
c.gridwidth = 1;
this.addComponent("第五六节", layout, c);
this.addComponent(" ", layout, c);
this.addComponent(" ", layout, c);
this.addComponent(" ", layout, c);
this.addComponent(" ", layout, c);
this.addComponent(" ", layout, c);
c.gridwidth = GridBagConstraints.REMAINDER;
this.addComponent(" ", layout, c);
c.gridwidth = 1;
this.addComponent("第七八节", layout, c);
this.addComponent(" ", layout, c);
this.addComponent(" ", layout, c);
this.addComponent(" ", layout, c);
this.addComponent(" ", layout, c);
this.addComponent(" ", layout, c);
c.gridwidth = GridBagConstraints.REMAINDER;
this.addComponent(" ", layout, c);
c.gridwidth = 1;
// c.gridx
this.setTitle(title);
this.pack();
this.setVisible(true);
}
private void addComponent(String name,GridBagLayout layout,GridBagConstraints c) {
Button bt = new Button(name);
layout.setConstraints(bt,c);
this.add(bt);
}
}
public class Example{
public static void main(String[] args) {
new layout("GridBagLayout");
}
}
原文:https://www.cnblogs.com/moaying/p/14806044.html