Java JLayeredPane来实现窗口背景图片

时间:2023-01-28 15:34:35

用JLayeredPane实现在不影响窗口组件布局的情况下,添加一个背景图片:

Java JLayeredPane来实现窗口背景图片

调整窗口大小后:

Java JLayeredPane来实现窗口背景图片

package com.han;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

/**
 * The JLayeredPane has no layout manager by default, so you have to use 
 * setBounds() for each component in it. However, you can still assign a layout
 * manager to the layered pane.
 * <p>
 * Many programs use intermediate containers (such as panels) and their layout 
 * managers to lay out components on the same layer, but use absolute 
 * positioning to lay out components on different layers.
 * <p>
 * We can use frame.pack() or frame.setSize() whatever its layout method is 
 * (use a layout manager or not).
 * <p>
 * In fact, if we hesitate to use component.setPreferredSize() or 
 * component.setSize(), we first see the layout method employed to layout this 
 * component. If it has a layout manager, we use component.setPreferredSize(); 
 * else (no layout manager), we use component.setBounds().
 * <p>
 * Concerning the JLayeredPane, the <i>depth</i> is higher, the component is on
 * the top (of course, there are some depth conventions, like DEFAULT_LAYER, 
 * PALETTE_LAYER, ...). Within its depth, the position number is 0, top-most; 
 * -1, bottom-most; 0, 1, 2, 3, ... from top to bottom.
 * 
 * @author HAN
 *
 */
@SuppressWarnings("serial")
public class JLayeredPane_1 extends JFrame {

	public JLayeredPane_1() {
		// TODO Auto-generated constructor stub
		Container container = getContentPane();

		final JLayeredPane layeredPane = new JLayeredPane();
//		layeredPane.setBorder(BorderFactory.createTitledBorder(null, 
//				"layeredPane", TitledBorder.LEADING, TitledBorder.TOP, 
//				new Font(null, Font.BOLD + Font.ITALIC, 14), Color.RED));
		layeredPane.setBorder(BorderFactory.createTitledBorder("<html><b><i>" +
				"<u><font face='SansSerif' size='5' color='red'>layeredPane" +
				"</font></u></i></b></html>"));
		Dimension dimension = new Dimension(300, 310);
		layeredPane.setPreferredSize(dimension);
		
		final JPanel panelBg = new JPanel();
		final Image imageBg = Toolkit.getDefaultToolkit()
				.getImage(this.getClass().getResource("/images/Lighthouse.jpg"));
		ImageIcon imageIcon = new ImageIcon(imageBg.getScaledInstance
				(dimension.width, dimension.height, Image.SCALE_FAST));
		final JLabel bg = new JLabel(imageIcon);
		final Point origin = new Point(10, 30);
		final Rectangle rectangle = new Rectangle(origin, dimension);
		panelBg.setBounds(rectangle);
		panelBg.add(bg);
		
		final JPanel panelContent = new JPanel();
		JButton button = new JButton("button 1");
		JButton button2 = new JButton("button 2");		
		JButton button3 = new JButton("button 3");	
		panelContent.setBounds(rectangle);
		panelContent.setOpaque(false); // 设置为透明
		panelContent.add(button);
		panelContent.add(button2);
		panelContent.add(button3);
		
		layeredPane.add(panelBg, new Integer(0)); // the same to layeredPane.add(panelBg);
		layeredPane.add(panelContent, new Integer(1));
		
		container.add(layeredPane, BorderLayout.CENTER);
		
		addComponentListener(new ComponentAdapter() {
			public void componentResized(ComponentEvent e) {
//				System.out.println(layeredPane.getSize()); //此处必须用getSize()而不是用getPreferredSize()
//				System.out.println(layeredPane.getPreferredSize());
				Dimension dimensionNew = layeredPane.getSize();
				Rectangle rectangleNew = new Rectangle(origin, dimensionNew);
				panelBg.setBounds(rectangleNew);
				panelContent.setBounds(rectangleNew);
				bg.setIcon(new ImageIcon(imageBg.getScaledInstance
				(dimensionNew.width, dimensionNew.height, Image.SCALE_FAST)));
			}
		});
	}

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JLayeredPane_1 frame = new JLayeredPane_1();
		frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

}