在java窗口程序中添加背景图片

来源:互联网 发布:vimeo视频下载软件 编辑:程序博客网 时间:2024/06/05 18:42
String path = "E:\\welcome\\1.jpg"; // 背景图片的路径。(相对路径或者绝对路径。本例图片放于"java项目名"的文件下)
ImageIcon background = new ImageIcon(path); // 背景图片
JLabel label = new JLabel(background); // 把背景图片显示在一个标签里面
label.setBounds(0, 0, this.getWidth(), this.getHeight()); // 把标签的大小位置设置为图片刚好填充整个面板
JPanel imagePanel = (JPanel) this.getContentPane(); // 把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明
imagePanel.setOpaque(false);
this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE)); // 把背景图片添加到分层窗格的最底层作为背景
0 0
原创粉丝点击