We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 24,824 times

Contents

Related Categories

Programming with Swing - JButton and JLabel

Michael H

JButton and JLabel

JButton

Here's where the fun comes in, yep, we are going to add a component, a JButton. You know what a button is right? One of those funky little rectangles that you click on and they go down and then pop back up again. Enough talk, you're probably crapping your-self in anticipation, so here we go!

JFrame frame = new JFrame("Title Bar Text");        
JPanel panel = new JPanel();                        
JButton button1 = new JButton("Button Text");      
frame.setSize(256,256);                            
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);                  
panel.add(button1);                                
frame.pack();                                      
frame.show();                                      

And there it is ladies and gentleman, two count 'em 2 new lines of code to create and add a JButton to our JPanel. Notice that when we added the panel, we want to add it to the JFrame's contentpane and when we want to add the JButton, we just plain add() it to the JPanel. This isn't exactly rocket science, if rocket science is what you want, go here.

JLabel

Alright, now that you know how to add components to the JPanel, I'm just going to tell you how to use the constructor method. I goes like this:

JLabel label1 = new JLabel("Label Text");

If you don't understand that, then you shouldn't be reading this, go read up on classes and constructor methods. After you create the JLabel, just add it to the JPanel like we did for JButton.

Comments

  • Closing button on internal frame

    Posted by manojacharya on 14 Nov 2005

    Hello,
    I want to add a closing button to the internal frame. I am not able to do it. Plz help.javascript:smilie(':confused:')
    confused

  • Java - MDI - ChildFrames

    Posted by BG3000 on 28 Oct 2004

    Hi.

    I have an ChildFrame inside of an DesktopFrame. I want to insert a JButton into the ChildFrame. But this JButton turns out to be very tiny in that child frame. If I maximize the child frame, t...

  • Posted by munas on 13 Sep 2004

    jho....

    just call your child frames as follows

    public class MainFrame extends JFrame
    {
    ChildFrame1 child1;
    public MainFrame()
    {
    final JDesktopPane desktop = new JDesktopPane();
    ...

  • JAVA MDI

    Posted by jho on 05 Aug 2004

    i have a JFrame named MainFrame which also uses JDesktoPane. I created 2 JInternalFrame class named ChildFrame1 and ChildFrame2. When I can click the menu on the mainframe, it shows the childframe1. I...