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

Thread: Label not showing up.

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Label not showing up.

    Alright guys... not sure what's goin on here, but obviously something is happening that I"m unaware of. You can ignore the ActionEvent method at the bottom... What I want to do is have the word "test" about the text field.

    package FifthFrame;
     
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    class MyJFrame extends JFrame implements ActionListener{
     
        // private instance variables
        JButton button;
        int buttonCount;
        JTextField inputLine;
        JLabel label;
     
        public static void main (String [] args) {
     
    	MyJFrame mine = new MyJFrame();
            mine.setVisible(true);
     
        }
     
        public MyJFrame () {
     
            setTitle ("This is a \"MyJFrame\" object (V5)");
            setSize (300, 500);
            setDefaultCloseOperation( EXIT_ON_CLOSE );
     
            // get the content pane and set properties
            Container contentPane = getContentPane();
            contentPane.setBackground (Color.blue);
            contentPane.setLayout(null); // so that we can use absolute positioning
     
            // construct a button, and set the number of click on it to 0
            button = new JButton("0");
            button.setBounds(110,230,80,40);
    	button.addActionListener(this);
            contentPane.add(button);
            buttonCount = 0;
     
            // construct a text field
            inputLine = new JTextField();
            inputLine.setBounds(100,100,100,20);
            inputLine.addActionListener(this);
            contentPane.add(inputLine);
     
            label = new JLabel ("test", JLabel.CENTER);
            label.setForeground(Color.white);
            contentPane.add(label);
     
        }
     
        public void actionPerformed(ActionEvent event) {
    	buttonCount++;
    	button.setText(Integer.toString(buttonCount));
            inputLine.setText("");
     
        }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Label not showing up.

    First, I strongly recommend against using a null layout. While layouts can be a bit intimidating at first, they are extremely important in GUI design (see A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)). Second, the bounds of the label is never set, and this is necessary to position the component.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Label not showing up.

    Alright, most of the code was pre-written like that. I'm supposed to just add a label. I wasn't aware that the bounds were required to be set - thanks.

Similar Threads

  1. Interaction between two label in animation
    By odiejodie in forum Java Theory & Questions
    Replies: 1
    Last Post: February 16th, 2011, 08:51 AM
  2. how to make widget label cloud at JSP
    By 20GreatWall in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: October 11th, 2010, 11:16 AM
  3. Can't set label text from a Jbutton
    By VBGuy in forum What's Wrong With My Code?
    Replies: 8
    Last Post: July 8th, 2010, 10:55 AM
  4. Showing a picture in a GUI
    By joachim89 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 15th, 2010, 02:42 PM
  5. centering a label inside a rectangle
    By Brain_Child in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 19th, 2009, 09:08 AM