user7355129
user7355129

Reputation:

Java JFrame - Add label-image

So i'm trying to add an image to my JFrame and i'm getting this. I'm using BorderLayout.PAGE_START but i don't want the grey background. Is there a way to "remove" that background or do it with another Layout and get the result that I want?

*I'm also going to add some images to the bottom of the frame so I will also not want to have a grey background there too.

edited: this is my code:

private JFrame getCreatedFrame(){
    JFrame frame = new JFrame("test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(screenSize);
    frame.setLocationRelativeTo(null);
    JFrame.setDefaultLookAndFeelDecorated(true);

    //set icon image
    String imgName = "images/domino.png";
    URL imageURL = getClass().getResource(imgName);
    if (imageURL != null) {
        icon = new ImageIcon(imageURL);
    }
    frame.setIconImage(icon.getImage());

    //set background image
    imgName = "images/background.jpg";
    imageURL = getClass().getResource(imgName);
    if (imageURL != null) {
        icon = new ImageIcon(imageURL);
    }
    JLabel background=new JLabel(icon);

    frame.add(background);
    return frame;
}

public void start() {
    short version=0,choice=0;
    JFrame frame=getCreatedFrame();

    //set welcome image
    String imgName = "images/welcome.png";
    URL imageURL = getClass().getResource(imgName);
    if (imageURL != null) {
        icon = new ImageIcon(imageURL);
    }
    JLabel welcome=new JLabel(icon);

    frame.add(welcome,BorderLayout.PAGE_START);

    frame.setVisible(true);
}

Upvotes: 1

Views: 1036

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Suggestions:

  • Make your code more OOP-compliant
  • Consider creating non-static fields for objects that need to be shared by methods of the class. Either that or getting both images in the same method
  • I gear my Swing GUI's towards making JPanels for flexibility since they can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. Your window looks like it's an introductory window which is usually a dialog (such as JDialog) and not an application window (e.g., JFrame).
  • You want to show one image on top of another, and there are several ways to do this, including drawing both images within a JPanel's paintComponent(...) method override.
  • Or if you want to use JLabels, simply give the bottom JLabel a layout manager, such as FlowLayout, and add the smaller JLabel to the bottom one.

For example, something like:

import java.awt.FlowLayout;
import java.awt.Image;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

@SuppressWarnings("serial")
public class WelcomePanel extends JPanel {

    public WelcomePanel(Image backGround, Image foreGround) {
        JLabel backGroundLabel = new JLabel(new ImageIcon(backGround));
        JLabel foreGroundLabel = new JLabel(new ImageIcon(foreGround));

        // if you want the welcome image away from the edge, then give
        // backGroundLabel an empty border with appropriate insets. For example:
        // backGroundLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        backGroundLabel.setLayout(new FlowLayout());
        backGroundLabel.add(foreGroundLabel);
    }

    private static void createAndShowGui() {
        String backImgName = "images/domino.png";
        String foreImgName = "images/welcome.png";
        URL backImageURL = WelcomePanel.class.getResource(backImgName);
        URL foreImageURL = WelcomePanel.class.getResource(foreImgName);
        Image backGroundImg = null;
        Image foreGroundImg = null;
        if (backImageURL != null && foreImageURL != null) {
            try {
                backGroundImg = ImageIO.read(backImageURL);
                foreGroundImg = ImageIO.read(foreImageURL);
            } catch (IOException e) {
                e.printStackTrace();
                System.exit(-1);
            }
        }

        WelcomePanel mainPanel = new WelcomePanel(backGroundImg, foreGroundImg);

        // or perhaps better to use a JDialog
        JFrame frame = new JFrame("Image On Image");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

Upvotes: 1

Related Questions