简体   繁体   中英

Java Mouse Event - MouseClicked works fine but MousePressed and MouseReleased don't

I try to handle MousePressed event in class A, which extends from abstract class B and implements interface C, but I only handle MouseClicked, MousePressed does not work

interface 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);

}

Abstract class 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() {
    .........
}

}

My Class 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) {
    ................
}}

I don't understand "why only MouseClicked work fine, all of another event of mouse do not work".

thanks for all answer.

What you are claiming is not possible. MouseClicked is the little brother of MousePressed and MouseReleased... It is not a system event by itself, but rather a synthetic one generated by the timing between MousePressed and MouseReleased. For further proof of this, check out java.awt.Robot... see the MouseClicked method in there? No, because there isn't one.

Not possible. Case closed

Problem was solved, I found where define mouseEvent for A class, I added comment where I define new mouse event.

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);
    };
};
........................

}

Thanks for all answer!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM