[RESOLVED] Background Image JLabel
Hello
I want to create a JPanel with a background image, and on that background image I want to arrange my buttons and text feilds etc in a border layout. Is this possibe? Having real trouble accomplishing this. Any assistence will be appreciated. I am using Java 2 at the moment. Thanks in advance
Code:
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.BorderLayout;
public class GUI2 extends JFrame
{
ImageIcon icon;
Image image;
public GUI2()
{
icon = new ImageIcon("background.jpg");
JPanel panel = new JPanel()
{
protected void paintComponent(Graphics g)
{
// Dispaly image at full size
g.drawImage(icon.getImage(), 0, 0, null);
super.paintComponent(g);
}
};
panel.setOpaque( false );
panel.setPreferredSize( new Dimension(400, 400) );
getContentPane().add( panel );
panel.setLayout(new BorderLayout());
JPanel southPanel = new JPanel();
JButton button = new JButton( "Hello" );
southPanel.setOpaque(true);
button.setOpaque(true);
southPanel.add(button);
add(southPanel, BorderLayout.NORTH);
// southPanel.add(button);
// southPanel.add(b);
}
public static void main(String [] args)
{
GUI2 frame = new GUI2();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(450, 250);
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}
Re: Background Image JLabel
Ok
#1 There is no Java2 because there is no Java1 (Not since 1997, the 2 was dropped officially last year)
#2 You're code is working fine and is doing everything you said you wanted to do.
#3 What's with the whitespace!! so many empty lines
Re: Background Image JLabel
Quote:
Originally Posted by ComputerJy
Ok
#1 There is no Java2 because there is no Java1 (Not since 1997, the 2 was dropped officially last year)
#2 You're code is working fine and is doing everything you said you wanted to do.
#3 What's with the whitespace!! so many empty lines
Thank you for your reply ComputerJy.
My problem is that I am trying to get my JPanel to show the background image instead of the default light blue background. In other words, when I add a panel to the image, a block around the panel shows the default light blue background, instead of the background image.
Maybe I am using the setOpaque(boolean); wrong
Re: Background Image JLabel
Quote:
Originally Posted by ComputerJy
#3 What's with the whitespace!! so many empty lines
Yeah, sorry about the excessive white space. Thanks!
1 Attachment(s)
Re: Background Image JLabel
Here's what I'm getting
What's wrong with it? cause I don't see anything blue except for the background
Re: Background Image JLabel
When I setLayout over the background image to BoxLayout, or GridLayout, parts of the background picture gets cut out to reveal the default color instead of the picture. I actually want the background picture to remain even when I change the border layout of the main panel. Using your picture as an example, I don't want the default blue background, I want the picture of the sky to fill the panel regardless of where I place buttons and regardless of which Border layout I use.
1 Attachment(s)
Re: Background Image JLabel
I still don't have a clear idea of what you want. But if what you want is something like the attached screenshot. Then add the southPanel to the JPanel panel instead of adding it to the JFrame itself, and set it (the southPanel) to transparent instead of opaque
Re: Background Image JLabel
Quote:
Originally Posted by ComputerJy
I still don't have a clear idea of what you want. But if what you want is something like the attached screenshot. Then add the southPanel to the JPanel panel instead of adding it to the JFrame itself, and set it (the southPanel) to transparent instead of opaque
Right, that worked, your second picture shows exactly what I want -- the button with the sky background. Thank you very much ComputerJy!