Question

Im having this problem where my Label2 which is assigned to be a text doesn't appear on the Jframe, even though I added it. Im assuming the other Labels which I set as images are overlaping it? If that is the problem how would I go upon fixing it?

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class Generator extends JFrame {

    private static final long serialVersionUID = 1L;

    /**
     * Launch the application.
     */
    public static void main(final String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    final Generator frame = new Generator();
                    frame.setVisible(true);
                } catch (final Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Generator() {
        /*
         * @Images/Text
         */
        final ImageIcon icon = new ImageIcon("data/hover_a.png");
        final JLabel label = new JLabel(icon);

        final ImageIcon icon1 = new ImageIcon("data/background.png");
        final JLabel label1 = new JLabel(icon1);

        final ImageIcon img = new ImageIcon("data/icon.png");

        final JLabel label2 = new JLabel("Test Text");

        /*
         * @Image/Text Bounds
         */
        label.setBounds(24, 266, 240, 40);
        label1.setBounds(0, 0, 289, 330);
        label2.setBounds(0, 0, 80, 15);

        /*
         * @Image Hovers
         */
        label.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseEntered(final MouseEvent evt) {
                label.setIcon(new ImageIcon("data/hover_b.png"));
            }

            @Override
            public void mouseExited(final MouseEvent evt) {
                label.setIcon(new ImageIcon("data/hover_a.png"));
            }

        });

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(295, 358));
        setTitle("test");
        getContentPane().setLayout(null);
        setResizable(false);
        setVisible(true);
        setIconImage(img.getImage());
        getContentPane().add(label);
        getContentPane().add(label1);
        getContentPane().add(label2);
        pack();
    }
}
Was it helpful?

Solution

You're calling setVisible(true) before adding all your components. Don't do this. The orde is:

  • add your components
  • call pack()
  • call setVisible(true)
  • if later you make changes then call revalidate() on the container, then repaint()

OTHER TIPS

From reading your code, it appears that you are trying to layer images on top of each other. This would be much better achieved through the use of customer panels with custom painting

Have a look at the Performing Custom Painting lesson & 2D Graphics Trail

Without know precisely what you want to achieve, I would create a background panel that paints the background and then I would use a layout manager to layout the two other icons (personally, I used a GridBagLayout, but it might be a little over kill for you needs)

I would also familiarise your self with the painting order of components

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top