?
Solved

Adding text ontop of an image using JLabels in a JPanel

Posted on 2009-03-29
6
Medium Priority
?
1,043 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1500 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

771 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