在java中运行JFrame时出现StackOverflowError

时间:2013-01-04 22:45:13

标签: java stack-overflow

我在java中编写了一个JFrame(framePanel)但我在运行它时遇到了StackOverflowError,或者通过实例化这个类的对象并调用方法'create_ShowGUI()'来从另一个类运行它。

基本上我想要在调用main()方法时调用initComponents()方法来初始化我的组件(设置单选按钮,对它们进行分组,将Actionlistener添加到所有单选按钮,创建JPanel(menuFrame)&添加它的按钮)。在initComponents()完成其工作后,JFrame被初始化,JPanel被添加到它和&最后显示框架。

你能帮我解决一下这个错误吗?

编辑:我使用NetBeans GUI Builder工具创建了各种应用程序。这是我第一次尝试从头开始做这件事。因此,如果不清楚的话,请原谅。

编辑:使用评论更新了代码

编辑:顺便说一下我用netbeans编码

代码从这里开始:

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

/**
 *
 * @author jtech
 * @version 1.0.0
 * 
 * 'c' loop variable is initialized to 1
 * 'c1' loop variable is initialized to 0
 */
public class BankMainMenuFrame 
{      

//    public void BankMainMenuFrame()
//    {                
//        initComponents(); 
//        create_ShowGUI();
//    }

    private void initComponents()
    {
//        JRadioButton defaultOpt = new JRadioButton();
//        defaultOpt.setText(bankMainMenuOpts[0]);
//        defaultOpt.setMnemonic(0x30);
//        defaultOpt.setActionCommand("Default");
//        defaultOpt.setSelected(true);

        //initializes the first button aside the loop that initializes others (default)
        aJB[0] = new JRadioButton();
        aJB[0].setText(bankMainMenuOpts[0]);
        aJB[0].setMnemonic(KeyEvent.VK_0);
        aJB[0].setSelected(true);
        aJB[0].setActionCommand("Default");
        aJB[0].addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent evt)
            {
                aJBActionPerformed(evt, c);
            }
        });

        //the other buttons are initialized here
        while (c < bankMainMenuOpts.length)
        {
            aJB[c] = new JRadioButton();
            aJB[c].setText(bankMainMenuOpts[c]);
            aJB[c].setMnemonic((vK_nums + c));
            aJB[c].setActionCommand(aJB[c].getText());   
            groupRadioButtons.add(aJB[c]);

            aJB[c].addActionListener(new ActionListener() 
            {
                @Override
                public void actionPerformed(ActionEvent evt)
                {
                    aJBActionPerformed(evt, c);
                }
            });

            c++;
        }

        framePanel = new JPanel(new GridLayout(0, 1));

        //buttons will be added to the panel one by one
        while (c1 < bankMainMenuOpts.length)
        {
            framePanel.add(aJB[c1]);

            c1++;
        }

    }

//other methods go here; like 'private void defaultOptActionPerformed(java.awt.event.ActionEvent evt) {}

    public void create_ShowGUI()
    {        
        initComponents();

        menuFrame = new JFrame("Bank Main Manu Program");
        menuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        menuFrame.add(framePanel); //framePanel is added to menuFrame
        menuFrame.setContentPane(new Container());
        menuFrame.pack();
        menuFrame.setVisible(true);
    }

    public void aJBActionPerformed(ActionEvent e, int optionNum)
    {
      //  aCA_MainMenuFrame.executeSelectedMenuOption(); 
        //calls a method which is in another class & that class isn't posted here due to its large 
       //amount of code         
    }

    public static void main(String[] args)
    {

                //instance of this class is created to call create_ShowGUI() method
                new BankMainMenuFrame().create_ShowGUI();

    }

    //<editor-fold defaultstate="collapsed" desc="variables decrlarations">
    private static char vK_nums = 0x30;
    private static int c = 1, c1 = 0;
    private static String[] bankMainMenuOpts = {"Select an option..",     //[0] (pre-selected)
        "Deposit Money",                                                  //[1]
        "Withdraw Money",                                                 //[2]
        "Display Balance",                                                //[3]
        "Exit Bank Account Program"};                                     //[4]

    private static JFrame menuFrame = null;
    private static JPanel framePanel = null;
    private static JRadioButton[] aJB = new JRadioButton[5];
    private static ButtonGroup groupRadioButtons = new ButtonGroup();

    //</editor-fold>   
}

2 个答案:

答案 0 :(得分:0)

您似乎对BankMainMenuFrame的第135行有循环依赖。 我怀疑构造函数是在循环中执行init。

答案 1 :(得分:0)

堆栈溢出似乎已得到纠正。 保留两个错误;

    while (c < bankMainMenuOpts.length) { //ERROR was <=

   //ERROR menuFrame.setContentPane(new Container());