java中如何让窗口的背景图片随着窗口的大小一起改变?

解决方案 »

  1.   

    将图片绘制在panel或直接frame里面 在窗口拉动的时候只要panel或frame变图片就会变
    重写paintcomponent或paint方法里面用
    g.drawImg画图
      

  2.   

    找一个jpg图片命名为test.jpg和这个类放在一起,直接运行就看到效果了,具体的实现里面注释写的很周到。
    import java.awt.BorderLayout;
    import java.awt.Graphics;
    import java.awt.Image;import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;/**
     * 可设置背景图片的JPanel,提供了三种显示背景图片的方式:居中、平铺和拉伸。
     * 未设置背景图片的情况下,同JPanel。
     * 
     * @author 003
     */
    public class JImagePane extends JPanel
    {
        private static final long serialVersionUID = -8251916094895167058L;
        
        /**
         * 居中
         */
        public static final String CENTRE = "Centre";
        
        /**
         * 平铺
         */
        public static final String TILED = "Tiled";    /**
         * 拉伸
         */
        public static final String SCALED = "Scaled";    /**
         * 背景图片
         */
        private Image backgroundImage;
        
        /**
         * 背景图片显示模式
         */
        private String imageDisplayMode;    /**
         * 背景图片显示模式索引(引入此属性有助于必要时扩展)
         */
        private int modeIndex;    /**
         * 构造一个没有背景图片的JImagePane
         */
        public JImagePane()
        {
            this(null, CENTRE);
        }
        
        /**
         * 构造一个具有指定背景图片和指定显示模式的JImagePane
         * @param image 背景图片
         * @param modeName 背景图片显示模式
         */
        public JImagePane(Image image, String modeName)
        {
            super();
            setBackgroundImage(image);
            setImageDisplayMode(modeName);
        }
        
        /**
         * 设置背景图片
         * @param image 背景图片
         */
        public void setBackgroundImage(Image image)
        {
            this.backgroundImage = image;
            this.repaint();
        }    /**
         * 获取背景图片
         * @return 背景图片
         */
        public Image getBackgroundImage()
        {
            return backgroundImage;
        }    /**
         * 设置背景图片显示模式
         * @param modeName 模式名称,取值仅限于ImagePane.TILED  ImagePane.SCALED  ImagePane.CENTRE
         */
        public void setImageDisplayMode(String modeName)
        {
            if(modeName != null)
            {
                modeName = modeName.trim();
                
                //居中
                if(modeName.equalsIgnoreCase(CENTRE))
                {
                    this.imageDisplayMode = CENTRE;
                    modeIndex = 0;
                }
                //平铺
                else if(modeName.equalsIgnoreCase(TILED))
                {
                    this.imageDisplayMode = TILED;
                    modeIndex = 1;
                }
                //拉伸
                else if(modeName.equalsIgnoreCase(SCALED))
                {
                    this.imageDisplayMode = SCALED;
                    modeIndex = 2;
                }
                
                this.repaint();
            }
        }    /**
         * 获取背景图片显示模式
         * @return 显示模式
         */
        public String getImageDisplayMode()
        {
            return imageDisplayMode;
        }    /**
         * 绘制组件
         * @see javax.swing.JComponent#paintComponent(Graphics)
         */
        @Override
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            
            //如果设置了背景图片则显示
            if(backgroundImage != null)
            {
                int width = this.getWidth();
                int height = this.getHeight();
                int imageWidth = backgroundImage.getWidth(this);
                int imageHeight = backgroundImage.getHeight(this);            switch(modeIndex)
                {
                    //居中
                    case 0:
                    {
                        int x = (width - imageWidth) / 2;
                        int y = (height - imageHeight) / 2;
                        g.drawImage(backgroundImage, x, y, this);
                        break;
                    }
                    //平铺
                    case 1:
                    {
                        for(int ix = 0; ix < width; ix += imageWidth)
                        {
                            for(int iy = 0; iy < height; iy += imageHeight)
                            {
                                g.drawImage(backgroundImage, ix, iy, this);
                            }
                        }
                        
                        break;
                    }
                    //拉伸
                    case 2:
                    {
                        g.drawImage(backgroundImage, 0, 0, width, height, this);
                        break;
                    }
                }
            }
        }
        
        public static void main(String[] args)
        {
            JFrame frame = new JFrame("JImagePane Test");
            Image iamge = new ImageIcon(JImagePane.class.getResource("test.jpg")).getImage();
            JImagePane imagePane = new JImagePane(iamge, JImagePane.SCALED);
            frame.getContentPane().add(imagePane, BorderLayout.CENTER);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(800, 600);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }
      

  3.   

    以下是SWT的实现
    import java.io.InputStream;import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.PaintEvent;
    import org.eclipse.swt.events.PaintListener;
    import org.eclipse.swt.graphics.Image;
    import org.eclipse.swt.graphics.ImageData;
    import org.eclipse.swt.graphics.Rectangle;
    import org.eclipse.swt.layout.FillLayout;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Shell;/**
     * 可设置背景图片的Composite
     * Composite本身是可以设置背景图片的,但是只提供了平铺的显示方式,该类实现了居中、平铺和拉伸三种显示方式。
     * 
     * @author 003
     */
    public class ImageComposite extends Composite
    {
        /**
         * 居中
         */
        public static final String CENTRE = "Centre";
        
        /**
         * 平铺
         */
        public static final String TILED = "Tiled";    /**
         * 拉伸
         */
        public static final String SCALED = "Scaled";    /**
         * 背景图片
         */
        private Image backgroundImage;
        
        /**
         * 背景图片显示模式
         */
        private String imageDisplayMode;    /**
         * 背景图片显示模式索引(引入此属性有助于必要时扩展)
         */
        private int modeIndex;    /**
         * 构造一个没有背景图片的ImageComposite
         * @param parent 父组件
         * @param style 风格
         */
        public ImageComposite(Composite parent, int style)
        {
            this(parent, style, null, CENTRE);
        }
        
        /**
         * 构造一个具有指定背景图片和指定显示模式的ImageComposite
         * @param parent 父组件
         * @param style 风格
         * @param image 背景图片
         * @param modeName 背景图片显示模式
         */
        public ImageComposite(Composite parent, int style, Image image, String modeName)
        {
            super(parent, style);
            addPaintListener(new PaintListener()
            {
                @Override
                public void paintControl(PaintEvent e)
                {
                    drawImage(e);
                }
            });
            setBackgroundImage(image);
            setImageDisplayMode(modeName);
        }    /**
         * 获取背景图片
         * @return 背景图片
         * @see org.eclipse.swt.widgets.Control#getBackgroundImage(Image)
         */
        @Override
        public Image getBackgroundImage()
        {
            return backgroundImage;
        }    /**
         * 设置背景图片
         * @param 背景图片
         * @see org.eclipse.swt.widgets.Control#setBackgroundImage(Image)
         */
        @Override
        public void setBackgroundImage(Image backgroundImage)
        {
            this.backgroundImage = backgroundImage;
            this.redraw();
        }    /**
         * 获取背景图片显示模式
         * @return 显示模式
         */
        public String getImageDisplayMode()
        {
            return imageDisplayMode;
        }    /**
         * 设置背景图片显示模式
         * @param modeName 模式名称,取值仅限于ImagePane.TILED  ImagePane.SCALED  ImagePane.CENTRE
         */
        public void setImageDisplayMode(String modeName)
        {
            if(modeName != null)
            {
                modeName = modeName.trim();
                
                //居中
                if(modeName.equalsIgnoreCase(CENTRE))
                {
                    this.imageDisplayMode = CENTRE;
                    modeIndex = 0;
                }
                //平铺
                else if(modeName.equalsIgnoreCase(TILED))
                {
                    this.imageDisplayMode = TILED;
                    modeIndex = 1;
                }
                //拉伸
                else if(modeName.equalsIgnoreCase(SCALED))
                {
                    this.imageDisplayMode = SCALED;
                    modeIndex = 2;
                }
                
                this.redraw();
            }
        }
        
        /**
         * 绘制背景
         * @param e PaintEvent
         */
        private void drawImage(PaintEvent e)
        {
            //如果设置了背景图片则显示
            if(backgroundImage != null)
            {
                int width = this.getSize().x;
                int height = this.getSize().y;
                int imageWidth = backgroundImage.getImageData().width;
                int imageHeight = backgroundImage.getImageData().height;
                
                switch(modeIndex)
                {
                    //居中
                    case 0:
                    {
                        int x = (width - imageWidth) / 2;
                        int y = (height - imageHeight) / 2;
                        e.gc.drawImage(backgroundImage, x, y);
                        break;
                    }
                    //平铺
                    case 1:
                    {
                        for(int ix = 0; ix < width; ix += imageWidth)
                        {
                            for(int iy = 0; iy < height; iy += imageHeight)
                            {
                                e.gc.drawImage(backgroundImage, ix, iy);
                            }
                        }
                        
                        break;
                    }
                    //拉伸
                    case 2:
                    {
                        ImageData data = backgroundImage.getImageData().scaledTo(width, height);
                        e.gc.drawImage(new Image(e.display, data), 0, 0);
                        break;
                    }
                }
            }
        }
        
        /**
         * 设置窗口位于屏幕中间
         * @param display 设备
         * @param shell 要调整位置的窗口对象
         */
        public static void center(Display display, Shell shell)
        {
            Rectangle bounds = display.getPrimaryMonitor().getBounds();
            Rectangle rect = shell.getBounds();
            int x = bounds.x + (bounds.width - rect.width) / 2;
            int y = bounds.y + (bounds.height - rect.height) / 2;
            shell.setLocation(x, y);
        }
        
        public static void main(String[] args)
        {
            Display display = Display.getDefault();
            InputStream is = ImageComposite.class.getResourceAsStream("test.jpg");
            Image image = new Image(display, new ImageData(is));
            Shell shell = new Shell();
            new ImageComposite(shell, SWT.NONE, image, ImageComposite.SCALED);
            shell.setText("ImageComposite Test");
            shell.setLayout(new FillLayout());
            shell.setSize(800, 600);
            center(display, shell);
            shell.open();
            shell.layout();
            
            while(!shell.isDisposed())
            {
                if(!display.readAndDispatch())
                {
                    display.sleep();
                }
            }        display.dispose();
        }
    }
      

  4.   

    还是找到漏洞了成功了。。谢谢。。哈哈
     Image iamge = new ImageIcon(JImagePane.class.getResource("test.jpg")).getImage();
    test.jpg前加个"/"就运行成功了。。郁闷的
      

  5.   

    GOD ME,你把test.jpg在哪里放的哦,我上面的代码是跟这个java文件放一起照你那样子我怎么调都不行,难道是JDK版本的问题?前面加个./的话把图片放在项目根目录可以运行
      

  6.   

    //package com.cn;import javax.swing.*;
    import java.awt.*;public class MyMun { /**
     * @param args
     */
    JLabel backlabel = null;
    JFrame chuangkou = null; public MyMun() {
    chuangkou = new JFrame("企业管理系统");
    // JPanel jpanel1=new JPanel();
    // jpanel1.setBackground(Color.red);
    backlabel = new JLabel();
    backlabel.setVerticalAlignment(SwingConstants.TOP);
    backlabel.setHorizontalAlignment(SwingConstants.CENTER);
    uptupian();
    JDesktopPane desktoppane = new JDesktopPane();
    desktoppane.add(backlabel, new Integer(Integer.MIN_VALUE));
    JPanel jpanel2 = new JPanel();
    jpanel2.setBackground(Color.black);
    //JPanel jpanel3 = new JPanel();
    //jpanel3.setBackground(Color.blue); Container conn = chuangkou.getContentPane();
    conn.add(desktoppane);
    // conn.add(jpanel1,BorderLayout.SOUTH);
    conn.add(jpanel2, BorderLayout.NORTH);
    //conn.add(jpanel3, BorderLayout.CENTER);
    // JMenuBar caidanlan = new JMenuBar();
    // caidanlan.setBorderPainted(falsh);
    /* JMenu caidan = new JMenu("基础信息管理");
    JMenu caidan1 = new JMenu("进货管理");
    JMenu caidan2 = new JMenu("销售管理");
    JMenu caidan3 = new JMenu("查询统计");
    JMenu caidan4 = new JMenu("库存管理");
    JMenu caidan5 = new JMenu("系统管理");
    caidanlan.add(caidan);
    caidanlan.add(caidan1);
    caidanlan.add(caidan2);
    caidanlan.add(caidan3);
    caidanlan.add(caidan4);
    caidanlan.add(caidan5);*/
    // chuangkou.setJMenuBar(caidanlan);
    chuangkou.setSize(800, 600);
    chuangkou.setVisible(true);
    } public void uptupian() {
    if (backlabel != null) {
    int backw = MyMun.this.chuangkou.getWidth();
    int backh = chuangkou.getHeight();
    backlabel.setSize(backw, backh);
    backlabel.setText("<html><body><image width='" + backw
    + "'height='" + (backh) + "'src="
    + MyMun.this.getClass().getResource("a.jpg")
    + "'></img></body></html>");
    } }
    static {
    try
    {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }catch(Exception e)
    {e.printStackTrace();}
    }
    public static void main(String[] args) {
    // TODO Auto-generated method stub new MyMun();
    }}
      

  7.   

    你确定是getResource("test.jpg")).getImage();吗????
    不是set???为什么是取而不是赋值呢?到底这个图片应该怎么去显示