日期:2014-05-20  浏览次数:20689 次

java Canvas里图片不能完全显示
import java.awt.*;
import java.awt.event.*;
public class AWTDrawing2{
public static void main(String[] args){
Frame frame = new Frame("图片");
MyCanvas mc = new AWTDrawing2().new MyCanvas();
Image image = Toolkit.getDefaultToolkit().getImage("src\\image\\{0E292FB7-2E5E-D59D-03B6-EB712AD47410}.jpg");
mc.setImage(image);
ScrollPane sp = new ScrollPane();
sp.add(mc);
frame.add(sp,"Center");
frame.setSize(400,400);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
frame.setVisible(true);
}
//nested class MyCanvas
class MyCanvas extends Canvas{
private Image im;
public void setImage(Image im){
this.im = im;
}
public void paint(Graphics g){
g.drawImage(im,0,0,this);
}
}
}

我读取的图片是一张大图,height超过屏幕的像素,我加了Scrollbar,为什么还不能完全显示呢?

------解决方案--------------------
把ScrollPanel再加到另一个Panel中成为他的component之一