-1

So I've been trying to teach myself the basics of Java (2D) gaming. After a long, annoying process trying to get an image to display, I got it working. Unfortunately, when I tried to add a second image, it replaced the first. I know that I'm making some obvious, noob mistake, but hey, I'm a noob at this. Anyway, here's my code:

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

public class Main extends JFrame {

    public static void main(String[] args0) {

        JFrame frame = new JFrame();
        ImageIcon background = new ImageIcon("background.png");
        JLabel backgroundLabel = new JLabel(background);
        frame.add(backgroundLabel);
        backgroundLabel.setVisible(true);

        ImageIcon title = new ImageIcon("title.png");
        JLabel titleLabel = new JLabel(title);
        frame.add(titleLabel);
        titleLabel.setVisible(false);

        frame.setVisible(true);
        frame.setTitle("Fastball");
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.pack();

    }

}

When I run this, the newly added "title.png" section overrides the first image, replacing it. Please, just tell me the simplest way to fix this with a brief explanation of my mistake.

(P.S. I'm using Eclipse Mars and the latest of all the Java stuff.)

Phyremaster
  • 87
  • 2
  • 8
  • This approach is trying to layout components in a situation where it should toss (all but one) components out the window and do custom painting instead! See the [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/) lesson of the tutorial for details. – Andrew Thompson Sep 11 '15 at 07:19
  • BTW - the `backgroundLabel.setVisible(true); .. titleLabel.setVisible(false);` suggests you might actually by trying to implement what a `CardLayout` is made for. It can flip between 'views' as needed. – Andrew Thompson Sep 11 '15 at 10:01
  • Why did someone downvote me? I know it's probably simple, but asking questions is what this site is for! ;( – Phyremaster Sep 15 '15 at 22:07

1 Answers1

3

Start by taking a look at How to Use BorderLayout to understand why the problem is occurring and then have a look at Laying Out Components Within a Container for some possible solutions

JFrame frame = new JFrame();
//...
frame.setContentPane(backgroundLabel);
frame.setLayout(new GridBagLayout());

ImageIcon title = new ImageIcon("title.png");
JLabel titleLabel = new JLabel(title);
frame.add(titleLabel);
//...

You should be adding your titleLabel to the backgroundLabel, but you can "cheat" a little with JFrame, you can set the backgroundLabel as the "content pane" of the frame, this means that anything you add to the frame is actually added to the backgroundLabel.

Cavets

  • JLabel does not have a layout manage applied by default, this means that unless you apply one, anything you add to it will not be sized or positioned and will appear "invisible"
  • JLabel does not use the layout manager to calculate it's preferred size, instead, it relies on the size of the image and it's text. This may not be an issue in most cases, but if your content, for some reason, exceeds the size of the image (either horizontally or vertically), the content will be clipped. You could have a look at this example which shows a couple of different ways to achieve the same result
Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I did what you said, and it worked, but I'm making another mistake; how do I set the location of elements in a GridBagLayout? – Phyremaster Sep 15 '15 at 22:29
  • `GridBagLayout` is a complex beast (powerful, but complex), have a look at [How to Use GridBagLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html) to start with – MadProgrammer Sep 15 '15 at 23:42
  • I checked that article out, but I'm still having trouble. Is there any way to put something at exact coordinates using GridBagLayout? – Phyremaster Sep 22 '15 at 00:33
  • `gridx` and `gridy` and the constraints you define to specify the location within the virtual grid for components to reside. Pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify – MadProgrammer Sep 22 '15 at 00:37
  • Ok, I understand. Thank you for your help, and hopefully I can use all this info to make a proper layout thingy (that came off dumb). Anyway, thanks and see ya later. – Phyremaster Sep 22 '15 at 00:41