More than one JPanel in a Frame / having a brackground Image and another Layer with Components on the top
Posted
by
user1905203
on Stack Overflow
See other posts from Stack Overflow
or by user1905203
Published on 2012-12-14T21:27:12Z
Indexed on
2012/12/14
23:03 UTC
Read the original article
Hit count: 139
I've got a JFrame with a JPanel in which there is a JLabel with an ImageIcon(). Everything's working perfectly, problem is i now want to add another JPanel with all the other stuff like buttons and so on to the JFrame. But it still shows the background Image on top and nothing with the second JPanel.
Can someone help me? Here is an extract of my code:
JFrame window = new JFrame("Http Download");
/*
* Background Section
*/
JPanel panel1 = new JPanel();
JLabel lbl1 = new JLabel();
/*
* Component Section
*/
JPanel panel2 = new JPanel();
JLabel lbl2 = new JLabel();
/*
* Dimension Section
*/
Dimension windowSize = new Dimension(800, 600);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
public HTTPDownloadGUI() {
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.setLayout(null);
panel1.setSize(windowSize);
panel1.setOpaque(false);
panel2.setLayout(null);
panel2.setSize(windowSize);
panel2.setOpaque(false);
lbl1.setSize(windowSize);
lbl1.setLocation(0, 0);
lbl1.setIcon(new ImageIcon(getClass().getResource("bg1.png")));
panel1.add(lbl1);
lbl2.setBounds(0, 0, 100, 100);
//lbl2.setIcon(new ImageIcon(getClass().getResource("bg2.png")));
lbl2.setBackground(Color.GREEN);
panel2.add(lbl2);
panel1.add(panel2);
window.add(panel1);
int X = (screen.width / 2) - (windowSize.width / 2);
int Y = (screen.height / 2) - (windowSize.height / 2);
window.setBounds(X,Y , windowSize.width, windowSize.height);
window.setVisible(true);
}
© Stack Overflow or respective owner