Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Java Problem

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Java Problem

    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


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Problem

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    A StackOverflow error is typically caused by an infinite recursive call. Note the statement in the Mode() constructor:

    Mode mod = new Mode();

    What is that going to do?

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    HypeIsReal (July 25th, 2014)

  4. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Java Problem

    Alright so i solved this by making new method outside of constructor like private void createWindow() and then calling that into constructor. So, thank you very much i appreciate your answer!

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Problem

    Glad to help. You're welcome.

    Update: I like your approach and it may be best for what will follow, but an option was to simply eliminate the mod object, as in:
        public Mode() {
            setLayout(new FlowLayout());
            setVisible(true);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(500, 400);
            setTitle("Hello");
        }

Similar Threads

  1. Java problem
    By destinedtale in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 7th, 2014, 11:02 AM
  2. HELP,java problem *java.lang.NullPointerException*
    By Agung_Rianto in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 24th, 2014, 11:19 AM
  3. java problem. please help
    By jimmy_crews in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 31st, 2013, 10:33 AM
  4. Java Problem
    By Rachit Semalty in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 22nd, 2013, 11:55 PM
  5. Having a problem with java IO
    By DanTheSand in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: December 7th, 2011, 02:16 AM