about JPopupMenu
i wrote a JPanel capable of drawing lines on it,
now i want to pop up a JPopupMenu when clicking MouseButton3,
here is my code:
import javax.swing.*;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.*;
public class ScribblePane1 extends JPanel
implements MouseListener, MouseMotionListener {
private int last_x,last_y;
private JPopupMenu pop = new JPopupMenu();
public ScribblePane1() {
this.addMouseListener(this);
this.addMouseMotionListener(this);
setPreferredSize(new Dimension(300, 500));
pop.add(new JMenuItem("test"));
}
//mouse events
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
maybePopup(e);
last_x = e.getX();
last_y = e.getY();
}
public void mouseReleased(MouseEvent e) {
maybePopup(e);
}
public void maybePopup(MouseEvent e) {
if (e.isPopupTrigger()) {
pop.show((Component)e.getSource(), e.getX(),e.getY());
}
}
//mouse motion events
public void mouseDragged(MouseEvent e) {
this.getGraphics().drawLine(last_x, last_y, e.getX(),e.getY());
last_x = e.getX();
last_y = e.getY();
}
public void mouseMoved(MouseEvent e) {
}
}
but when pop.show() is invoked,
the lined drew already will be covered and disappear,
what's the problem? and what should i do to fix it?
sorry my english is not good,
hope you can understand what i want to say,
thanks all :)
|