0

I am studying swing in java. I would like to display two images but at the moment my code only shows one image. How would I go about displaying two separate images inside my JFrame?

Here is the code that I currently use to display the image:

public void init_background(BufferedImage imageBuf) {
    jFrame.setContentPane(new JLabel(new ImageIcon(imageBuf)));
    ...
}

How should I change it to allow multiple images to be shown inside my JFrame?

sprinter
  • 27,148
  • 6
  • 47
  • 78
K. Ms
  • 85
  • 10
  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 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). 3) Exactly how do you want the two images displayed? Beside each other? One with transparency, directly on top of another image without? .. 4) I would not recommend using a `JLabel` to display a background image. Instead use a `JPanel` that custom paints the image. This way, when components (e.g a label with another image) are added to the panel .. – Andrew Thompson Mar 17 '17 at 13:41
  • 1
    .. the panel will return the correct preferred size for the use of the layout manage, and the `pack()` method of the top level container. 5) `jFrame.setLocation((int) (di.getWidth() / 2 - di1.getWidth() / 2), (int) (di.getHeight() / 2 - di1.getHeight() / 2));` Much shorter - `jFrame.setLocationRelativeTo(null);` but better still, `jFrame.setLocationByPlatform(true);` – Andrew Thompson Mar 17 '17 at 13:42
  • 2
    `this code is show the one image.` - I don't see how. The "init_background" method is the only method that adds a image (ie label) to the frame and that method is never invoked. – camickr Mar 17 '17 at 14:48
  • Thanks for all the answers! I found a solution!! – K. Ms Mar 18 '17 at 13:13
  • Cut out useless code. – sprinter Mar 20 '17 at 03:04
  • *"I found a solution!!"* Then either delete the question, or answer it below. – Andrew Thompson Mar 20 '17 at 21:17

0 Answers0