Alek345
Alek345

Reputation: 47

JLabel as Background Image

I can't seem to figure this out.
Please help I need this to work out to continue my project.
Awww I have to add this for allowing me to post

import javax.swing.*;
import java.awt.*;

@SuppressWarnings("serial")
public class MainFrame extends JFrame {


public static void Draw(){
    DrawFrame();
}


public static void DrawFrame(){
    int h = 600;
    int w = 340;
    JFrame frame = new JFrame();
    JLabel background1 = new JLabel(new ImageIcon("/res/mariocraft_main.png"));


    frame.setResizable(false);
    frame.setSize(h, w);
    frame.setTitle("MarioCraft");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.add(background1);

    background1.setVisible(true);
    background1.setIcon(new ImageIcon("/res/mariocraft_main.png"));
    background1.setText("Background failed to load");

    }

}

Upvotes: 0

Views: 23020

Answers (4)

gangu
gangu

Reputation: 59

import java.awt.Container;
import java.awt.FlowLayout;


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


public class Mainframe extends JFrame
{
    public JLabel image ;



    public Container c; 

    public Mainframe()
    {
        c=this.getContentPane();
        image=new JLabel(new ImageIcon("bg.jpg"));
        image.setSize(500, 550);

        c.setLayout(new FlowLayout());
        c.add(image);

         add(image);




         this.setSize(500, 550);
         this.show();
         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) 
    {
            new Mainframe();
    }

}

Upvotes: 0

JavaTechnical
JavaTechnical

Reputation: 9357

Do you want to set JLabel as background image for the JFrame. Then,

frame.setContentPane(new JLabel(new ImageIcon("someimage.jpg"));

See a sample code snippet taken for here

frame.setLayout(new BorderLayout());
frame.setContentPane(new JLabel(new ImageIcon("someimage.jpg")));
frame.setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am a button");
frame.add(l1);
frame.add(b1);

Upvotes: 0

camickr
camickr

Reputation: 324118

A JLabel always displays the image at its actual size so you should not be manually setting the size of the frame.

Instead the code should be something like:

JLabel background1 = new JLabel(new ImageIcon("/res/mariocraft_main.png"));

JFrame frame = new JFrame();     
frame.add(background1); 
frame.pack();
frame.setResizable(false);     
frame.setVisible(true);     

Upvotes: 4

mre
mre

Reputation: 44240

You need to add the JLabel instance to the JFrame before you realize it (i.e. make it visible). Also, remove these three calls:

background1.setVisible(true);
background1.setIcon(new ImageIcon("/res/mariocraft_main.png"));
background1.setText("Background failed to load");

They are completely unnecessary. Also, another approach to setting a background image to a component is to override it's paintComponent method and draw the image directly to it's Graphics object.

Upvotes: 1

Related Questions