0

I've added an image that I want to use as a background image and I want to put jLabels on top of it. So I use the image icon feature and show the image, but when I try to put a jLabel on it, it gets moved off to the side. I've tried several tutorials and it appears to work on youtube, but when I try to do the same thing on my own they get moved out of position.

field.setIcon(new javax.swing.ImageIcon(getClass().getResource("/wiffleball/resources/field2.png"))); // NOI18N
mKorbel
  • 109,525
  • 20
  • 134
  • 319
The Hawk
  • 1,496
  • 6
  • 26
  • 43

2 Answers2

1

The JLabel doesn't have a layout manager by default. Label's also have default text positioning, which is normally aligned to the left, you need to change all these default values...

enter image description here

You may want to use a different layout manager other the BorderLayout, but this is just an example...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SimpleLabel {

    public static void main(String[] args) {
        new SimpleLabel();
    }

    public SimpleLabel() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JLabel label = new JLabel(new ImageIcon("C:\\hold\\thumbnails\\_cg_836___Tilting_Windmills___by_Serena_Clearwater.png"));
                label.setHorizontalAlignment(JLabel.CENTER);
                label.setVerticalAlignment(JLabel.CENTER);

                label.setLayout(new BorderLayout());

                JLabel child = new JLabel("Can you see me?");
                child.setForeground(Color.WHITE);
                child.setFont(label.getFont().deriveFont(Font.BOLD, 24f));
                child.setHorizontalAlignment(JLabel.CENTER);
                child.setVerticalAlignment(JLabel.CENTER);
                child.setHorizontalTextPosition(JLabel.CENTER);
                child.setVerticalTextPosition(JLabel.CENTER);
                label.add(child);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(label);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • The JLabel has a layout manager, :-) [JLabel haven't any LayoutManager](http://stackoverflow.com/questions/8575641/how-returns-xxxsize-from-jcomponents-added-to-the-jlabel), returns null from this property – mKorbel Jun 05 '13 at 06:35
  • @mKorbel or something like that ;) – MadProgrammer Jun 05 '13 at 06:45
0

I put everything on a jPanel and that seemed to do it. It just took some tinkering with. Thanks!

The Hawk
  • 1,496
  • 6
  • 26
  • 43