■■■    ■■■■■■■■■    ■■■■■■■■■    ■■■■■■请问我如何把六个控件排成以上的布局?(左三个一样宽,右三个一样宽,且不要紧挨着)把每个控件SetBounds的方法,我觉得太傻。用LayOut可以实现么?GridLayout实现的好象不满足我的要求,因为它总是使所有控件width相同,而且紧挨在一起。谢谢!!

解决方案 »

  1.   

    用borland的nullLayout,可以精确到象素
      

  2.   

    能具体给段code么?我刚学时间不长:(
      

  3.   

    用NETBEANS 5.0拖拽吧...
    我觉得没必要把精力放在排版上
    咱们又不是干美工的
      

  4.   

    ■■■    ■■■■■■
    flowlayout,borderlayou,boxlayou结合使用,然后设置间隔就行
      

  5.   

    import java.awt.BorderLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;public class T
    {
    public static void main(String[] args)
    {
    JLabel label1 = new JLabel("姓名:");
    JLabel label2 = new JLabel("出生日期:");
    JLabel label3 = new JLabel("地址:");

    JTextField textfield1 = new JTextField(10);
    JTextField textfield2 = new JTextField(10);
    JTextField textfield3 = new JTextField(10);

    JPanel p = new JPanel(new GridBagLayout());
    p.add(label1, new GridBagConstraints(
    0, 0, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, 
    new Insets(5, 5, 5, 5), 0, 0));
    p.add(textfield1, new GridBagConstraints(
    1, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 
    new Insets(5, 5, 5, 5), 0, 0)); p.add(label2, new GridBagConstraints(
    0, 1, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, 
    new Insets(0, 5, 5, 5), 0, 0));
    p.add(textfield2, new GridBagConstraints(
    1, 1, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 
    new Insets(0, 5, 5, 5), 0, 0)); p.add(label3, new GridBagConstraints(
    0, 2, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, 
    new Insets(0, 5, 5, 5), 0, 0));
    p.add(textfield3, new GridBagConstraints(
    1, 2, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 
    new Insets(0, 5, 5, 5), 0, 0)); JFrame f = new JFrame();
    f.getContentPane().add(p, BorderLayout.CENTER);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);

    }
    }
      

  6.   

    喜欢这样编码的!网格包很强大,不过熟练后还是应该用IDE吧。
      

  7.   

    用GridBagLayout,它比GridLayout灵活得多,可以设置行距、列间距,每行、每列都可以有不同的宽度,而且一个控件可以占据多个单元格,并且控件之间还可以设置间隙。不过,最好的是把BoxLayout、BorderLayout、GridBagLayout等组合使用,这样才能更灵活、更贴切地构建出你想要的布局。
      

  8.   

    用什么IDE并不是很重要,掌握各种Layout Manager的功能并灵活运用才是重点。