Search code examples
javaswingjlabelembedded-resourceimageicon

NetBeans GUI Builder Image


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

Solution

  • 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);
                }
            });
        }
    }