Solved

Adding text ontop of an image using JLabels in a JPanel

Posted on 2009-03-29
6
1,041 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 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
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
String array comparison 4 56
printf performancy 11 101
web project error add remove 1 79
Tagging and Merging on Branch 1 68
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

734 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