Insets insets = new Insets(5,5 ,5 , 5);
panel.add(a, new GridBagConstraints(0, 0, 1, 4, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0));
panel.add(b, new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0));
panel.add(c, new GridBagConstraints(2, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0));
panel.add(d, new GridBagConstraints(3, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0));
panel.add(e, new GridBagConstraints(4, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0));

解决方案 »

  1.   

    这东东功能强,但不是很好用,特别是在不用IDE开发时,昨天为了一个布局折腾了一上午才搞好,看到它就头大!
      

  2.   

    应该用JBuilder来创建就不用发愁布局的问题了
      

  3.   

    package com.boasoft;import java.awt.Button;
    import java.awt.Color;
    import java.awt.Event;
    import java.awt.Frame;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.Panel;public class RadioButtonDemo extends Frame
    {
        public RadioButtonDemo()
        {
            setTitle("网袋布局");
            GridBagLayout layout=new GridBagLayout();
           
            Panel p=new Panel(layout);
            GridBagConstraints cons=new GridBagConstraints();
            cons.gridx=100;
            cons.gridy=135; // modified here 
            cons.gridwidth=2;
            cons.gridheight=4;
            cons.weightx=0;
            cons.weighty=GridBagConstraints.RELATIVE;
            Button b1=new Button("A");
            layout.setConstraints(b1,cons);
            p.add(b1);
            
            cons.gridx=200;
            cons.gridy=100;
            cons.gridwidth=1;
            cons.gridheight=1;
            cons.weightx=0;
            cons.weighty=0;
            Button b2=new Button("B");
            layout.setConstraints(b2,cons);
            p.add(b2);
            
            cons.gridx=200;
            cons.gridy=125;
            cons.gridwidth=1;
            cons.gridheight=1;
            cons.weightx=0;
            cons.weighty=0;
            Button b3=new Button("C");
            layout.setConstraints(b3,cons);
            p.add(b3);
            
            cons.gridx=200;
            cons.gridy=150;
            cons.gridwidth=1;
            cons.gridheight=1;
            cons.weightx=0;
            cons.weighty=0;
            Button b4=new Button("D");
            layout.setConstraints(b4,cons);
            p.add(b4);
            
            cons.gridx=200;
            cons.gridy=175;
            cons.gridwidth=1;
            cons.gridheight=1;
            cons.weightx=0;
            cons.weighty=0;
            Button b5=new Button("E");
            layout.setConstraints(b5,cons);
            p.add(b5);
                    
            add("Center",p);
            
            
            
            
        }
        public boolean handleEvent(Event evt)
        {
            if(evt.id==Event.WINDOW_DESTROY)System.exit(0);
            return super.handleEvent(evt);
            
        }
        
        public static void main(String[] args)
        {
            RadioButtonDemo f=new RadioButtonDemo();
            f.resize(600,500);
            f.setBackground(Color.white);
            f.show();
        }
    }
    //是要这个效果吗?建议你使用JPanel中来布局,就像HTML使用<table>来布局一样
      

  4.   

    JBuilder是好用,但公司现在不用这东西,用EditPlus + JDK,就是在做GUI时比较痛苦
      

  5.   

    这个是你想要的效果啊!测试过没问题的import javax.swing.*;
    import java.awt.*;public class test {
        
        public static void main(String args[]) {
    JFrame f = new JFrame(" GridBagLayout Simples");
            GridBagConstraints c ;        f.getContentPane().setLayout(new GridBagLayout());        f.addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent evt) {
            System.exit(0);
                }
            });        c = new GridBagConstraints();
            c.gridx = 0;
            c.gridy = 0;
            c.gridheight = 4;
            c.fill = GridBagConstraints.VERTICAL;
            f.getContentPane().add(new JButton("A"), c);        c = new GridBagConstraints();
            c.gridx = 1;
            c.gridy = 0;
            f.getContentPane().add(new JButton("B"), c);        c = new GridBagConstraints();
            c.gridx = 1;
            c.gridy = 1;
            f.getContentPane().add(new JButton("C"), c);        c = new GridBagConstraints();
            c.gridx = 1;
            c.gridy = 2;
            f.getContentPane().add(new JButton("E"), c);        c = new GridBagConstraints();
            c.gridx = 1;
            c.gridy = 3;
            f.getContentPane().add(new JButton("E"), c);
             
    f.pack();
    f.show();
        }
    }
      

  6.   

    bovy(★ 天道酬勤 ★) 你的我试过了,好像不行,mail25(随风飘) 的可以。难道awt就实现不了吗?
    继续等待.....
      

  7.   

    把添加组件的方法改一下就好了
    import java.awt.*;public class test {
        
        public static void main(String args[]) {
     Frame f = new  Frame(" GridBagLayout Simples");
            GridBagConstraints c ;        f.setLayout(new GridBagLayout());        f.addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent evt) {
            System.exit(0);
                }
            });        c = new GridBagConstraints();
            c.gridx = 0;
            c.gridy = 0;
            c.gridheight = 4;
            c.fill = GridBagConstraints.VERTICAL;
            f.add(new  Button("A"), c);        c = new GridBagConstraints();
            c.gridx = 1;
            c.gridy = 0;
            f.add(new  Button("B"), c);        c = new GridBagConstraints();
            c.gridx = 1;
            c.gridy = 1;
            f.add(new  Button("C"), c);        c = new GridBagConstraints();
            c.gridx = 1;
            c.gridy = 2;
            f.add(new  Button("E"), c);        c = new GridBagConstraints();
            c.gridx = 1;
            c.gridy = 3;
            f.add(new  Button("E"), c);
             
    f.pack();
    f.show();
        }
    }