看简单的测试代码,正常情况下在鼠标press、release、drag时,窗口会显示mouse pressed\released\dragged。但在mouseReleased方法添加popupmenu的显示后,在单击(press)且没有拖动(drag)鼠标的情况下,仍会显示mouse dragged,这是为什么?哪位大侠能给解释一下????
import java.awt.*;
import java.awt.event.*;
public class TestMouse
{
public static void main(String[] args)
{
Frame f=new Frame("f");
final TextArea t=new TextArea(100,100);
final PopupMenu pop=new PopupMenu();
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
pop.add(new MenuItem("Popup"));
t.add(pop);
t.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
t.append("Mouse pressed.\n");
}
public void mouseReleased(MouseEvent e){
t.append("Mouse released.\n");
//添加下面一句后,没有拖动鼠标也会显示鼠标被拖动了,但第一次鼠标单击时显示正常,很奇怪
// pop.show (t,e.getX(),e.getY());
}
});
t.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
t.append("Mouse dragged.\n");
}
}); f.add(t);
f.pack();
f.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
public class TestMouse
{
public static void main(String[] args)
{
Frame f=new Frame("f");
final TextArea t=new TextArea(100,100);
final PopupMenu pop=new PopupMenu();
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
pop.add(new MenuItem("Popup"));
t.add(pop);
t.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
t.append("Mouse pressed.\n");
}
public void mouseReleased(MouseEvent e){
t.append("Mouse released.\n");
//添加下面一句后,没有拖动鼠标也会显示鼠标被拖动了,但第一次鼠标单击时显示正常,很奇怪
// pop.show (t,e.getX(),e.getY());
}
});
t.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
t.append("Mouse dragged.\n");
}
}); f.add(t);
f.pack();
f.setVisible(true);
}
}
//把坐标换成固定值就正常了
pop.show(t, 100, 100);
2、出错的原理是什么,能说清楚吗?
官方早不维护了(差不多10年了),有Bug也是正常换 Swing 看看吧,没这种现象