以下为这个网格袋布局的代码
import java.awt.*;
import javax.swing.*;
public class MessagePanel extends JFrame {
GridBagLayout gridbag = new GridBagLayout();
public MessagePanel(){
super();
GridBagConstraints constraints;
setLayout(gridbag);
//创建标签和文本区域
JLabel toL = new JLabel("To: ");
JTextField toT = new JTextField();
JLabel subL = new JLabel("Subject: ");
JTextField subT = new JTextField();
JLabel ccL = new JLabel("cc: ");
JTextField ccT = new JTextField();
JLabel bccL = new JLabel("bcc: ");
JTextField bccT = new JTextField();
//加入到网格袋
addComponent(toL,0,0,1,1,10,100,
GridBagConstraints.NONE,GridBagConstraints.EAST);
addComponent(toT,1,0,9,1,90,100,
GridBagConstraints.HORIZONTAL,GridBagConstraints.WEST);
addComponent(subL,0,1,1,1,10,100,
GridBagConstraints.NONE,GridBagConstraints.EAST);
addComponent(subT,1,1,9,1,90,100,
GridBagConstraints.HORIZONTAL,GridBagConstraints.WEST);
addComponent(ccL, 0, 2, 1, 1, 10, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
addComponent(ccT, 1, 2, 4, 1, 40, 100,
GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
addComponent(bccL, 5, 2, 1, 1, 10, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
addComponent(bccT, 6, 2, 4, 1, 40, 100,
GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
}
private void addComponent(Component component,int gridx,int gridy,
int gridwidth,int gridheight,int weightx,int weighty,int fill,
int anchor){
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = gridx;
constraints.gridy = gridy;
constraints.gridwidth = gridwidth;
constraints.gridheight = gridheight;
constraints.weightx = weightx;
constraints.weighty = weighty;
constraints.fill = fill;
constraints.anchor = anchor;
gridbag.setConstraints(component, constraints);
}
}
import java.awt.*;
import javax.swing.*;
public class MessagePanel extends JFrame {
GridBagLayout gridbag = new GridBagLayout();
public MessagePanel(){
super();
GridBagConstraints constraints;
setLayout(gridbag);
//创建标签和文本区域
JLabel toL = new JLabel("To: ");
JTextField toT = new JTextField();
JLabel subL = new JLabel("Subject: ");
JTextField subT = new JTextField();
JLabel ccL = new JLabel("cc: ");
JTextField ccT = new JTextField();
JLabel bccL = new JLabel("bcc: ");
JTextField bccT = new JTextField();
//加入到网格袋
addComponent(toL,0,0,1,1,10,100,
GridBagConstraints.NONE,GridBagConstraints.EAST);
addComponent(toT,1,0,9,1,90,100,
GridBagConstraints.HORIZONTAL,GridBagConstraints.WEST);
addComponent(subL,0,1,1,1,10,100,
GridBagConstraints.NONE,GridBagConstraints.EAST);
addComponent(subT,1,1,9,1,90,100,
GridBagConstraints.HORIZONTAL,GridBagConstraints.WEST);
addComponent(ccL, 0, 2, 1, 1, 10, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
addComponent(ccT, 1, 2, 4, 1, 40, 100,
GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
addComponent(bccL, 5, 2, 1, 1, 10, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
addComponent(bccT, 6, 2, 4, 1, 40, 100,
GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
}
private void addComponent(Component component,int gridx,int gridy,
int gridwidth,int gridheight,int weightx,int weighty,int fill,
int anchor){
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = gridx;
constraints.gridy = gridy;
constraints.gridwidth = gridwidth;
constraints.gridheight = gridheight;
constraints.weightx = weightx;
constraints.weighty = weighty;
constraints.fill = fill;
constraints.anchor = anchor;
gridbag.setConstraints(component, constraints);
}
}