Solved

Adding text ontop of an image using JLabels in a JPanel

Posted on 2009-03-29
6
1,021 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumHeights2  challenge 7 76
count11 challenge 6 47
pairs challenge 5 44
unix in java example 9 39
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now