import java.awt.*; import java.awt.event.*; import javax.swing.JApplet; import javax.swing.*;
public class Img extends JApplet implements ActionListener { canvas can; JTextField t = new JTextField(20); JButton b = new JButton("替换"); JPanel p1 = new JPanel(new GridLayout(1,1)); JPanel p2 = new JPanel(); Image img; String imgname = "43906.gif"; public void init() { p2.add(t); p2.add(b); img = getImage(getCodeBase(),imgname); can = new canvas(img); p1.setBorder(BorderFactory.createTitledBorder("图像")); p1.add(can); getContentPane().add(p1,"Center"); getContentPane().add(p2,"South"); b.addActionListener(this); } public void actionPerformed(ActionEvent a) { imgname = t.getText(); img = getImage(getCodeBase(),imgname); can = new canvas(img); repaint(); } } class canvas extends Canvas { Image img; int w,h; public canvas(Image img) { this.img = img; } public void paint(Graphics g) { w = img.getWidth(this); h = img.getHeight(this); g.drawImage(img,10,10,w,h,this); } } 在class目录下有几个图片,第一次显示没有问题,但下次替换为何就不行呢……