贴出源代码,不知道程序错在哪? ( 积分: 200 )

  • 主题发起人 主题发起人 gxhu
  • 开始时间 开始时间
G

gxhu

Unregistered / Unconfirmed
GUEST, unregistred user!
我想在窗口上放一些图,希望这些图可以激发鼠标事件。请帮我看看,急!!!
//源程序清单ShowImage.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class ShowImage extends Applet{
Image picture;
//定义类型为Image的成员变量
NewCanvas newCanvas;
public void init()
{
picture=Toolkit.getDefaultToolkit().getImage("lan.gif");
//装载图像
newCanvas= new NewCanvas(picture,this,"172.16.16.43");
newCanvas.ix=20;
newCanvas.iy=30;
newCanvas.sx=20;
newCanvas.sy=20;
newCanvas.setVisible(true);
}
public void paint(Graphics g){
newCanvas.paint(g);
}
public static void main(String args[]){
Frame frame=new Frame("This is a test");
frame.setSize(800,600);

Applet ap=new ShowImage();
ap.init();
frame.add(ap);
ap.start();
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}

}
//源程序清单NewCanvas.java
import java.awt.*;
import java.awt.event.*;
public class NewCanvas extends Canvas{
ShowImage parent;
Image img=null;
int ix=0;
int iy=2;
int sx=0;
int sy=0;

boolean mouse=false;
String ipAddr;
Dimension dim;

public NewCanvas(Image i, ShowImage p, String ip)
{
super();
img = i;
parent = p;
this.ipAddr = ip;
}

void SetImage(Image i)
{
img = i;
}

void SetImagexy(int x, int y)
{
ix = x;
iy = y;
}

void SetStringxy(int x, int y)
{
sx = x;
sy = y;
}

public void paint(Graphics g)
{
if ( img != null ) g.drawImage(img,ix,iy,40,40,Color.white,this);
if ( ipAddr != null )
{
g.drawString(ipAddr,sx,sy);
}
if ( mouse )
{
dim = size();
g.setColor(Color.blue);
g.drawRect(0,0,dim.width-1,dim.height-1);
}
}
public boolean mouseEnter(Event evt, int x, int y)
{
mouse = true;
repaint();
return true;
}
public boolean mouseExit(Event evt, int x, int y)
{
mouse = false;
repaint();
return true;
}
public boolean mouseDown(Event evt, int x, int y)
{
Frame helpDialog=new Frame() ;
helpDialog.setTitle("this is a dial");
helpDialog.show();
return true;

}
}
 
我想在窗口上放一些图,希望这些图可以激发鼠标事件。请帮我看看,急!!!
//源程序清单ShowImage.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class ShowImage extends Applet{
Image picture;
//定义类型为Image的成员变量
NewCanvas newCanvas;
public void init()
{
picture=Toolkit.getDefaultToolkit().getImage("lan.gif");
//装载图像
newCanvas= new NewCanvas(picture,this,"172.16.16.43");
newCanvas.ix=20;
newCanvas.iy=30;
newCanvas.sx=20;
newCanvas.sy=20;
newCanvas.setVisible(true);
}
public void paint(Graphics g){
newCanvas.paint(g);
}
public static void main(String args[]){
Frame frame=new Frame("This is a test");
frame.setSize(800,600);

Applet ap=new ShowImage();
ap.init();
frame.add(ap);
ap.start();
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}

}
//源程序清单NewCanvas.java
import java.awt.*;
import java.awt.event.*;
public class NewCanvas extends Canvas{
ShowImage parent;
Image img=null;
int ix=0;
int iy=2;
int sx=0;
int sy=0;

boolean mouse=false;
String ipAddr;
Dimension dim;

public NewCanvas(Image i, ShowImage p, String ip)
{
super();
img = i;
parent = p;
this.ipAddr = ip;
}

void SetImage(Image i)
{
img = i;
}

void SetImagexy(int x, int y)
{
ix = x;
iy = y;
}

void SetStringxy(int x, int y)
{
sx = x;
sy = y;
}

public void paint(Graphics g)
{
if ( img != null ) g.drawImage(img,ix,iy,40,40,Color.white,this);
if ( ipAddr != null )
{
g.drawString(ipAddr,sx,sy);
}
if ( mouse )
{
dim = size();
g.setColor(Color.blue);
g.drawRect(0,0,dim.width-1,dim.height-1);
}
}
public boolean mouseEnter(Event evt, int x, int y)
{
mouse = true;
repaint();
return true;
}
public boolean mouseExit(Event evt, int x, int y)
{
mouse = false;
repaint();
return true;
}
public boolean mouseDown(Event evt, int x, int y)
{
Frame helpDialog=new Frame() ;
helpDialog.setTitle("this is a dial");
helpDialog.show();
return true;

}
}
 
声明:可以用Applet类
 
问题有这么难?居然没人能回答?
 
你的程序出现循环引用了 ! 这样不好的 !
只要把 第二个类 class NewCanvas
中的 ShowImage parent;
变成 Object parent 就可以了
 
to-->hulun,谢谢您的回复!我按您的建议改了,可是用鼠标在图象上点右键时还是没有激发鼠标事件呀?
 
public class NewCanvas extends Canvas
这样写好像不对吧,你应该还要实现鼠标事件接口才行,才会触发鼠标事件
你改成
public class NewCanvas extends Canvas implements MouseListener试试
 
你可以按楼上的方法试一下 就好了
 
非常感谢二位!!!
我还有一个问题(与这个问题还有点关系),重新开了一个贴子,请二位光顾![:)]
 
后退
顶部