jpanel的布局为GridBagLayout,但其中的组件有时会不明原因的缩成一团。包括jlabel,jtextfield,jtable,jbutton等。
经多次修改,感觉好像是只要其中一个jtextfield的值(是字符串)如果超过一定长度,就会出现上述情况。
困扰好几天了,期待各位的帮助!

解决方案 »

  1.   

    这样试试看  比如JPanel buttonPanel = new JPanel(new GridLayout(8, 1));这样会自动调剂大小的
      

  2.   

    public GysXiuGaiPanel() {
    super();
    setLayout(new GridBagLayout());
    setBounds(50, 50, 700, 700); setupComponet(new JLabel("资料编号:"), 0, 0, 1, 0, false);
    bianhao= new JComboBox();
    setupComponet(bianhao, 1, 0, 2, 50, true);
    initComboBox();
    bianhao.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    doKeHuSelectAction();
    }
    });


    setupComponet(new JLabel("资料名称:"), 3, 0, 1, 0, false);
    lian= new JTextField();
    lian.setFocusable(false);
    setupComponet(lian, 4, 0, 2, 0, true);
    setupComponet(new JLabel("请先勾选:"), 0, 1, 6, 0, true);


    table = new JTable();
    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    initTable();
    JScrollPane scrollPanel = new JScrollPane(table);
    scrollPanel.setAutoscrolls(true);
    scrollPanel.setPreferredSize(new Dimension(700, 200));
    setupComponet(scrollPanel, 0, 2, 6, 1, true);


                    JButton rkButton = new JButton("修改");
    rkButton.addActionListener(new RkActionListener());
    setupComponet(rkButton, 4, 4, 1, 1, false);

    JButton tjButton = new JButton("删除");
    tjButton.addActionListener(new TjActionListener());
    setupComponet(tjButton, 5, 4, 1, 1, false);
    }
    // 初始化表格
    public void initTable() {
    String[] columnNames = {"状态", "文件编号","文件名称", "文件页码", "文件时间", "备注"};
    ((DefaultTableModel) table.getModel())
    .setColumnIdentifiers(columnNames);

    //final DefaultTableModel dftm = (DefaultTableModel) table.getModel();
    DefaultTableModel   dftm   =   new   DefaultTableModel()   {   
      public   Class   getColumnClass(int   columnIndex)   
      {   
      return   columnIndex   ==   0   ?   Boolean.class   :   String.class;   
      } 
      public   boolean   isCellEditable(int   rowindex,int   colindex){   
                      if   (colindex==1)   return   false;       //设置第二列只读   
                      return   true;                                           //其他列可以修改   
      }   
      };   
      table.setModel(dftm);
    dftm.setColumnIdentifiers(columnNames);
    }// 设置组件位置并添加到容器中
    private void setupComponet(JComponent component, int gridx, int gridy,
    int gridwidth, int ipadx, boolean fill) {
    final GridBagConstraints gridBagConstrains = new GridBagConstraints();
    gridBagConstrains.gridx = gridx;
    gridBagConstrains.gridy = gridy;
    if (gridwidth > 1)
    gridBagConstrains.gridwidth = gridwidth;
    if (ipadx > 0)
    gridBagConstrains.ipadx = ipadx;
    gridBagConstrains.insets = new Insets(5, 1, 3, 1);
    if (fill)
    gridBagConstrains.fill = GridBagConstraints.HORIZONTAL;
    add(component, gridBagConstrains);
    }就是我划横线的lian(jtextfield),它的长度超过一定的值就发生上述情况。感谢各位了!
      

  3.   

    自己结了吧,找到答案了。
    We encourage you to specify the number of columns for each text field. If you don't specify the number of columns or a preferred size, then the field's preferred size changes whenever the text changes, which can result in unwanted layout updates