Background Image problem :(
Hello !
I have a problem with the background image when i use an internal frame. Here is my code :
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.Container;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
publicclass Main
{
JFrame fr =new JFrame("My Frame");
Container ct =new Container();
JDesktopPane dp =new JDesktopPane();
JInternalFrame ifr =new JInternalFrame("My Internal Frame", false, true, false,false);
ImageIcon icon =new ImageIcon("Logo.jpg");
public Main()
{
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fr.setBounds(250, 250, 800, 600);
fr.setResizable(false);
ct = fr.getContentPane();
ifr.setBounds(150, 150, 510, 150);
dp.add(ifr);
ct.add(dp);
/*JPanel panel = new JPanel()
{
protected void paintComponent(Graphics g)
{
g.drawImage(icon.getImage(), 0, 0, null);
}
};
panel.setOpaque( false );
panel.setPreferredSize( new Dimension(400, 400) );
JScrollPane scrollPane = new JScrollPane(panel);
fr.getContentPane().add(scrollPane);*/
JMenuBar mb =new JMenuBar();
JMenu m =new JMenu("Menu");
JMenuItem mi =new JMenuItem("Menu_Item");
mi.addActionListener(new ActionListener()
{
publicvoid actionPerformed(ActionEvent e)
{
ifr.setVisible(true);
}
});
m.add(mi);
mb.add(m);
fr.setJMenuBar(mb);
fr.setVisible(true);
}
publicstaticvoid main(String[] args)
{
new Main();
}
}
And the image (http://img172.imageshack.us/img172/8024/logors3.jpg). Name itLogo.jpg to compile the program.
My internal frame is not showing when i put the background image (Remove the comment to view the problem).
Thanks !
# 3
> I don't understand your solution
from the api docs for BorderLayout
<api docs>
A border layout lays out a container, arranging and resizing its components to
fit in five regions: north, south, east, west, and center. Each region may contain
no more than one component, and is identified by a corresponding constant:
NORTH, SOUTH, EAST, WEST, and CENTER. When adding a component to a
container with a border layout, use one of these five constants, for example:
Panel p = new Panel();
p.setLayout(new BorderLayout());
p.add(new Button("Okay"), BorderLayout.SOUTH);
As a convenience, BorderLayout interprets the absence of a string
specification the same as the constant CENTER:
Panel p2 = new Panel();
p2.setLayout(new BorderLayout());
p2.add(new TextArea()); // Same as p.add(new TextArea(), BorderLayout.CENTER);
</api docs>
so, in your code
ct = fr.getContentPane();
ct.add(dp);
fr.getContentPane().add(scrollPane);
scrollpane replaces dp, because
"Each region may contain no more than one component"
and
"As a convenience, BorderLayout interprets the absence of a string
specification the same as the constant CENTER: "
now, when you fix the code, ifr still may not show due to the bounds you have set.
comment out the setResizable(false) and, if ifr is not visible, drag the frame taller
# 4
Michael_Dunn : Thank you for your help !
camickr : After Micheal_Dunn's post, i understand your solution ! It's done !
Here is my new code :
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.Container;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Main
{
JFrame fr = new JFrame("My Frame");
Container ct = new Container();
JInternalFrame ifr = new JInternalFrame("My Internal Frame", false, true, false, false);
ImageIcon icon = new ImageIcon("Logo.jpg");
public Main()
{
fr.setBounds(250, 250, 800, 600);
fr.setResizable(false);
JDesktopPane dp = new JDesktopPane()
{
protected void paintComponent(Graphics g)
{
g.drawImage(icon.getImage(), 0, 0, null);
}
};
dp.setOpaque(false);
ifr.setBounds(150, 150, 510, 150);
dp.add(ifr);
fr.add(dp);
JMenuBar mb = new JMenuBar();
JMenu m = new JMenu("Menu");
JMenuItem mi = new JMenuItem("Menu_Item");
mi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
ifr.setVisible(true);
}
});
m.add(mi);
mb.add(m);
fr.setJMenuBar(mb);
fr.setVisible(true);
}
public static void main(String[] args)
{
new Main();
}
}
Thanks !