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);
}
}
Last edited by 01010011; Nov 15th, 2008 at 08:28 AM.
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
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson My Blog
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
Last edited by 01010011; Nov 14th, 2008 at 07:56 PM.
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.
Last edited by 01010011; Nov 15th, 2008 at 08:26 AM.
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
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson My Blog
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!