プログラムを実行すると、ウィンドウがJavaプログラムから表示され、スタックオーバーフローエラーが発生する問題があります。私のコードで間違っていたところ、私が思っていたウィンドウが表示されないJavaおよびスタックオーバーフローエラー
public Ballot() {
bp.addBallot();
frame.setTitle("Ballot");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(430,350);
frame.setLocation(300,100);
frame.setResizable(false);
Container cont = frame.getContentPane();
cont.setLayout(null);
cont.setBackground(Color.GRAY);
ButtonGroup bg = new ButtonGroup();
JRadioButton[] buttons = new JRadioButton[numButtons];
for (int i = 0; i < buttons.length; i++) {
bg.add(buttons[i]);
}
JLabel label = new JLabel("Make your choice");
label.setForeground(Color.BLUE);
label.setFont(new Font("Arial", Font.BOLD, 11));
next = new JButton("Next");
next.setForeground(Color.GREEN);
next.addActionListener(this);
panel = new JPanel();
for (int i = 0; i < buttons.length; i++) {
panel.add(buttons[i]);
}
panel.add(next);
cont.add(panel);
setVisible(true);
}
:ここ
は、私は問題を抱えているコードのサンプルです。
助けるかもしれない追加のコード:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.io.*;
public class Ballot extends JPanel implements ActionListener {
JPanel panel;
JFrame frame = new JFrame();
int numButtons;
JRadioButton[] buttons;
JButton next = new JButton();
int votedOrNot;
BallotProgram bp = new BallotProgram();
public Ballot() {
bp.addBallot();
frame.setTitle("Ballot");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(430,350);
frame.setLocation(300,100);
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
Container cont = frame.getContentPane();
cont.setLayout(null);
cont.setBackground(Color.GRAY);
ButtonGroup bg = new ButtonGroup();
JRadioButton[] buttons = new JRadioButton[numButtons];
for (int i = 0; i < buttons.length; i++) {
bg.add(buttons[i]);
}
JLabel label = new JLabel("Make your choice");
label.setForeground(Color.BLUE);
label.setFont(new Font("Arial", Font.BOLD, 11));
next = new JButton("Next");
next.setForeground(Color.GREEN);
next.addActionListener(this);
panel = new JPanel();
for (int i = 0; i < buttons.length; i++) {
panel.add(buttons[i]);
}
panel.add(next);
cont.add(panel);
setVisible(true);
}
public void actionPerformed(ActionEvent a) {
if (next.getText().equals("Cast Vote")) {
if (votedOrNot == 0) {
votedOrNot++;
}
else {
}
}
else if (next.getText().equals("Login to Vote")) {
}
}
}
そして、他のクラス:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.io.*;
public class BallotProgram {
String[] choices;
int numOfChoices;
public ArrayList<Ballot> addBallot() {
File file = new File("ballots.txt");
boolean exists = file.exists();
ArrayList<Ballot> ballot = new ArrayList<Ballot>();
Ballot b = new Ballot();
try {
Scanner s = new Scanner(file);
while (s.hasNextLine()) {
int ballotID = s.nextInt();
s.nextLine();
String ballotTitle = s.nextLine();
choices = new String[numOfChoices];
b.buttons = new JRadioButton[b.numButtons];
for (int i = 0; i < b.numButtons; i++) {
choices[i] = s.nextLine();
b.buttons[i] = new JRadioButton(choices[i]);
}
Ballot bt = new Ballot();
ballot.add(bt);
}
s.close();
} catch (IOException f) {
System.out.println("can't find the file");
}
return ballot;
}
そして、何私の端末は言う:
Exception in thread "main" Exception in thread "main" java.lang.StackOverflowError
at sun.awt.X11GraphicsConfig.pGetBounds(Native Method)
at sun.awt.X11GraphicsConfig.getBounds(X11GraphicsConfig.java:314)
at java.awt.Window.init(Window.java:505)
at java.awt.Window.<init>(Window.java:537)
at java.awt.Frame.<init>(Frame.java:420)
at java.awt.Frame.<init>(Frame.java:385)
at javax.swing.JFrame.<init>(JFrame.java:189)
at Ballot.<init>(Ballot.java:10)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
おかげ
'setVisible()'の前に 'pack()'を実行してみてください。 – Zulfe
また、 'bp.addBallot()'の結果として実行しているものがstackOverflowExceptionを引き起こしていないことを確認してください。スタックトレースを提供した場合、何が間違っているかを簡単に伝えることができます。 – Zulfe
が編集を行いました。なぜウィンドウが開かないのか+スタックオーバーフローエラーか分かりません。 – ABY