报错: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);
}
}
------解决方案--------------------------------------------------------
修改了一下,在我的机器能正常显示
- Java code
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); } }}