我有一个带有 JPanel 的 JFrame,其中有一个带有 ImageIcon() 的 JLabel。一切正常,问题是我现在想在 JFrame 中添加另一个 JPanel,其中包含按钮等所有其他内容。但它仍然在顶部显示背景图像,而第二个 JPanel 则没有。
有人能帮我吗?这是我的代码的摘录:
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);
}