用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")
- publicclass 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() {
- publicvoid 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
- */
- publicstaticvoid 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);
- }
- }