Solved

Adding text ontop of an image using JLabels in a JPanel

Posted on 2009-03-29
6
1,035 Views
Last Modified: 2013-11-23
I have a section in my code where i add in label3 and basically want to display the current date and maybe even the current time ontop of the image i have that fills up the entire JPanel. I wanted the current date/time to be displayed on top of this image. I can't seem to get the text to appear or placed where i want it. Any ideas?
public void welcome()
	{
  	  JFrame jF1 = new JFrame("ESCAPE THE CYLON INVASION!"); // Set the window title.
 
      // Set the size of the JFrame to 709 x 645 pixels. 
      jF1.setSize(709, 569);
 
	  // Create a JButton that will prompt for continuance. 
      JButton b1 = new JButton("BEGIN REGISTRATION");
      // Center the button in the JFrame.
      b1.setVerticalTextPosition(AbstractButton.CENTER);
      // Start the text on the left.
      b1.setHorizontalTextPosition(AbstractButton.LEADING);
      // Set the button's action.
	  b1.setMnemonic(KeyEvent.VK_D);
	  // Listen for the click.
      b1.addActionListener(new ActionListener() 
      {
    	  public void actionPerformed(ActionEvent e)
		  {
              // Upon clicking, open frame 2.
              name = getName();
          }
      });
		JLabel label3 = new JLabel();
    label3.setText(CURRENT DATE HERE);
    label3.setHorizontalTextPosition(JLabel.CENTER);
    label3.setVerticalTextPosition(JLabel.CENTER);
      // Create a new JLabel object to put a picture on.
      JLabel pictureJLabel = new JLabel();
      // Use JLabel ImageIcon to insert an image
      pictureJLabel.setIcon(new ImageIcon("start_screen.jpg"));
 
      // Create a new JPanel object to post other objects on.
      JPanel jPanel1 = new JPanel();
 
      // Post the JLabels on the JPanel
      jPanel1.add(pictureJLabel); // Post the BSG start screen. 
      jPanel1.add(b1);
		jPanel1.add(label3);            // Post the JButton
      
      // Post the JPanel into the JFrame
      jF1.getContentPane().add(jPanel1);
 
      // Displays the JFrame
      jF1.setVisible(true);
      
	}
	
	public static void about()
	{		
	JOptionPane.showMessageDialog(null, "You are fighting for your life aboard Battle Star Galactica, use your fighting skills to survive!\n"
	+ "Fees\n" + "Basic Registration Fees:\n" + "$2.50 for a Mini Player (ages under 4)\n" + "$5.00 for a Junior Player (ages 4  12)\n" + "$7.50 for a Power Player (ages 13  17)\n" + "$9.75 for an Xtreme Player (Ages 18 and above)\n" +  "$1.25 for each additional feature: a Cylon Batteship, Missiles, Health Packs\n" + "When you are ready to continue, click OK.");
 
	}

Open in new window

0
Comment
Question by:checkmofoshoduno
  • 4
6 Comments
 

Author Comment

by:checkmofoshoduno
ID: 24015985
I know i dont have the proper code in there to display the current date/time but i just wanted it to display text first, and then i would correct things so that it displayed the information i wanted.
0
 
LVL 17

Accepted Solution

by:
Thomas4019 earned 500 total points
ID: 24015993
First you need "jPanel1.setLayout(null)". Then for each Componet you are adding to your JPanel, you need to call setBounds(x,y,width,height);
0
 

Author Comment

by:checkmofoshoduno
ID: 24016192
After i add:

jPanel1.setLayout(null)

it becomes an empty frame on my welcome screen.
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Author Comment

by:checkmofoshoduno
ID: 24016440
I got what you were saying to work.
0
 

Author Closing Comment

by:checkmofoshoduno
ID: 31564189
Thanks for the help!
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 24016456
try setIconTextGap,

import java.awt.GridLayout;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/*
 * LabelDemo.java needs one other file:
 *   images/middle.gif
 */
public class LabelDemo extends JPanel {
    public LabelDemo() {
        super(new GridLayout(3,1));  //3 rows, 1 column
        JLabel label1, label2, label3;

        ImageIcon icon = createImageIcon("images/middle.gif",
                                         "a pretty but meaningless splat");

        //Create the first label.
        label1 = new JLabel("Image and Text",
                            icon,
                            JLabel.CENTER);
        //Set the position of its text, relative to its icon:
        label1.setVerticalTextPosition(JLabel.BOTTOM);
        label1.setHorizontalTextPosition(JLabel.CENTER);
        label1.setIconTextGap( -20 ) ;

        //Create the other labels.
        label2 = new JLabel("Text-Only Label");
        label3 = new JLabel(icon);

        //Create tool tips, for the heck of it.
        label1.setToolTipText("A label containing both image and text");
        label2.setToolTipText("A label containing only text");
        label3.setToolTipText("A label containing only an image");

        //Add the labels.
        add(label1);
        add(label2);
        add(label3);
    }

    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path,
                                               String description) {
        java.net.URL imgURL = LabelDemo.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL, description);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("LabelDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add content to the window.
        frame.add(new LabelDemo());

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            //Turn off metal's use of bold fonts
              UIManager.put("swing.boldMetal", Boolean.FALSE);
               
            createAndShowGUI();
            }
        });
    }
}
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jar file executable 12 52
diffSum example 4 37
Running JavaFX on JDeveloper 12C 1 33
Java: anonymous class 4 23
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question