我尝试在A类中处理MousePressed事件,它从抽象类B扩展并实现接口C,但我只处理MouseClicked,MousePressed不起作用
接口C
public interface C {
public void setText(String msg, Dimension d);
public void setVisible(boolean b);
public boolean isVisible();
public Dimension getSize();
public void setLocation(Point p);
}
抽象类B
public abstract class B {
public B() {
}
int x, y, w, h, size;
private boolean visible = false;
MouseListener mouseListener = null;
boolean isInner(int p_x, int p_y) {
......
}
public void addMouseListener(MouseListener l) {
mouseListener = l;
}
public void setVisible(boolean b) {
.....
}
public boolean isVisible() {
........
}
public void repaint() {
.......
}
public void setSize(int size) {
.......
}
public void setLocation(int x, int y) {
........
}
public Dimension getSize() {
.......
}
public Point getLocationOnScreen() {
.........
}
}
我的A级:
public class A extends B implements C {
private String _text;
private String[] _lines;
private Color _backgroundColor = new Color(255, 255, 192);
private Color _borderColor = new Color(128, 128, 128);
private Color _foreColor = Color.black;
private int _fontSize = 9;
private boolean inner_viewer = false;
private TextLayout textLayout;
private TextLayout _lstTextLayout[];
public A(String msg, Dimension d, int fontSize, Color foreColor, Color bkColor, Color borderColor) {
setText(msg, d, fontSize, foreColor, bkColor, borderColor);
inner_viewer = EXE_NAME.equals(D.get_extensionToolCommand());
this.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
System.err.println("Released");
}
@Override
public void mousePressed(MouseEvent e) {
System.err.println("Pressed");
}
@Override
public void mouseExited(MouseEvent e) {
System.err.println("Exited");
}
@Override
public void mouseEntered(MouseEvent e) {
System.err.println("Entered");
}
@Override
public void mouseClicked(MouseEvent e) {
System.err.println("Click");
}
});
}
@Override
public void setText(String msg, Dimension d, int fontSize, Color foreColor, Color bkColor, Color borderColor) {
this._fontSize = fontSize;
this._foreColor = foreColor;
this._backgroundColor = bkColor;
this._borderColor = borderColor;
this._text = msg;
this.w = d.width;
this.h = d.height;
this._lines = _text.split("\n");
this._lstTextLayout = new TextLayout[_lines.length];
}
@Override
public void setLocation(Point p) {
super.setLocation(p.x, p.y);
}
public void paint(Graphics g) {
................
}}
我不明白"为什么只有MouseClicked才能正常工作,所有其他鼠标事件都无法正常工作"。
感谢所有回答。
答案 0 :(得分:1)
您声称的内容是不可能的。 MouseClicked是MousePressed和MouseReleased的小兄弟......它本身不是一个系统事件,而是一个由MousePressed和MouseReleased之间的时间生成的合成的事件。有关此问题的进一步证明,请查看java.awt.Robot ...请参阅那里的MouseClicked方法?不,因为没有一个。
不可能。案件结案
答案 1 :(得分:0)
问题解决了,我找到了为A类定义mouseEvent的地方,我在定义新鼠标事件时添加了注释。
Class AScreen:
public class AScreen extends JWindow {
..................................
private MouseListener callIfNeed(A a) {
if (a == null || a.mouseListener == null) {
final MouseListener dummy = new MouseAdapter() {
};
return dummy;
}
return a.mouseListener;
}
private InnerAnnotateSticky frontA(MouseEvent e1) {
Point p = e1.getPoint();
for (int i = front_element.size() - 1; i >= front.size(); i--) {
if (front_element.get(i).isInner(p.x, p.y))
return (A) front_element.get(i);
}
return null;
}
private enum MOUSE_EVENT {
CLICK,
//define new mouse event
PRESSED,
RELEASED,
}
private MouseAdapter adapter = new MouseAdapter() {
private void mouseEventUpdate(MOUSE_EVENT event, MouseEvent e) {
synchronized (getInstance()) {
if (event.equals(MOUSE_EVENT.CLICK)) {
A a = frontA(e);
MouseListener listener = callIfNeed(a);
if (listener != null) {
listener.mouseClicked(e);
}
}
// add new mouse event
if (event.equals(MOUSE_EVENT.PRESSED)) {
A a = frontA(e);
MouseListener listener = callIfNeed(a);
if (listener != null) {
listener.mousePressed(e);
}
}
if (event.equals(MOUSE_EVENT.RELEASED)) {
A a = frontA(e);
MouseListener listener = callIfNeed(a);
if (listener != null) {
listener.mouseReleased(e);
}
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
mouseEventUpdate(MOUSE_EVENT.CLICK, e);
}
//I add new 2 override method
@Override
public void mousePressed(MouseEvent e) {
mouseEventUpdate(MOUSE_EVENT.PRESSED, e);
};
@Override
public void mouseReleased(MouseEvent e) {
mouseEventUpdate(MOUSE_EVENT.RELEASED, e);
};
};
........................
}
感谢所有回答!