Results 1 to 7 of 7
  1. #1
    watachiaieto is offline Member
    Join Date
    May 2012
    Posts
    11
    Rep Power
    0

    Default Make a JLabel (and obviously how to add it to a JFrame...)

    Swing is one of the most complicated graphical design languages you will ever learn. The code to make a simple frame is probably beyond the scoop of a beginner Java programmer.
    The code is so hard that this tutorial will all be dedicated to making a JFrame.
    Here is the code:
    Java Code:
    JFrame frame = new JFrame("Title");
    frame.setVisible(true);
    And if you want to get REALLY complicated for programs later on where there will be ActionListeners...
    Java Code:
    public class MyFrame extends JFrame
    {
         this.setVsible(true);
    }
    Oh, you want a combination of the two?
    Java Code:
    public class MyFrame extends JFrame
    {
         JFrame frame = this;
         frame.setVsible(true);
    }
    Contrary to how people make swing seem, it is easy.

  2. #2
    watachiaieto is offline Member
    Join Date
    May 2012
    Posts
    11
    Rep Power
    0

    Default Make a JLabel (and obviously how to add it to a JFrame...)

    What is a JLabel? Well what is a label at a store?

    The JLabel holds text, and all you have to do is stick the JLabel onto the JFrame, kind of like the people at walmart stick labels onto the shelves.

    So the constructor for a JLabel is pretty simple...
    Java Code:
    JLabel label = new JLabel("Text on the JLabel");
    Now how to you add it to a JFrame? With one line of code
    (Of course I am going to give a whole example though...)
    Java Code:
    JFrame window = new JFrame("JLabel Tutorial");
    JLabel windowsticker = new JLabel("I am sticking to the window, with text!!!");
    window.add(windowsticker);
    window.setVisible(true);
    Doesn't that seem kinda intuitive to add a window sticker to a window?
    Good, I'm glad you think so too :D
    Last edited by watachiaieto; 05-23-2012 at 09:22 AM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Make a JLabel (and obviously how to add it to a JFrame...)

    Removed from Java Example -> Swing. Please review the Forums page, where you can read, in the heading for the "Learn Java" section (emphasis added):
    Learn Java programming with our Java tutorials and tips. Attention: Only forum staff can post to these forums!
    Did you have a question or did you just want to share a couple of uncompilable code snippets?

    db

    edit Merged another thread started in the wrong section, with even worse code.
    Last edited by DarrylBurke; 05-23-2012 at 09:33 AM.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    watachiaieto is offline Member
    Join Date
    May 2012
    Posts
    11
    Rep Power
    0

    Default Make a JButton (and add it to a JFrame too)

    Buttons are something we use in programs, and so they are something we make in Java code.
    Here is how you make a button
    Java Code:
    JButton notalever = new JButton("Push me, don't pull! I am a button...");
    Now lets put it together...
    public class Machine extends JFrame
    Java Code:
    {
         this.setTitle("I Do Work!");
         JButton dosomething = new JButton("Push me, I guarantee the window is lying through it's teeth (buttons...)!");
         this.add(dosomething);
         JFrame machine = this;
         machine.setVisible(true);
         this.pack();
    }
    So basically I used machine and this interchangeably - that is TERRIBLE programming... consistency is the key.
    That was just so people could see the different things that you can do to modify a JFrame's state
    I will say that I PERSONALLY like to assign this to an object name just to prevent confusion with other classes and other pieces inside of it.

    I also used the pack() method - it is used to make the JFrame JUST big enough so that everything fits snuggly.

  5. #5
    watachiaieto is offline Member
    Join Date
    May 2012
    Posts
    11
    Rep Power
    0

    Default Re: Make a JLabel (and obviously how to add it to a JFrame...)

    The "Java > Examples > Swing " was hopelessly empty. I was just trying to fill the null space.

    I did not have a question when I wrote the thread, but now I would love to know how to delete my own thread. Otherwise (if I am unable to do so) I would like to request you do it for me.

  6. #6
    watachiaieto is offline Member
    Join Date
    May 2012
    Posts
    11
    Rep Power
    0

    Default Re: Make a JLabel (and obviously how to add it to a JFrame...)

    OH! and I have one more thread in there from before I noticed you removing them...

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Make a JLabel (and obviously how to add it to a JFrame...)

    That's ok, I've merged it here too.

    With due respect for your intentions, you have not reached a stage in your learning Swing that qualifies you to advise others. There are perfectly good tutorials on the Oracle site, linked from the API for most of the Swing classes.

    Please acknowledge that you have seen and read this, after which I will removed this thread.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. make a rectangle over JLabel
    By kumar.dinesh7 in forum AWT / Swing
    Replies: 6
    Last Post: 05-26-2012, 07:03 PM
  2. Cannot refresh a JLabel on a JFrame
    By carnotan in forum AWT / Swing
    Replies: 11
    Last Post: 03-25-2011, 04:02 PM
  3. I can't make JLabel do what I want (size-wise)
    By fletcher in forum AWT / Swing
    Replies: 9
    Last Post: 10-15-2009, 05:41 AM
  4. Make a text in Jlabel down to next Line
    By hungleon88 in forum AWT / Swing
    Replies: 2
    Last Post: 12-02-2008, 12:10 AM
  5. help me with JFrame and JLabel
    By michcio in forum New To Java
    Replies: 5
    Last Post: 11-20-2007, 08:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •