| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
12年前发布

为Java的JLayeredPane组件添加窗口背景图片

用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.   * 

* 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. *

* We can use frame.pack() or frame.setSize() whatever its layout method is * (use a layout manager or not). *

* 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(). *

* Concerning the JLayeredPane, the depth 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("" + "layeredPane" + "")); 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); } }

来自:http://blog.csdn.net/gaowen_han/article/details/8164720

 本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1352432197198.html
Java Swing Java开发