加在JScrollPane上面的一行就跑到右边去了。户籍、住所这一行就跑到界面的右边去了。界面变的很大,代码如下:package test;import javax.swing.UIManager;
import java.awt.*;
import javax.swing.*;
import java.util.Vector;
public class student1 {
  boolean packFrame = false;
  void buildConstraints(GridBagConstraints gbc, int gx, int gy,
          int gw, int gh, int wx, int wy) {          gbc.gridx = gx;
          gbc.gridy = gy;
          gbc.gridwidth = gw;
          gbc.gridheight = gh;
          gbc.weightx = wx;
          gbc.weighty = wy;
      }  //Construct the application
  public student1() {
    JFrame f=new JFrame();
    Container contentPane=f.getContentPane();
    contentPane.setLayout(new GridLayout(2,1)) ;    JPanel p1=new JPanel();
    JLabel La=new JLabel("姓名");
    JTextField jt=new JTextField(10);
    JLabel La1=new JLabel("出生日期");
    JTextField jt1=new JTextField("2005-12-12",10);
    String[] s={"上海公司","北京公司"};
    JComboBox combol=new JComboBox(s);
    combol.addItem("广州公司") ;
    JLabel la2=new JLabel("一级公司");    String[] s1={"上海公司","北京公司"};
   JComboBox combol1=new JComboBox(s);
   combol1.addItem("广州公司") ;
   JLabel la3=new JLabel("二三级公司");
   JLabel la4=new JLabel("性别");
   JTextField jt4=new JTextField(5);
    //combol.setBorder(BorderFactory.createTitledBorder("一级公司") ) ;
    p1.add(La);
    p1.add(jt);
    p1.add(La1);
    p1.add(jt1);
    p1.add(la2);
    p1.add(combol);
    p1.add(la3);
    p1.add(combol1);
    p1.add(la4);
    p1.add(jt4);
    p1.setLayout(new FlowLayout(10,2,2));
    contentPane.add(p1);
    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    JPanel p2=new JPanel();    buildConstraints(constraints, 0, 0, 1, 1, 5, 100);
   constraints.fill = GridBagConstraints.NONE;
   constraints.anchor = GridBagConstraints.WEST;
    JLabel La5=new JLabel("户籍");
    gridbag.setConstraints(La5, constraints);
   // La5.setHorizontalAlignment(JLabel.LEFT );   buildConstraints(constraints, 1, 0, 1, 1, 45, 100);
   constraints.anchor = GridBagConstraints.WEST;
   constraints.fill = GridBagConstraints.HORIZONTAL;
   JTextField jt5=new JTextField(15);
  gridbag.setConstraints(jt5, constraints);
    //jt5.setHorizontalAlignment(JTextField.LEFT ) ;    buildConstraints(constraints, 2, 0, 1, 1, 5, 100);
    constraints.fill = GridBagConstraints.NONE;
    constraints.anchor = GridBagConstraints.WEST;
   JLabel La6=new JLabel("住所");
   gridbag.setConstraints(La6, constraints);
   //La6.setHorizontalAlignment(JLabel.LEFT );   buildConstraints(constraints, 3, 0, 1, 1,45, 100);
  constraints.fill = GridBagConstraints.HORIZONTAL;
  constraints.anchor = GridBagConstraints.WEST;
  JTextField jt6=new JTextField(15);
     gridbag.setConstraints(jt6, constraints);
  //  jt6.setHorizontalAlignment(JTextField.LEFT ) ;   // buildConstraints(constraints, 0, 1,4, 1,0, 100);
   // constraints.fill = GridBagConstraints.HORIZONTAL;
    //constraints.anchor = GridBagConstraints.WEST;
    //JButton jb=new JButton("查询");
    //gridbag.setConstraints(jb, constraints);    p2.add(La5);
    p2.add(jt5);
    p2.add(La6);
    p2.add(jt6);
    //p2.add(jb);
    p2.setLayout(gridbag) ;
    contentPane.add(p2) ;    //JPanel p3=new JPanel();
    //JButton jb=new JButton("查询");
    //p3.add(jb) ;
    Object[][] playerinfo={{"测试1",new Integer(55)},{"测试2",new Integer(60)}};
    String[] Names={"姓名","成绩"};
    JTable table=new JTable(playerinfo,Names);
    table.setPreferredScrollableViewportSize(new Dimension(400,100));
    JScrollPane jp=new JScrollPane(table);
    jp.setSize(400,100) ;    //p3.setLayout(new FlowLayout(10,2,2));
    contentPane.add(jp);    f.setTitle("学生管理") ;
    f.setSize(600,300) ;
    f.pack();
    f.setVisible(true) ;  }
  //Main method
  public static void main(String[] args) {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    new student1();
  }
}

解决方案 »

  1.   

    不理解你为什么要用GridBagLayout
    使用FlowLayout,BorderLayout以及null Layout三者相互嵌套就可以设计出所有的界面。
      

  2.   

    不理解为什么不用GridBagLayout???
    我写Swing程序只需要GridBagLayout就可以设计出所有的界面,反而不理解你什么要使用FlowLayout,BorderLayout以及null Layout三者相互嵌套?这不是更罗嗦吗?
    ==================================================================
    楼猪的程序犯了个严重低级的错误(可以打自己耳光):
    contentPane.setLayout(new GridLayout(2,1)) ;
    你明明加了3个JPanel在contentPane上,怎么只给了GridLayout 2 列?
    改成:
    contentPane.setLayout(new GridLayout(3,1)) ;
    万事大吉。
      

  3.   

    stevech(无心的诺言) 谢谢我确实太初级了