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: JLabel Will Not Show Up In Frame

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JLabel Will Not Show Up In Frame

    So Im making a game with a start screen followed by my game. I have a button centered but Im having a hard time displaying my label. Do you all have any options? Here's my code:
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
     
    public class TitleScreen extends JPanel implements ActionListener{
     
    	private String name = "Breakout";
    	private Container c;
    	private JFrame menuscreen;
    	private JButton startbutton;
    	private JLabel label;
     
    	public TitleScreen(){
    		menuscreen = new JFrame("Breakout Menu Screen");
    		label = new JLabel("Breakout");
    		menuscreen.add(label);
    		startbutton = new JButton("Start");
    		startbutton.setPreferredSize(new Dimension (200,200));
    		startbutton.setBounds(110, 120, 100, 100);
    		startbutton.addActionListener(this);
    		menuscreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		c = menuscreen.getContentPane();
    		this.setLayout(null);
    		this.add(startbutton);
    		c.add(this);
    		this.setPreferredSize(new Dimension(300,300));
    		menuscreen.pack();
    		menuscreen.setLocationRelativeTo(null);
    		menuscreen.setVisible(true);
     
     
    	}
     
    	public void paintComponents(Graphics g){
    		Font font1 = new Font("Arial", Font.BOLD, 70);
    		g.setFont(new Font (name,10, 18));
    		g.setColor(Color.red);
    		//g.drawString(name,120, 20);
    		g.drawString("Breakout", 125, 250);
     
     
    	}
    	public static void main(String[]  args){
    		new TitleScreen();
     
    	}
     
    	public void actionPerformed(ActionEvent ae){
    		Object a = ae.getSource();
    		if(a == startbutton){
    			new Main();
    			this.menuscreen.dispose();
    			new Main();
    		}
     
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,343
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: JLabel Will Not Show Up In Frame

    Look at how the layout manager handles components added to the container it is managing.
    You need to tell some layout managers where to put the components that are added. Other layout managers will automatically place the added components.

    The code doesn't have any comments describing what it is doing to shorten the time it takes someone else reading the code to understand what the code is trying to do.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: JLabel Will Not Show Up In Frame

    You have:
    menuscreen = new JFrame("Breakout Menu Screen");
    label = new JLabel("Breakout");
    menuscreen.add(label);
    startbutton = new JButton("Start");
    ...
    c = menuscreen.getContentPane();
    this.setLayout(null);
    this.add(startbutton);
    c.add(this);

    JFrame's content pane's default layout is BorderLayout. Read through JFrame's class description. As Norm noted, you need to tell some layout managers (and BorderLayout is one of them) where components are to be added. See How to Use BorderLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container) for some examples. In fact, study the BorderLayoutDemo.java code sample in the tutorial to learn the clean way to add components.

Similar Threads

  1. My frame wont show anything
    By scarecrow in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 8th, 2014, 06:08 PM
  2. Trouble getting my frame and panels to show up!?!?
    By syregnar86 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 21st, 2013, 02:12 PM
  3. JLabel text on second frame
    By 0w1 in forum AWT / Java Swing
    Replies: 0
    Last Post: July 12th, 2013, 04:40 PM
  4. JLabel not appearing in new Frame ? please help
    By Prince76 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 8th, 2013, 03:48 AM
  5. New frame should only show GUI once!
    By gerre in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 30th, 2012, 11:34 PM