我的代码编译但是当我运行它时,会弹出一个奇怪的错误消息。
错误消息显示
“java.lang.StackOverflowError:null(在sun.awt.Win32GraphicsConfig中)。
它应该做的是打印一个带有4x10按钮阵列的面板,其中包含一个按钮,列出到目前为止所有存储的信息。按钮现在没有做任何事情,但这很好,因为我现在只关心实际的图形。对于数组列表,还有另一个类,如果需要,我可以在注释中添加它。
以下是代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.*;
import java.io.File;
import java.io.PrintWriter;
public class CulminatingProjectGUI extends JFrame implements ActionListener
{
private static JButton[][] jbtNumButtons = new JButton[10][4];
private static JButton jbtList = new JButton("List");
ArrayList<Culminating> seats = new ArrayList<Culminating>();
public CulminatingProjectGUI()
{
//Construct JFame object as a container for other objects
JFrame frame = new JFrame("Fly By Buddies");
//Set the dimensions of the window
frame.setSize(750, 720);
//Creates a pane for content
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(11, 4));
jbtList.addActionListener(new CulminatingProjectGUI());
for (int m = 1,n = 1; n < 11;)
{
jbtNumButtons[n][m].addActionListener(new CulminatingProjectGUI());
if(n == 1)
{
jbtNumButtons[n][m].setText(m + "A");
}
else if(n == 2)
{
jbtNumButtons[n][m].setText(m + "B");
}
else if(n == 3)
{
jbtNumButtons[n][m].setText(m + "C");
}
else if(n == 4)
{
jbtNumButtons[n][m].setText(m + "D");
}
pane.add(jbtNumButtons[n][m]);
m++;
if(m > 4)
{
n++;
m = 0;
}
}
//add content pane to a frame
pane.add(jbtList);
frame.setContentPane(pane);
//Display the frame - window
frame.setVisible(true);
//Bring the window to front
frame.toFront();
}
public static void main(String []args)
{
CulminatingProjectGUI frame = new CulminatingProjectGUI();
//Create seat array. Still testing.
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("List"))
{
String listedFlyers = "";
for (int h =0;h< 40;h++)
{
listedFlyers = listedFlyers + seats.get(h).toString() + "\n";
}
JTextArea text = new JTextArea(listedFlyers, 10, 40);
JScrollPane scroll = new JScrollPane(text);
}
else if (e.getActionCommand().equals(""))
{
}
}
}
答案 0 :(得分:1)
jbtNumButtons [n] [m] .addActionListener(new CulminatingProjectGUI());
这是问题所在。您创建CulminatingProjectGUI
的新实例,并在构造函数中一次又一次地创建它。您应该考虑以下事项:
jbtNumButtons[n][m].addActionListener(this);