我在java中编写了一个JFrame(framePanel),但是在运行它或通过实例化这个类的一个对象并调用方法'create_ShowGUI()'从另一个类运行它时,我得到了一个StackOverflowError。
基本上我希望在调用 main() 方法时,调用 initComponents() 方法来初始化我的组件(设置单选按钮,将它们分组,将 Actionlistener 添加到所有单选按钮,创建 JPanel(menuFrame)并将按钮添加到它) . 在 initComponents() 完成其工作后,初始化 JFrame,将 JPanel 添加到其中并最终显示该框架。
你能帮我解决错误吗?
编辑:我创建了各种应用程序,但使用的是 NetBeans GUI Builder Tool。这是我第一次尝试从头开始。因此,如果有不清楚的地方,请原谅。
编辑:用评论更新代码
编辑:顺便说一句,我用 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>
}