my program is compiling but does not work, code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class GUI extends JFrame{
public String response;
public int secretNumber;
public int max_GUESSES = 6;
public int guess;
public String userName;
public String password;
public String userNameInput;
public String passwordInput;
public JButton button;
public JTextField textField;
public JLabel promptLabel;
public JLabel resultLabel;
public JLabel randomLabel;
public GUI()
{
setLayout(new FlowLayout());
promptLabel = new JLabel("Enter a random number 1-100");
add(promptLabel);
textField = new JTextField(5);
add(textField);
button = new JButton("Guess!");
add(button);
resultLabel = new JLabel("");
add(resultLabel);
randomLabel = new JLabel("");
add(randomLabel);
Event e = new Event();
button.addActionListener(e);
}
public static void main(String[] args) {
GUI guessingGame = new GUI();
guessingGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guessingGame.setVisible(true);
guessingGame.setSize(400,400);
guessingGame.setTitle("Random Number Guessing Game");
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class Event extends GUI implements ActionListener
{
Random randomGenerator;
public void actionPerformed(ActionEvent e)
{
int loopCounter=0;
secretNumber = randomGenerator.nextInt(100);
guess = (int)(Double.parseDouble(textField.getText()));
do
{
loopCounter++;
if (guess == secretNumber)
{
resultLabel.setText("you won the game");
}
else if (guess < secretNumber)
{
resultLabel.setText("your number is lower, please guess again");
}
else if (guess > secretNumber)
{
resultLabel.setText("your number is higher, please guess again");
}
}
//while loop:
while ((loopCounter < max_GUESSES)&&(guess != secretNumber));
{
resultLabel.setText("you had too many tries, please start game again");
randomLabel.setText("the random number is "+ secretNumber);
}
}
}
Errors:
Exception in thread "main" java.lang.StackOverflowError
at javax.swing.text.StyleContext$SmallAttributeSet.equals(StyleContext.java:838)
at java.util.WeakHashMap.eq(WeakHashMap.java:259)
at java.util.WeakHashMap.get(WeakHashMap.java:353)
at java.util.Collections$SynchronizedMap.get(Collections.java:1975)
at javax.swing.text.StyleContext.getImmutableUniqueSet(StyleContext.java:503)
at javax.swing.text.StyleContext.addAttributes(StyleContext.java:323)
at javax.swing.text.AbstractDocument$AbstractElement.addAttributes(Abstractdocument.java:1971)
at javax.swing.text.AbstractDocument$AbstractElement.<init>(Abstractdocument.java:1762)
at javax.swing.text.AbstractDocument$LeafElement.<init>(Abstractdocument.java:2488)
at javax.swing.text.AbstractDocument$BidiElement.<init>(Abstractdocument.java:2660)
at javax.swing.text.Abstractdocument.<init>(Abstractdocument.java:132)
at javax.swing.text.Abstractdocument.<init>(Abstractdocument.java:92)
at javax.swing.text.Plaindocument.<init>(Plaindocument.java:74)
at javax.swing.text.Plaindocument.<init>(Plaindocument.java:64)
at javax.swing.text.DefaultEditorKit.createDefaultDocument(DefaultEditorKit.java:113)
at javax.swing.plaf.basic.BasicTextUI.installUI(BasicTextUI.java:785)
at javax.swing.JComponent.setUI(JComponent.java:662)
at javax.swing.text.JTextComponent.setUI(JTextComponent.java:322)
at javax.swing.text.JTextComponent.updateUI(JTextComponent.java:332)
at javax.swing.text.JTextComponent.<init>(JTextComponent.java:306)
at javax.swing.JTextField.<init>(JTextField.java:212)
at javax.swing.JTextField.<init>(JTextField.java:179)
at GUI.<init>(GUI.java:33)
at Event.<init>(Event.java:5)
at GUI.<init>(GUI.java:45)
at Event.<init>(Event.java:5)
at GUI.<init>(GUI.java:45)
at Event.<init>(Event.java:5)
at GUI.<init>(GUI.java:45)
at Event.<init>(Event.java:5)
at GUI.<init>(GUI.java:45)
at Event.<init>(Event.java:5)
at GUI.<init>(GUI.java:45)
at Event.<init>(Event.java:5)
at GUI.<init>(GUI.java:45)
do you have any idea what I did wrong? thanks for your input

New Topic/Question
Reply




MultiQuote








|