java JFrame中插入背景图片,并且不会覆盖标签的图片要怎么做?

我用标签插入背景图片后我再用标签插入图片就会背覆盖显示不出来了,百度了好久没找到答案... 我用标签插入背景图片后 我再用标签插入图片就会背覆盖显示不出来了,百度了好久没找到答案 展开
 我来答
藤原子大雄
2018-11-11 · TA获得超过7195个赞
知道大有可为答主
回答量:6977
采纳率:82%
帮助的人:1707万
展开全部
private JFrame frame = new JFrame("背景图片测试");

private JPanel imagePanel;

private ImageIcon background;

public static void main(String[] args) {
new bj();
}

public bj() {
background = new ImageIcon("C:\\Users\\Pictures\\2.jpg");// 背景图片
JLabel label = new JLabel(background);// 把背景图片显示在一个标签里面
// 把标签的大小位置设置为图片刚好填充整个面板
label.setBounds(0, 0, background.getIconWidth(),
background.getIconHeight());
// 把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明
imagePanel = (JPanel) frame.getContentPane();
imagePanel.setOpaque(false);
// 内容窗格默认的布局管理器为BorderLayout
imagePanel.setLayout(new FlowLayout());
imagePanel.add(new JButton("测试按钮"));

frame.getLayeredPane().setLayout(null);
// 把背景图片添加到分层窗格的最底层作为背景
frame.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(background.getIconWidth(), background.getIconHeight());
frame.setResizable(false);
frame.setVisible(true);
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式