报错:Exception in thread "main" java.lang.NoSuchMethodError: com.wwe.w96.ShadePanel.<init>(Ljava/awt/Image;)V
at com.wwe.w96.AutoImageSizeByForm.<init>(AutoImageSizeByForm.java:14)
at com.wwe.w96.AutoImageSizeByForm.main(AutoImageSizeByForm.java:22)
就是 ShadePanel contentPanel = new ShadePanel(Toolkit.getDefaultToolkit().getImage(getClass().getResource("psb.jpeg")));这一句package com.wwe.w96;import java.awt.*;
import javax.swing.*;public class AutoImageSizeByForm extends JFrame{

public AutoImageSizeByForm(){

setTitle("AutoImageSizeByForm");
setBounds(100,100,450,300);
ShadePanel contentPanel = new ShadePanel(Toolkit.getDefaultToolkit().getImage(getClass().getResource("psb.jpeg")));
//contentPanel.setImage();
getContentPane().add(contentPanel,BorderLayout.CENTER);
setVisible(true);

}

public static void main(String[]args){
AutoImageSizeByForm application = new AutoImageSizeByForm();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}class ShadePanel extends JPanel{

Image image;

public ShadePanel(Image image){
this.image = image;
}

public void paintComponent(Graphics g){

if(image!=null){
int width = getWidth();
int height = getHeight();
g.drawImage(image, 0, 0, width, height, this);
}
super.paintComponent(g);
}

}

解决方案 »

  1.   

    NoSuchMethodError,是不是你用的jar包版本不对?
    顺便问下,你不会再加班吧?
      

  2.   

    没引什么新的jar包啊,setImage()是我写的一个方法啊
      

  3.   

    貌似getClass()方法没有通过对象名调用
      

  4.   

    修改了一下,在我的机器能正常显示
    package com.wwe.w96;import java.awt.Graphics;
    import java.awt.Image;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;public class AutoImageSizeByForm extends JFrame { public AutoImageSizeByForm() { setTitle("AutoImageSizeByForm");
    setBounds(100, 100, 450, 300);
    Image img = new ImageIcon("psb.jpeg").getImage();
    ShadePanel contentPanel = new ShadePanel(img); this.add(contentPanel);
    setVisible(true); } public static void main(String[] args) {
    AutoImageSizeByForm application = new AutoImageSizeByForm();
    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }}class ShadePanel extends JPanel { Image image; public ShadePanel(Image image) {
    this.image = image;
    } public void paint(Graphics g) {
    super.paint(g);

    if (image != null) { int width = getWidth();
    int height = getHeight();
    g.drawImage(image, 0, 0, width, height, this);

    } }}