GridBagLayout的Swing错误

时间:2012-02-16 03:32:08

标签: java swing gridbaglayout

我正在使用gridbag布局管理器编写一个mutlientry对话框,当我试图运行这个程序时,它向我展示了一些我从未遇到过的奇怪错误。

 public class MultiEntryDlg extends JDialog {
    private static final long serialVersionUID = 1L;
   private final JPanel             contentPanel    = new JPanel() ;
   private DButton          pb_OK                   = null ;  
   private DButton          pb_CANCEL               = null ;  
   private JLabel           lb_Name                 = null ;
   private JTextField       ef_name                 = null ;
   private JLabel           lb_taxonomyValue        = null;
   private JTextField       ef_taxonomyValue        = null ;
   private JLabel           lb_subscriptionKey      = null ;
   private JTextField       ef_subscriptionKey      = null ;
   private JCheckBox        cb_subscribable         = null ;
   private JTextField       ef_alias                = null ;
   private JLabel           lb_alias                = null ;
   private JLabel           lb_orderNumber          = null ;
   private JTextField       ef_orderNum             = null ;
   private JTextAreaCounter mle_DESCRIPTION_COUNTER = null ;               
   private SpellCheckPanel  spellCheckPanel         = null ;
   private JLabel    lb_contentLocation             = null ;
   private JComboBox contentlocCombo                = null ;
   private JLabel    lb_brandSubGrp                 = null ;
   private JComboBox brandSubGrpCombo               = null ;
   private JLabel    lb_fixedLocation               = null ;
   private JComboBox fixedlocCombo                  = null ;
   private JLabel    lb_taxonomyType                = null ;
   private JComboBox taxonomyCombo                  = null ;
   private NoTabJTextArea mle_DESCRIPTION           = null ;

   private MultiEntryDlg(){
       creatAndShowGUI();
   }

   public void actionPerformed ( ActionEvent e ) {

      if ( ( e.getSource() == pb_OK )
          dispose();
      else if (e.getSource() ==pb_CANCEL)
          dispose();
   }
   private void creatAndShowGUI(){
       initializeBasicProperty();
       addComponents();
   }

private void addComponents() {
    // TODO Auto-generated method stub
    lb_Name = new JLabel("Name (Max 75 char)");
    addLeftAlign(lb_Name, 0, 1);
    ef_name = new JTextField(10);
    addMergeColumn(ef_name, 1, 1, 2);
    spellCheckPanel = new SpellCheckPanel();
    spellCheckPanel.setTextComponent(ef_name);
    addCenterAlign(spellCheckPanel, 3, 1);
    lb_alias = new JLabel("Alias");
    addLeftAlign(lb_alias, 0, 2);
    ef_alias = new JTextField(10);
    addMergeColumn(ef_alias, 1, 2, 3);
    lb_orderNumber = new JLabel("Order Number");
    addLeftAlign(lb_orderNumber, 0, 3);
    ef_orderNum = new JTextField(10);
    addCenterAlign(ef_orderNum, 1, 3);
    lb_contentLocation = new JLabel("Content Location");
    addCenterAlign(lb_contentLocation, 2, 3);
    contentlocCombo = new JComboBox();
    addCenterAlign(contentlocCombo, 3, 3);
    lb_brandSubGrp = new JLabel("Brand Sub Group");
    addLeftAlign(lb_brandSubGrp, 0, 4);
    brandSubGrpCombo = new JComboBox();
    addCenterAlign(brandSubGrpCombo, 1, 4);
    lb_fixedLocation = new JLabel("Fixed Location");
    addLeftAlign(lb_fixedLocation, 2, 4);
    fixedlocCombo  = new JComboBox();
    addCenterAlign(fixedlocCombo, 3, 4);
    lb_taxonomyValue = new JLabel("Taxonomy Value");
    addLeftAlign(lb_taxonomyValue, 0, 5);
    ef_taxonomyValue = new JTextField(10);
    addCenterAlign(ef_taxonomyValue, 1, 5);
    lb_taxonomyType  = new JLabel ("Taxonomy Type");
    addLeftAlign(lb_taxonomyType, 2, 5);
    taxonomyCombo = new JComboBox();
    addCenterAlign(taxonomyCombo, 3, 5);
    cb_subscribable = new JCheckBox("Subscribable");
    addLeftAlign(cb_subscribable, 0, 6);
    lb_subscriptionKey = new JLabel("Subscription Key");
    addCenterAlign(lb_Name, 2, 6);
    ef_subscriptionKey = new JTextField(10);
    addCenterAlign(ef_subscriptionKey, 3, 6);
    addControlButton();
}

public static void main(String[] args) {
    try {
        MultiEntryDlg dialog = new MultiEntryDlg();
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void addControlButton() {
    // TODO Auto-generated method stub
    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    getContentPane().add(buttonPanel,BorderLayout.SOUTH);
    pb_OK = new DButton(Str.getStr(STR_OK));
    pb_CANCEL = new DButton(Str.getStr(STR_CANCEL));

    pb_OK.addActionListener(this);
    pb_CANCEL.addActionListener(this);

    buttonPanel.add(pb_OK);
    buttonPanel.add(pb_CANCEL);

}

private void addLeftAlign (Component component , int x, int y){
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.insets = new Insets(0, 0, 5, 5);
    constraints.anchor = GridBagConstraints.WEST;
    constraints.gridx = x;
    constraints.gridy = y;
    contentPanel.add(component, constraints);

}

private void addCenterAlign (Component component , int x ,int y){
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.insets = new Insets(0, 0, 5, 0);
    constraints.anchor = GridBagConstraints.HORIZONTAL;
    constraints.gridx = x;
    constraints.gridy = y;
    contentPanel.add(component, constraints);
}

private void addMergeColumn (Component component , int x, int y, int weight){
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.insets = new Insets(0, 0, 5, 5);
    constraints.anchor = GridBagConstraints.HORIZONTAL;
    constraints.gridx = x;
    constraints.gridy = y;
    constraints.gridwidth = weight;
    contentPanel.add(component, constraints);
}


private void initializeBasicProperty() {
    // TODO Auto-generated method stub
    setBounds(100, 100, 500, 250);
    getContentPane().setLayout(new BorderLayout());
    contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    getContentPane().add(contentPanel, BorderLayout.CENTER);    
    GridBagLayout gbl_contentPanel = new GridBagLayout();
    gbl_contentPanel.columnWidths = new int[]{0, 0, 0};
    gbl_contentPanel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0};
    gbl_contentPanel.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
    gbl_contentPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    contentPanel.setLayout(gbl_contentPanel);
}
} 

我收到了以下错误。

strArray in strObject is null. String Index:1  Language Ind:0
strArray in strObject is null. String Index:2  Language Ind:0
Exception in thread "main" java.lang.StackOverflowError
at com.ibm.nzna.projects.qit.gui.MultiEntryDlg.setVisible(MultiEntryDlg.java:523)
at com.ibm.nzna.projects.qit.gui.MultiEntryDlg.setVisible(MultiEntryDlg.java:523)
at com.ibm.nzna.projects.qit.gui.MultiEntryDlg.setVisible(MultiEntryDlg.java:523)
    etc..

有人可以帮我解决这个错误吗?

1 个答案:

答案 0 :(得分:0)

这不能直接回答您的问题。但是真的不要使用GridBagLayout ..它是一个疯狂的布局管理器。查看编写组件所需的代码量。 IMO,这就是为什么许多人不去写Swing应用程序的原因。

而是使用jgoodies表单布局。这是:http://www.jgoodies.com/freeware/forms/

如果您还没有听说过它...它是一个非常简单的布局管理器。您可以将列和行规范指定为简单字符串。您可以在库中使用现有的PanelBuider或FormBuilder,也可以自己创建一个。我在这里做了一个:

http://code.google.com/p/swingobjects/source/browse/SwingObjects/src/org/aesthete/swingobjects/view/SwingObjFormBuilder.java