int backw = JXCFrame.this.frame.getWidth();
int backh = frame.getHeight(); // 获取窗口的宽和高
backLabel.setSize(backw, backh); // 设置标签的宽和高

// 导入背景图片
backLabel.setText("<html><body><image width ='"
+ backw + "'height ='" + (backh-90) + "'src="
+ JXCFrame.this.getClass().getResource("welcome1.jpg")
+ "'></img></body></html>");如上代码,frame是一个普通的JFrame,backLabel是一个普通的JLabel,现在把backLabel放进frame里去了,但图片不能自动缩放哪位大虾能帮我解决这个问题?

解决方案 »

  1.   

    不是很明确楼主想要实现的功能,自动缩放是明白了,可以实现,但是JFrame仅仅只是放一个图片么?
      

  2.   

    图片缩放可能要自己写
    bufferedimage有getscaledInstance方法,可以采用其中的SMOOTH参数,即用区域平均采样的方法缩放得到缩放后的图片根据这个思路,可以先获得bufferedimage的缩放示例,然后在jframe上绘制——或者自己继承一个JFrame,在onPaint方法中复写,应用上述逻辑。
      

  3.   


    你可以这样理解,JFrame仅仅只是放一个图片,因为我frame的NORTH放的是其它,NORTH以下放的是这个图片
      

  4.   


    这个好像是awt的吧?swing的没有吗?
      

  5.   

    这是CSDN上已经有的一个实现,贴出来
    import   java.awt.*; 
    import   java.awt.BorderLayout; 
    import   java.awt.Component; 
    import   java.awt.Graphics; 
    import   java.awt.Graphics2D; import   javax.swing.Icon; 
    import   javax.swing.ImageIcon; 
    import   javax.swing.JFrame; 
    import   javax.swing.JLabel;
    import   javax.swing.JButton; 
    import   java.awt.image.*;public   class   MIcon   implements   Icon   { 
    private  BufferedImage i=null;
            private   Icon   icon   =   null;         public   MIcon(Icon   icon)   { 
                    this.icon   =   icon; 
    //                icon=new ImageIcon(i);
            }         public   int   getIconHeight()   { 
                    return   icon.getIconHeight(); 
            }         public   int   getIconWidth()   { 
                    return   icon.getIconWidth(); 
            }         public   void   paintIcon(Component   c,   Graphics   g,   int   x,   int   y)   { 
                    float   wid   =   c.getWidth(); 
                    float   hei   =   c.getHeight(); 
                    int   iconWid   =   icon.getIconWidth(); 
                    int   iconHei   =   icon.getIconHeight();                 Graphics2D   g2d   =   (Graphics2D)   g; 
                    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,   
                            RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
                    g2d.scale(wid/iconWid,   hei/iconHei); 
                    icon.paintIcon(c,   g2d,   0,   0); 
            }         public   static   void   main(String[]   args)   { 
                    MIcon   icon   =   new   MIcon(new   ImageIcon( "resources/images/thu3.jpg ")); 
                    JLabel   label   =   new   JLabel(icon); 
                    JFrame   frame   =   new   JFrame(); 
                    frame.getContentPane().add(label,   BorderLayout.CENTER); 
    //                frame.getContentPane().add(new JButton("click"),BorderLayout.NORTH);
                    frame.setSize(800,   600); 
                    frame.setLocationRelativeTo(null); 
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
                    frame.setVisible(true); 
            } 
      

  6.   

    楼主运行上面代码,注意更改图片路径,我用的是工程根目录下resources目录下的子目录images下面的thu3.jpg,你换成自己的一副图片吧
      

  7.   

    你们的方法试过,不行,我自己已经解决这个问题了,跟大家分享一下
    //设置窗口放大缩小时改变图片大小
    frame.addComponentListener(new ComponentAdapter()
    {
    @Override
    public void componentResized(ComponentEvent e)
    {
    updateBack();
    }
    });        private void updateBack()
    {
    if (null != backLabel)
    { int backw = JXCFrame.this.frame.getWidth();
    int backh = frame.getHeight(); // 获取窗口的宽和高
    backLabel.setSize(backw, backh); // 设置标签的宽和高
    backLabel.setLayout(new BorderLayout());

    // 导入背景图片
    backLabel.setText("<html><body><image width ='"
    + backw + "'height ='" + (backh-90) + "'src="
    + JXCFrame.this.getClass().getResource("welcome1.jpg")
    + "'></img></body></html>");
    }
    }