So hello guys, im new to Java, still learning so i meant to make like random number generator game where window will be opened and have like 3 buttons if user hits first it move him to mode where he will be guessing numbers from 1-10, if he clicks second button it moves him to window where he will be guessing from numbers from 1-20 etc.So i made 2 files Game.java and Mode.java(Mode 1-10). So here's my code from Game.java
import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.UIManager; import javax.swing.JButton; import javax.swing.JLabel; public class Game extends JFrame{ private JLabel label1; private JLabel label2; private JLabel label3; private JButton button1; private JButton button2; private JButton button3; public Game(){ try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }catch(Exception e){ e.printStackTrace(); } label1 = new JLabel("Choose this mode"); add(label1); button1 = new JButton("1-10 Mode"); button1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg){ dispose(); // Over here i want to open new window where it will be actual game new Mode();//But this doesen't work, screen disposes but it dont opens another window } }); add(button1); } public static void main(String[] args) { Game gui = new Game(); gui.setLayout(new FlowLayout()); gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gui.setSize(300, 100); gui.setVisible(true); gui.setTitle("Game"); } }
Here's the code from Mode.java
import java.awt.FlowLayout; import javax.swing.JFrame; public class Mode extends JFrame{ private static final long serialVersionUID = 1L; public Mode() { Mode mod = new Mode(); mod.setLayout(new FlowLayout()); mod.setVisible(true); mod.setLocationRelativeTo(null); mod.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mod.setSize(500, 400); mod.setTitle("Hello"); } }
So screen disposes but don't show any new window and show this error
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at sun.awt.Win32GraphicsConfig.getBounds(Native Method)
at sun.awt.Win32GraphicsConfig.getBounds(Unknown Source)
at Mode.<init>(Mode.java:10)
Any help would be nice. Greets
By the way, i am using Eclipse IDE