0

An image is being displayed on JLabel and the text is displayed right to it. I want the image to be the background of JLabel and the text should be displayed over it.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class TestGUI extends JFrame implements MouseMotionListener {
    JLabel l1;

    TestGUI() {
        l1 = new JLabel();
        getContentPane().setBackground(Color.GREEN);

        setLayout(null);
        l1.setBounds(50, 80, 600, 60);

        l1.setIcon(new ImageIcon("E:\\subjects\\JAVA\\Internet Images\\blue.jpg"));
        l1.setFont(new Font("Jokerman", 3, 20));
        addMouseMotionListener(this);
        add(l1);

        show();
        setSize(600, 500);
        setDefaultCloseOperation(3);
    }

    public void mouseMoved(MouseEvent me) {
        l1.setText("x=" + me.getX() + " y=" + me.getY());
        System.out.println("mouseMoved");
    }

    public void mouseDragged(MouseEvent me) {
        System.out.println("mouseDragged");
    }

    public static void main(String x[]) {
        new TestGUI();
    }
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
karthik
  • 9
  • 1
  • Possible duplicate of [How to add an image to a JPanel?](https://stackoverflow.com/questions/299495/how-to-add-an-image-to-a-jpanel) – Sergiy Medvynskyy Oct 18 '19 at 11:12
  • Add your image to a JPanel (as it shown in the referenced topic), and then add your label to the panel. – Sergiy Medvynskyy Oct 18 '19 at 11:13
  • 2
    1) `setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) .. – Andrew Thompson Oct 18 '19 at 11:16
  • 1
    .. hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). 3) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 4) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. Please [edit] the question to add 4 space chars ahead of the closing `}` at the end of the code. 5) `..extends JFrame` don't extend frame, or other components or classes. without good reason. – Andrew Thompson Oct 18 '19 at 11:17
  • 6) `new Font("Jokerman", 3, 20)` Don't use numbers for defined constants. Something like `new Font("Jokerman", Font.ITALIC + Font.BOLD, 20)` is both more informative to the reader, and gets compile-time checking. 7) `"Jokerman"` unless that font is supplied with the app, best to plan on using backups if it's not available. – Andrew Thompson Oct 18 '19 at 16:51

1 Answers1

1

I want the image to be the background of JLabel and the text should be displayed over it.

By default text is displayed to the right of the image.

This can be changed by playing with properties of the JLabel class. For example:

label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);

Read the JLabel API for other properties that might be useful.

camickr
  • 321,443
  • 19
  • 166
  • 288