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 10 of 10

Thread: A GUI issue.

  1. #1
    Junior Member LasOz's Avatar
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default A GUI issue.

    Hello there,

    I'd like to quickly start off by saying after contemplating what section to post this in this one seemed more appropriate then any of the other sections, though I have a gut feeling I'm still wrong.

    I'm fairly week at OO programming, no particular reason aside from I'm much more used to C which is more procedural.

    My problem at the moment is (and forgive my terms they're probably going to be wrong) I have two graphic classes 'startMenu' and 'Camera', which are part of a game I'm getting to grips with. I want to make it so that 'startMenu' will "go away" after the user hits enter, which I've currently implemented very poorly with a scan statement. From running this code with the other classes I find that nothing comes up (no window) until I hit enter. In this case it seems to skip (although is probably appearing for a nano second) everything in 'startMenu' and just goes to 'Camera'.

    Because I'm so used to procedural I'm just so baffled by the fact it's not similar to C in the sense that a scanf statement will "Stop" everything and wait for input; where as here it waits for input then starts.

    How do I make it so that 'startMenu' will show in the window and after the key enter is pressed it will show 'Camera'? (If that question makes any sense).

    I also have some additional questions:
    • How do I "take away" a class from the JFrame? I'm '.add' ing stuff to it is there a similar command to remove it from the frame entirely instead of just being in the background.
    • How do I layer graphics in the JFrame? I've heard and looked at panels, content and just '.add' ing it to the window in the right order. What would be the best option for a game?
    • How can I make an Image a button? I've looked around but all tutorials regarding this are just "Put image on button", I want the image to act as a button.


    Thank you for your time, I hope I haven't wasted it.

    class Thing 
    {
     
      public Thing()
      {
        JFrame mainwindow = new JFrame("Dooby");
        mainwindow.add(new startMenu());
     
        Scanner scan = new Scanner(System.in);
        String startit = scan.nextLine();
     
        mainwindow.add(new Camera());
        mainwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainwindow.setSize(800, 600);
        mainwindow.setVisible(true);
        mainwindow.setLocationRelativeTo(null);
      }
     
      public static void main (String[] args)
      {
        new Thing();
      }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: A GUI issue.

    Let me see if I can help, you have a few issues here...

    First off, that scan.nextLine() is acting like the scanf function you described- it stops and waits for input before it continues executing code, and since you don't show the gui until after you call scan.nextLine(), your program will not appear to start until after you hit enter.

    That being said, I don't think a Scanner is really what you're looking for. You want the user to hit 'enter' in the window, not in the command prompt, right? It's better to implement a KeyListener or Key Bindings in that case.

    Also, instead of adding and removing components from your gui, you'd be much better off using CardLayout, since that's exactly what it was designed for.

    As for layering components, there is a method setComponentZOrder() in the Container class, but most games do custom rendering instead of using separate components.

    And to make the image a button, you can either put the image in a JLabel and use a MouseListener to detect clicks, or you can set it as a JButton's icon and just set the size and border of the JButton to suit your needs.

    As always, your best friend is the API: Java Platform SE 6 If you have any questions about a class or method, that's your first stop.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    LasOz (March 8th, 2012)

  4. #3
    Junior Member LasOz's Avatar
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: A GUI issue.

    Thanks! I went with your mouselistener idea and I've run into another little problem.

    I'd like what's displayed to change from 'startMenu' to 'Camera' upon a click (there are some pointless lines of code in there). But it doesn't actually change what's displayed. What's going on?

    I can have the code, without Mouselistener, to have:
        mainwindow.add(new startMenu());
        mainwindow.add(new Camera());
    And it will display what's in Camera.

    But here's the actual code itself, what am I doing wrong here with the mouse listener?

    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.util.Scanner;
     
    class Thing extends JFrame
    {
      int x, y;
      private JFrame mainwindow = new JFrame("Dooby");
     
      public Thing()
      {
        mainwindow.add(new startMenu());
        Handlerclass handler = new Handlerclass();
        mainwindow.addMouseListener(handler);
        mainwindow.addMouseMotionListener(handler);
        mainwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainwindow.setSize(800, 600);
        mainwindow.setVisible(true);
        Scanner scan = new Scanner(System.in);
        String startit = scan.nextLine();
        mainwindow.setLocationRelativeTo(null);
      }
     
      public static void main (String[] args)
      {
        new Thing();
      }
     
      private class Handlerclass implements MouseListener, MouseMotionListener
      {
        public void mouseClicked(MouseEvent e)
        {
          mainwindow.add(new Camera());
          System.err.println("Boo");
        }
        public void mousePressed(MouseEvent e)
        {
        }
        public void mouseReleased(MouseEvent e)
        {
        }
        public void mouseEntered(MouseEvent e)
        {
        }
        public void mouseExited(MouseEvent e)
        {
        }
        public void mouseMoved(MouseEvent e)
        {
        }
        public void mouseDragged(MouseEvent e)
        {
        }
      }
    }

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: A GUI issue.

    what am I doing wrong here with the mouse listener?
    Can you explain what the problem is?

  6. #5
    Junior Member LasOz's Avatar
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: A GUI issue.

    Quote Originally Posted by Norm View Post
    Can you explain what the problem is?
    Ah, sorry!

    When I click, the content of 'Camera' is not displayed; instead the content of 'startMenu' remains on the screen.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: A GUI issue.

    Does the println statement execute and print "Boo" on the console?

    Where do you remove the old contents of the container?


    After you add something to a container, you need to tell the container and its layout manager to redo the layout of all the components in the container. The Container class has methods you can use to tell it to do that.

  8. The Following User Says Thank You to Norm For This Useful Post:

    LasOz (March 8th, 2012)

  9. #7
    Junior Member LasOz's Avatar
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: A GUI issue.

    Quote Originally Posted by Norm View Post
    Does the println statement execute and print "Boo" on the console?

    After you add something to a container, you need to tell the container and its layout manager to redo the layout of all the components in the container. The Container class has methods you can use to tell it to do that.
    It does print "Boo" every time I click.

    So simply "refresh" the layout? May I ask why when I have mainwindow.add(startMenu()) with mainwindow.add(Camera()) directly after it that the window actually displays the content of 'Camera' (While briefly flashing the content of startMenu)?

    Would this be a good resource JAVA Platform SE6 to look for a refresh method?

    Thank you by the way!

  10. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: A GUI issue.

    Yeah, the API is your best friend. The method you're looking for is validate or revalidate. Alternatively, you could look into CardLayout.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. The Following User Says Thank You to KevinWorkman For This Useful Post:

    LasOz (March 8th, 2012)

  12. #9
    Junior Member LasOz's Avatar
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: A GUI issue.

    Quote Originally Posted by KevinWorkman View Post
    Yeah, the API is your best friend. The method you're looking for is validate or revalidate. Alternatively, you could look into CardLayout.
    Ok, using validate now.

    It works and changes the content of the window to the 'Camera' class.

    Now 'Camera' uses a working keylistener, but in switching to it with validate I loose control... Pressing the keys doesn't yeild any output from the program. What's going wrong?

  13. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: A GUI issue.

    Might want to check which component has the focus, and make sure that the component you want to monitor for key presses requests it. Again, CardLayout is still probably how you should be proceeding. How to Use CardLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. not sure what the issue is here
    By Tfence in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 28th, 2011, 02:18 AM
  2. NullPointerException Issue
    By Nightshade in forum Exceptions
    Replies: 2
    Last Post: October 2nd, 2011, 02:07 AM
  3. Possible threading issue
    By Kerr in forum Threads
    Replies: 3
    Last Post: March 6th, 2011, 05:24 PM
  4. [SOLVED] Calendar Issue
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 11th, 2010, 01:19 PM
  5. Issues with Tomcat 6.0
    By sanyog24681 in forum Java Servlet
    Replies: 0
    Last Post: October 21st, 2008, 07:55 AM

Tags for this Thread