java吧 关注:1,240,133贴子:12,712,360
  • 3回复贴,共1

怎样将已经创建好的网格袋布局运用到swing界面中?

只看楼主收藏回复

比如 我在项目中新建一个类,创建好了一个网格袋布局。
这个布局用于email程序中 写信的界面


1楼2016-07-28 21:59回复
    以下为这个网格袋布局的代码
    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);
    }
    }


    2楼2016-07-28 22:02
    回复
      报错信息
      Exception in thread "main" java.lang.Error: Unresolved compilation problem:
      at Message.main(Message.java:15)


      5楼2016-07-28 22:04
      回复
        没人我就再顶一下


        来自Android客户端7楼2016-07-28 22:35
        回复