当前位置: 代码迷 >> Java相关 >> [求助]JAPPLET下替换图像问题
  详细解决方案

[求助]JAPPLET下替换图像问题

热度:126   发布时间:2005-09-07 09:19:00.0
[求助]JAPPLET下替换图像问题

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目录下有几个图片,第一次显示没有问题,但下次替换为何就不行呢……

搜索更多相关的解决方案: JAPPLET  图像  

----------------解决方案--------------------------------------------------------
应该是IE给你缓存了,所以没有替换吧?
----------------解决方案--------------------------------------------------------
但我用JFrame也不能正常显示啊,应该不是IE缓存的问题吧……
----------------解决方案--------------------------------------------------------
///////////////////////////////////
//
//       try it  -:)
//
///////////////////////////////////
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 = "cat.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 update()
  {
    imgname = t.getText();
    img = getImage(getCodeBase(),imgname);
    can.setImg(img);
    can.repaint();
  }
  public void actionPerformed(ActionEvent a)
  {
    update();
  }
}

class canvas extends Canvas
{
  Image img;
  int w,h;
  public canvas(Image img)
  {
    this.img = img;
  }
  public void setImg(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);
  }
}
----------------解决方案--------------------------------------------------------
谢谢~~~
问个问题,为什么一定要定义一个方法来传递呢,用构造函数传递就不行啊……
曾经把update方法写在Canvas类里面,致使图像无法显示……呵呵
----------------解决方案--------------------------------------------------------
用构造函数传递也可以,我个人认为没这个必要。
下面是用构造函数实现的代码:
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 = "cat.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 update()
  {
    imgname = t.getText();
    img = getImage(getCodeBase(),imgname);
    p1.remove(can);
    can = new canvas(img);
    p1.add(can);
    p1.updateUI();
    can.repaint();
  }
  
  public void actionPerformed(ActionEvent a)
  {
    update();
  }
}

class canvas extends Canvas
{
  Image img;
  int w,h;
  public canvas(Image img)
  {
    this.img = img;
  }
  public void setImg(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);
  }
}
----------------解决方案--------------------------------------------------------
  相关解决方案