Results 1 to 7 of 7
- 05-23-2012, 08:10 AM #1
Member
- Join Date
- May 2012
- Posts
- 11
- Rep Power
- 0
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:
And if you want to get REALLY complicated for programs later on where there will be ActionListeners...Java Code:JFrame frame = new JFrame("Title"); frame.setVisible(true);
Oh, you want a combination of the two?Java Code:public class MyFrame extends JFrame { this.setVsible(true); }
Contrary to how people make swing seem, it is easy.Java Code:public class MyFrame extends JFrame { JFrame frame = this; frame.setVsible(true); }
- 05-23-2012, 08:18 AM #2
Member
- Join Date
- May 2012
- Posts
- 11
- Rep Power
- 0
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...
Now how to you add it to a JFrame? With one line of codeJava Code:JLabel label = new JLabel("Text on the JLabel");
(Of course I am going to give a whole example though...)
Doesn't that seem kinda intuitive to add a window sticker to a window?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);
Good, I'm glad you think so too :DLast edited by watachiaieto; 05-23-2012 at 08:22 AM.
- 05-23-2012, 08:30 AM #3
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):
Did you have a question or did you just want to share a couple of uncompilable code snippets?Learn Java programming with our Java tutorials and tips. Attention: Only forum staff can post to these forums!
db
edit Merged another thread started in the wrong section, with even worse code.Last edited by DarrylBurke; 05-23-2012 at 08:33 AM.
Why do they call it rush hour when nothing moves? - Robin Williams
- 05-23-2012, 08:38 AM #4
Member
- Join Date
- May 2012
- Posts
- 11
- Rep Power
- 0
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
Now lets put it together...Java Code:JButton notalever = new JButton("Push me, don't pull! I am a button...");
public class Machine extends JFrame
So basically I used machine and this interchangeably - that is TERRIBLE programming... consistency is the key.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(); }
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.
- 05-23-2012, 08:51 AM #5
Member
- Join Date
- May 2012
- Posts
- 11
- Rep Power
- 0
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.
- 05-23-2012, 08:59 AM #6
Member
- Join Date
- May 2012
- Posts
- 11
- Rep Power
- 0
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...
- 05-23-2012, 09:18 AM #7
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
make a rectangle over JLabel
By kumar.dinesh7 in forum AWT / SwingReplies: 6Last Post: 05-26-2012, 06:03 PM -
Cannot refresh a JLabel on a JFrame
By carnotan in forum AWT / SwingReplies: 11Last Post: 03-25-2011, 03:02 PM -
I can't make JLabel do what I want (size-wise)
By fletcher in forum AWT / SwingReplies: 9Last Post: 10-15-2009, 04:41 AM -
Make a text in Jlabel down to next Line
By hungleon88 in forum AWT / SwingReplies: 2Last Post: 12-01-2008, 11:10 PM -
help me with JFrame and JLabel
By michcio in forum New To JavaReplies: 5Last Post: 11-20-2007, 07:44 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks