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

Thread: Creating a Simple GUI JFrame

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow Creating a Simple GUI JFrame

    I'm new this java program community, although i'm also new to java i'll share some of my knowledge to beginners. Just Copy & Pasting the code wouldn't do anyone any good. I'll start off simple and explain the commands. Lets get started. Whole source code at the end of the thread.
    For this tutorial im using Eclipse
    Lets start off by creating a new .java class if you haven't done so already. I've named mines gui.java. If your using a regular text editor make sure your java class looks like this code shown below:
    public class gui {
     
    }
    If you have some basic knownledge of Java lets enter the public void code,
    public class gui {
    	public static void main(String args[]) {
     
    	}
    }
    After we've gotten this covered lets import the JFrame function. Above the code were going to type import javax.swing.*;. This import makes the java class understand what the JFrame codes are. Without this you will get many errors within you class. So now your code should look something similar as:
    import javax.swing.*;
    public class gui {
    	public static void main(String args[]) {
     
    	}
    }
    Alright now that we've gotten all the boring stuff out of the way let's begin coding the real fun lines. JFrame or any other function will need a name, you can name it anything such as frame, bobby, anything you want it doesn't matter. I'm going to name mine tobi I just like the name.
    JFrame tobi = new JFrame("youwillneverfigurethisout");
    Now let me explain what i just wrote, the JFrame as you know is the Gui also the tobi variable we discussed earlier. The new eh...it explains itself there. ("youwillneverfigurethisout"); You can always type in anything you want here. dose not matter. make sure your code looks like:
    import javax.swing.*;
    public class gui {
    	public static void main(String args[]) {
    		JFrame tobi = new JFrame("youwillneverfigurethisout");
     
    	}
    }
    I will not explain many of these functions because it dose it themselves.
    Where going to start off by making the gui visible simple by typing
    tobi.setVisible(true);
    Next this comes in handy the title of the gui
    tobi.setTitle("Xbatz Gui Example")
    Set the size. . .
    tobi.setSize(400, 200);
    Making the gui close-able when the X is clicked
    tobi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Well almost there! Make sure your class looks like this:
    import javax.swing.*;
    public class gui {
    	public static void main(String args[]) {
    		JFrame tobi = new JFrame("youwillneverfigurethisout");
    		tobi.setVisible(true);
    		tobi.setTitle("Xbatz GUI Example");
    		tobi.setSize(400, 200);
    		tobi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    	}
    }
    If you have no errors lets run it!
    For users who are using a text editor, sorry i don't know how to run a class from that :\ i'm sure there is a tutorial on how within the site. For Eclipse user, NetBeans, etc. Go to the Run tab and select run. if you get no errors then your gui should look something like this:

    NOTE:: Well thanks guys i know it isn't much but i'm a beginner and will learn more soon
    Last edited by Xbatz; April 16th, 2012 at 10:24 PM.


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Creating a Simple GUI JFrame

    Remember to adhere to the Java Naming Conventions, especially with your class name. That would mean gui -> Gui [And that isn't a very good name either]

    Some possible class names:

    • FrameTest
    • XbatzFrame
    • FirstFrame
    • FrameExample


    Also, that code should be called on the EDT, because it is very gui-based. Finally, you should comment your code! This would make it look a bit more like:

    import javax.swing.*;
     
    import java.awt.FlowLayout;
     
    /**
     * This class demonstrates how to create a basic
     * JFrame.  For example purposes, this frame will
     * have a label saying "Hello World!"
     * 
     * @see javax.swing.JFrame
     * @author Timothy Moore
     */
    public class FrameExample extends JFrame
    {
      /**
       * Creates the frame example and makes it visible
       * 
       * @throws AssertionError if not called on the EDT
       */
      protected FrameExample()
      {
        setTitle("Frame Example");
        setSize(400, 200);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null); // Middle of the screen
     
        prepareComponents(); // Throws the assertion error
     
        setVisible(true); // This will paint the entire frame
      }
     
      /**
       * Prepares the components for display. 
       * 
       * @throws AssertionError if not called on the EDT
       */
      private void prepareComponents()
      {
        assert SwingUtilities.isEventDispatchThread();
     
        setLayout(new FlowLayout(FlowLayout.CENTER));
     
        add(new JLabel("Hello World!"));
      }
     
      /**
       * @param args not used
       */
      public static void main(String[] args)
      {
        SwingUtilities.invokeLater(new Runnable() {
          @Override
          public void run()
          {
            FrameExample example = new FrameExample();
          }
        });
      }
    }

    Hope you get the idea, I added the example because so few people put code on the Event Dispatch Thread these days. Note that I subclass JFrame, instead of create one using its constructor. This is a much more versatile technique.

    Some links:


Similar Threads

  1. Replies: 8
    Last Post: February 22nd, 2012, 02:42 PM
  2. Simple JFrame : Help with what goes in it
    By IBWebN in forum AWT / Java Swing
    Replies: 1
    Last Post: January 16th, 2012, 09:49 PM
  3. Creating subsequent frames in java with Jframe
    By bondage in forum AWT / Java Swing
    Replies: 3
    Last Post: April 11th, 2011, 07:28 AM
  4. how to make a simple JButton on a JFrame window?
    By chronoz13 in forum AWT / Java Swing
    Replies: 8
    Last Post: November 20th, 2009, 10:08 PM

Tags for this Thread