各位大虾,帮忙找找问题吧,我对了一天都没有想清楚原因//没有添加表格之前,GridBagLayout的分布就按照我的意思来分,2:3的布局import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import java.sql.SQLException;
import javax.swing.JToolBar;
import javax.swing.JLabel;public class Model_Main  {

private static final long serialVersionUID = 1L;
    private static Model_Main s=null;  //  @jve:decl-index=0:
public JScrollPane user_Scroll = null;  //  @jve:decl-index=0:visual-constraint="30,134"
public JTable Ggood_table = null;
public JFrame Ggood_frame=new JFrame("模具");  //  @jve:decl-index=0:visual-constraint="38,62"
int cols=0,rows=0,cols1=0,rows1=0;
//Dimension screensize=user_frame.getToolkit().getScreenSize();  //  @jve:decl-index=0:
String head[]={"模具编码","模具名称","规格","重量","单位","归属地","备注"};  //  @jve:decl-index=0:
String head1[]={"项目","颜色","材料","啤重","物料编码","物料名称","物料规格","单位","产出","净重","备注","归属","啤备注"};
String fix_head[]={"行号"};
Dimension screensize=Ggood_frame.getToolkit().getScreenSize();
Insets   screenInsets   =Ggood_frame.getToolkit().getScreenInsets(Ggood_frame.getGraphicsConfiguration()); 
Rectangle reg=new Rectangle(screenInsets.left,screenInsets.top,screensize.width-screenInsets.left-screenInsets.right,   
                            screensize.height - screenInsets.top-screenInsets.bottom);  //  @jve:decl-index=0:
//建立以user_frame作为父窗口,userpanel作为放置子窗口的容器
private JToolBar StatusBar = null;
private JLabel label_user = null;
private JLabel ll = null;
DefaultTableModel dtm1=null,fix_dtm1=null,fix_dtm2=null,dtm=null;//jtable的表格容器
private JScrollPane jScrollPane = null;  //  @jve:decl-index=0:visual-constraint="161,468"
    public JTable f_good_table = null;
private JTable fix_table1 = null;  //  @jve:decl-index=0:visual-constraint="76,136"
private JTable fix_table2 = null;  //  @jve:decl-index=0:visual-constraint="74,206"
//按钮字体
Font ft=new Font("SansSerif", Font.PLAIN,12);
//DefaultTableModel dtm
/**
 * This is the default constructor
 */
public Model_Main() throws SQLException {
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gc=new GridBagConstraints();
make_button(Ggood_frame,gbl,gc);
Ggood_frame.setLayout(gbl);
Ggood_frame.setSize(reg.width,reg.height);
Ggood_frame.setVisible(true);
Ggood_frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
//添加按钮菜单
public void make_button(JFrame jframe,GridBagLayout gbl,GridBagConstraints gc){
gc.fill=GridBagConstraints.BOTH;//在水平和垂直方向同时调整控件的大小
    //添加JPanel控件
    JPanel userpanel=new JPanel ();
            gc.weighty=1;
            gc.weightx=1;
    gbl.setConstraints(userpanel,gc);
    jframe.add(userpanel);
    //userpanel的另外的网格布袋布局
GridBagLayout gbl1=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
gbc.fill=GridBagConstraints.BOTH;
gbc.weightx=1;
gbc.weighty=1;
userpanel.setLayout(gbl1);
add_table(userpanel,gbl1,gbc);

}
//添加表格
private void add_table(JPanel jpanel,GridBagLayout gbl,GridBagConstraints gcs){
//第一个面板
gcs.fill=GridBagConstraints.BOTH;
gcs.weightx=2;
gcs.weighty=11;
getUser_Scroll();
gbl.setConstraints(user_Scroll, gcs);
jpanel.add(user_Scroll);
gcs.weightx=3;
gcs.gridwidth=GridBagConstraints.REMAINDER;
getJScrollPane();
gbl.setConstraints(jScrollPane,gcs);
jpanel.add(jScrollPane);


} /**
 * This method initializes user_Scroll
 * 
 * @return javax.swing.JScrollPane
 */
private JScrollPane getUser_Scroll() {
if (user_Scroll == null) {
user_Scroll = new JScrollPane();
                  }
return user_Scroll;
} /**
 * This method initializes Ggood_table
 * 
 * @return javax.swing.JTable
 */
private JTable getGgood_table() {
int j=0,k=0;
if (Ggood_table == null) {
rows=0;
String data[][]=new String[rows][cols];
String data1[][]=new String[rows][fix_head.length];
dtm=new DefaultTableModel(data,head);
 fix_dtm1=new DefaultTableModel(data1,fix_head);
//设置表格不可编辑
Ggood_table=new JTable(dtm){
public boolean isCellEditable(int col,int row){
return (col==-1);

}
};
Ggood_table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
Ggood_table.getColumn("模具编码").setPreferredWidth(110);
Ggood_table.getColumn("规格").setPreferredWidth(150);
Ggood_table.getTableHeader().setReorderingAllowed(false);
}
return Ggood_table;
}
//模具表格与固定列同步
  public void checkSelection(boolean isFixedTable){
    int fixedSelectedIndex = fix_table1.getSelectedRow();
        int selectedIndex =Ggood_table.getSelectedRow();
        if(fixedSelectedIndex ==-1){
         fixedSelectedIndex =0;
        }
        if(selectedIndex ==-1){
         selectedIndex =0;
        }
        if (fixedSelectedIndex != selectedIndex) {
            if (isFixedTable) {
             Ggood_table.setRowSelectionInterval(fixedSelectedIndex,
                        fixedSelectedIndex);
            } else {
               fix_table1
                        .setRowSelectionInterval(selectedIndex, selectedIndex);
            }
        }
  }
  //物料表格同步
  public void checkSelection1(boolean isFixedTable){
    int fixedSelectedIndex = fix_table2.getSelectedRow();
        int selectedIndex =f_good_table.getSelectedRow();
        if(fixedSelectedIndex ==-1){
         fixedSelectedIndex =0;
        }
        if(selectedIndex ==-1){
         selectedIndex =0;
        }
        if (fixedSelectedIndex != selectedIndex) {
            if (isFixedTable) {
             f_good_table.setRowSelectionInterval(fixedSelectedIndex,
                        fixedSelectedIndex);
            } else {
               fix_table2
                        .setRowSelectionInterval(selectedIndex, selectedIndex);
            }
        }
  }
synchronized public static Model_Main getInstance(){
if(s==null){
try {
s=new Model_Main();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
s.Ggood_frame.toFront();//窗口获得焦点
//窗口最小化,则复原

if (s.Ggood_frame.getExtendedState() == JFrame.ICONIFIED) { 
s.Ggood_frame.setExtendedState(JFrame.NORMAL); 
}
}
return s;

} private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane(); }
return jScrollPane;
} /**
 * This method initializes f_good_table
 * 
 * @return javax.swing.JTable
 */
private JTable getF_good_table() {
if (f_good_table == null) {
rows=0;
String data[][]=new String[rows][head1.length];
String data1[][]=new String[rows][fix_head.length];
dtm1=new DefaultTableModel(data,head1);
fix_dtm2=new DefaultTableModel(data1,fix_head);
f_good_table = new JTable(dtm1){
public boolean isCellEditable(int col,int row){
return (col==-1);

}
};
f_good_table.getColumn("物料编码").setPreferredWidth(90);

f_good_table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
f_good_table.getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {
                    public void valueChanged(ListSelectionEvent arg0) {
                        checkSelection1(false);
                    }
                });
/*}catch(SQLException e1){
e1.printStackTrace();
}*/

}
return f_good_table;
}
private JTable getFix_table1(DefaultTableModel dtm) {
if (fix_table1 == null) {
fix_table1 = new JTable(dtm);
fix_table1.setSize(new Dimension(137, 47));
fix_table1.getColumn("行号").setPreferredWidth(38);
fix_table1.getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {
                    public void valueChanged(ListSelectionEvent arg0) {
                        checkSelection(true);
                    }
                });
}
return fix_table1;
} /**
 * This method initializes fix_table2
 * 
 * @return javax.swing.JTable
 */
