GridBagLayout 的 Swing 错误

发布于 2025-01-06 15:02:28 字数 6947 浏览 0 评论 0原文

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

 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..

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

I am writing a mutlientry dialog box using the gridbag layout manager, when I am trying to run this program it is showing me some weird error which I never encountered.

 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);
}
} 

I am getting following error.

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..

Could anyone help me with this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七禾 2025-01-13 15:02:29

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

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

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

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

This doesn't answer your question directly. But really don't use GridBagLayout.. its a crazy layoutmanager. Look at the amount of code you need to write to layout your components. IMO, thats the reason why many people moved away from not writing Swing application.

Instead use the jgoodies form layout. Here it is: http://www.jgoodies.com/freeware/forms/

If you haven't heard about it.. its a very simple layout manager. You can specify the column and row specs as simple strings. You can use existing PanelBuider or FormBuilder in the library or make one of your own. I have made one here:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文