Solved

Adding text ontop of an image using JLabels in a JPanel

Posted on 2009-03-29
6
1,036 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet  URL Rewriting 1 42
xampp tool 12 56
hibernate example issues from command prompt 10 50
servlet and mdb, jms error 1 39
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

821 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