private JTable getFix_table2(DefaultTableModel dtm) {
if (fix_table2 == null) {
fix_table2 = new JTable(dtm);
fix_table2.setSize(new Dimension(161, 54));
fix_table2.getColumn("行号").setPreferredWidth(38);
fix_table2.getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {
                    public void valueChanged(ListSelectionEvent arg0) {
                        checkSelection1(true);
                    }
                });
}
return fix_table2;
}
public static void main(String args[]){
 try {
new Model_Main();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}

解决方案 »

  1.   

    //添加表格之后,GridBagLayout就变成对等分了。import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.Rectangle;
    import javax.swing.JTable;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    import javax.swing.table.DefaultTableModel;
    import java.sql.SQLException;
    import javax.swing.JToolBar;
    import javax.swing.JLabel;public class Model_Main  {

    private static final long serialVersionUID = 1L;
        private static Model_Main s=null;  //  @jve:decl-index=0:
    public JScrollPane user_Scroll = null;  //  @jve:decl-index=0:visual-constraint="30,134"
    public JTable Ggood_table = null;
    public JFrame Ggood_frame=new JFrame("模具");  //  @jve:decl-index=0:visual-constraint="38,62"
    int cols=0,rows=0,cols1=0,rows1=0;
    //Dimension screensize=user_frame.getToolkit().getScreenSize();  //  @jve:decl-index=0:
    String head[]={"模具编码","模具名称","规格","重量","单位","归属地","备注"};  //  @jve:decl-index=0:
    String head1[]={"项目","颜色","材料","啤重","物料编码","物料名称","物料规格","单位","产出","净重","备注","归属","啤备注"};
    String fix_head[]={"行号"};
    Dimension screensize=Ggood_frame.getToolkit().getScreenSize();
    Insets   screenInsets   =Ggood_frame.getToolkit().getScreenInsets(Ggood_frame.getGraphicsConfiguration()); 
    Rectangle reg=new Rectangle(screenInsets.left,screenInsets.top,screensize.width-screenInsets.left-screenInsets.right,   
                                screensize.height - screenInsets.top-screenInsets.bottom);  //  @jve:decl-index=0:
    //建立以user_frame作为父窗口,userpanel作为放置子窗口的容器
    private JToolBar StatusBar = null;
    private JLabel label_user = null;
    private JLabel ll = null;
    DefaultTableModel dtm1=null,fix_dtm1=null,fix_dtm2=null,dtm=null;//jtable的表格容器
    private JScrollPane jScrollPane = null;  //  @jve:decl-index=0:visual-constraint="161,468"
        public JTable f_good_table = null;
    private JTable fix_table1 = null;  //  @jve:decl-index=0:visual-constraint="76,136"
    private JTable fix_table2 = null;  //  @jve:decl-index=0:visual-constraint="74,206"
    //按钮字体
    Font ft=new Font("SansSerif", Font.PLAIN,12);
    //DefaultTableModel dtm
    /**
     * This is the default constructor
     */
    public Model_Main() throws SQLException {
    GridBagLayout gbl=new GridBagLayout();
    GridBagConstraints gc=new GridBagConstraints();
    make_button(Ggood_frame,gbl,gc);
    Ggood_frame.setLayout(gbl);
    Ggood_frame.setSize(reg.width,reg.height);
    Ggood_frame.setVisible(true);
    Ggood_frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
    //添加按钮菜单
    public void make_button(JFrame jframe,GridBagLayout gbl,GridBagConstraints gc){
    gc.fill=GridBagConstraints.BOTH;//在水平和垂直方向同时调整控件的大小
        //添加JPanel控件
        JPanel userpanel=new JPanel ();
                gc.weighty=1;
                gc.weightx=1;
        gbl.setConstraints(userpanel,gc);
        jframe.add(userpanel);
        //userpanel的另外的网格布袋布局
    GridBagLayout gbl1=new GridBagLayout();
    GridBagConstraints gbc=new GridBagConstraints();
    gbc.fill=GridBagConstraints.BOTH;
    gbc.weightx=1;
    gbc.weighty=1;
    userpanel.setLayout(gbl1);
    add_table(userpanel,gbl1,gbc);

    }
    //添加表格
    private void add_table(JPanel jpanel,GridBagLayout gbl,GridBagConstraints gcs){
    //第一个面板
    gcs.fill=GridBagConstraints.BOTH;
    gcs.weightx=2;
    gcs.weighty=11;
    getUser_Scroll();
    gbl.setConstraints(user_Scroll, gcs);
    jpanel.add(user_Scroll);
    gcs.weightx=3;
    gcs.gridwidth=GridBagConstraints.REMAINDER;
    getJScrollPane();
    gbl.setConstraints(jScrollPane,gcs);
    jpanel.add(jScrollPane);


    } /**
     * This method initializes user_Scroll
     * 
     * @return javax.swing.JScrollPane
     */
    private JScrollPane getUser_Scroll() {
    if (user_Scroll == null) {
    user_Scroll = new JScrollPane();
    //添加表格
    user_Scroll.setViewportView(getGgood_table());
    JViewport viewport=new JViewport();
    viewport.setView(getFix_table1(fix_dtm1));
    viewport.setPreferredSize(fix_table1.getPreferredSize());
    user_Scroll.setRowHeader(viewport);
    user_Scroll.setCorner(JScrollPane.UPPER_LEADING_CORNER,fix_table1.getTableHeader());
      }
    return user_Scroll;
    } /**
     * This method initializes Ggood_table
     * 
     * @return javax.swing.JTable
     */
    private JTable getGgood_table() {
    int j=0,k=0;
    if (Ggood_table == null) {
    rows=0;
    String data[][]=new String[rows][cols];
    String data1[][]=new String[rows][fix_head.length];
    dtm=new DefaultTableModel(data,head);
     fix_dtm1=new DefaultTableModel(data1,fix_head);
    //设置表格不可编辑
    Ggood_table=new JTable(dtm){
    public boolean isCellEditable(int col,int row){
    return (col==-1);

    }
    };
    Ggood_table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    Ggood_table.getColumn("模具编码").setPreferredWidth(110);
    Ggood_table.getColumn("规格").setPreferredWidth(150);
    Ggood_table.getTableHeader().setReorderingAllowed(false);
    }
    return Ggood_table;
    }
    //模具表格与固定列同步
      public void checkSelection(boolean isFixedTable){
        int fixedSelectedIndex = fix_table1.getSelectedRow();
            int selectedIndex =Ggood_table.getSelectedRow();
            if(fixedSelectedIndex ==-1){
             fixedSelectedIndex =0;
            }
            if(selectedIndex ==-1){
             selectedIndex =0;
            }
            if (fixedSelectedIndex != selectedIndex) {
                if (isFixedTable) {
                 Ggood_table.setRowSelectionInterval(fixedSelectedIndex,
                            fixedSelectedIndex);
                } else {
                   fix_table1
                            .setRowSelectionInterval(selectedIndex, selectedIndex);
                }
            }
      }
      //物料表格同步
      public void checkSelection1(boolean isFixedTable){
        int fixedSelectedIndex = fix_table2.getSelectedRow();
            int selectedIndex =f_good_table.getSelectedRow();
            if(fixedSelectedIndex ==-1){
             fixedSelectedIndex =0;
            }
            if(selectedIndex ==-1){
             selectedIndex =0;
            }
            if (fixedSelectedIndex != selectedIndex) {
                if (isFixedTable) {
                 f_good_table.setRowSelectionInterval(fixedSelectedIndex,
                            fixedSelectedIndex);
                } else {
                   fix_table2
                            .setRowSelectionInterval(selectedIndex, selectedIndex);
                }
            }
      }
    synchronized public static Model_Main getInstance(){
    if(s==null){
    try {
    s=new Model_Main();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    else{
    s.Ggood_frame.toFront();//窗口获得焦点
    //窗口最小化,则复原

    if (s.Ggood_frame.getExtendedState() == JFrame.ICONIFIED) { 
    s.Ggood_frame.setExtendedState(JFrame.NORMAL); 
    }
    }
    return s;

    } private JScrollPane getJScrollPane() {
    if (jScrollPane == null) {
    jScrollPane = new JScrollPane();
    //添加表格
    jScrollPane.setViewportView(getF_good_table());
    JViewport viewport=new JViewport();
    viewport.setView(getFix_table2(fix_dtm2));
    viewport.setPreferredSize(fix_table2.getPreferredSize());
    jScrollPane.setRowHeader(viewport);
    jScrollPane.setCorner(JScrollPane.UPPER_LEADING_CORNER,fix_table2.getTableHeader());
      
    }
    return jScrollPane;
    } /**
     * This method initializes f_good_table
     * 
     * @return javax.swing.JTable
     */
    private JTable getF_good_table() {
    if (f_good_table == null) {
    rows=0;
    String data[][]=new String[rows][head1.length];
    String data1[][]=new String[rows][fix_head.length];
    dtm1=new DefaultTableModel(data,head1);
    fix_dtm2=new DefaultTableModel(data1,fix_head);
    f_good_table = new JTable(dtm1){
    public boolean isCellEditable(int col,int row){
    return (col==-1);

    }
    };
    f_good_table.getColumn("物料编码").setPreferredWidth(90);

    f_good_table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    f_good_table.getSelectionModel().addListSelectionListener(
                    new ListSelectionListener() {
                        public void valueChanged(ListSelectionEvent arg0) {
                            checkSelection1(false);
                        }
                    });
    /*}catch(SQLException e1){
    e1.printStackTrace();
    }*/

    }
    return f_good_table;
    }
    private JTable getFix_table1(DefaultTableModel dtm) {
    if (fix_table1 == null) {
    fix_table1 = new JTable(dtm);
    fix_table1.setSize(new Dimension(137, 47));
    fix_table1.getColumn("行号").setPreferredWidth(38);
    fix_table1.getSelectionModel().addListSelectionListener(
                    new ListSelectionListener() {
                        public void valueChanged(ListSelectionEvent arg0) {
                            checkSelection(true);
                        }
                    });
    }
    return fix_table1;
    } /**
     * This method initializes fix_table2
     * 
     * @return javax.swing.JTable
     */
    private JTable getFix_table2(DefaultTableModel dtm) {
    if (fix_table2 == null) {
    fix_table2 = new JTable(dtm);
    fix_table2.setSize(new Dimension(161, 54));
    fix_table2.getColumn("行号").setPreferredWidth(38);
    fix_table2.getSelectionModel().addListSelectionListener(
                    new ListSelectionListener() {
                        public void valueChanged(ListSelectionEvent arg0) {
                            checkSelection1(true);
                        }
                    });
    }
    return fix_table2;
    }
    public static void main(String args[]){
     try {
    new Model_Main();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }}
      

  2.   

    在表格布局最近gridData.horizontalSpan =3; //水平占三个格gridData.verticalSpan = 3; //水平占三个格 看你的需要了 可以解决你的问题的
      

  3.   

    java的布局比较麻烦.它是根据所包含的容器以及相互之间关系来显示的.
    需要仔细调试.
      

  4.   

    hytdsky,你说的是SWT,我是用SWING。SWING的GridBagLayout的布局