javaGUI设置背景图片

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Demo  extends JFrame{
	Deme2(){
		//图片路径
		String path = "C:/Users/44693/Desktop/6666/wi.jpg";
		//背景图片
		ImageIcon background = new ImageIcon(path);
		//将背景图片显示在一个标签里
	    JLabel imagelabel = new JLabel(background); 
		
	    // 把标签的大小位置设置为图片刚好填充整个面板  
		imagelabel.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());  
		
		// 把背景图片添加到分层窗格的最底层作为背景 
     	this.getLayeredPane().add(imagelabel, new Integer(Integer.MIN_VALUE)); 
		
		// 把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明 
		JPanel imagepanel = (JPanel) this.getContentPane();  
		
		//调用setOpaque(false);设置是否透明 
		imagepanel.setOpaque(false);  
		//设置窗体大小刚好为图片大小
		this.setSize(background.getIconHeight(), background.getIconHeight());
		//设置窗体居中
		this.setLocationRelativeTo(null);
		//设置窗体可关闭
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//设置窗体可见
		this.setVisible(true);
		
	}
	
	public static void main(String[] args) {
		new Demo();
	}
}

猜你喜欢

转载自blog.csdn.net/qq_41262903/article/details/80541019
今日推荐