我在应该做的简单任务中遇到问题。
下列类表示带有图片的JPanel。
每次我通过拖放来绘制形状后,我都希望鼠标对框架/面板/组件无响应。我正在尝试通过以各种可能的方式删除mouselisteners来做到这一点,就像在方法mouseReleased(...)中所看到的那样
但是,发生的事情是,当我完成绘制形状后,鼠标将继续响应,并且每当我按下框架上的按钮时,鼠标就会继续绘制形状(逻辑有些缺陷)。
如何删除mouselistener,以便在addShape()方法完成后可以单击并使用鼠标做我想做的一切?
谢谢!
包问题2;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class PicturePanel extends JPanel { // TODO
static final int NONE = -1,
LINES_ONLY = 0, BOUNDED_SHAPES = 1, ALL_SHAPES = 2,
RECTANGLE = 0, OVAL = 1, LINE = 2,
DELETION = 11;
private int shapesMode;
private int actionMode;
private Picture<Shape> picture;
private JPanel controls;
private JButton addShapeBtn, removeShapeBtn;
private Shape tempShape; // for drawing the picture by dragging
private question2.Point startDrag, endDrag;
public PicturePanel(int shapesMode) {
this.shapesMode = shapesMode;
picture = new Picture<Shape>();
addShapeBtn = new JButton("Add shape");
removeShapeBtn = new JButton("Remove shape");
ControlsListener l = new ControlsListener();
addShapeBtn.addActionListener(l);
removeShapeBtn.addActionListener(l);
controls = new JPanel();
controls.add(addShapeBtn);
controls.add(removeShapeBtn);
this.setLayout(new BorderLayout());
this.add(controls, BorderLayout.SOUTH);
tempShape = new Line(0, 0, 1000, 100, Color.black);
picture.add(tempShape);
}
private class MouseListener extends MouseAdapter implements MouseMotionListener {
public void mouseClicked(MouseEvent e) {
switch (actionMode) {
case DELETION:
question2.Point clickPosition = new question2.Point(e.getX(), e.getY());
picture.remove(clickPosition);
PicturePanel.this.removeMouseListener(this);
repaint();
actionMode = NONE;
break;
}
question2.Point clickPosition = new question2.Point(e.getX(), e.getY());
picture.remove(clickPosition);
PicturePanel.this.removeMouseListener(this);
repaint();
}
}
private class ControlsListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addShapeBtn) {
if (shapesMode == LINES_ONLY) {
addShape(LINE);
} else if (shapesMode == BOUNDED_SHAPES) {
addShape(chooseBoundedShape(PicturePanel.this));
} else {
addShape(chooseAnyShape(PicturePanel.this));
}
} else if (e.getSource() == removeShapeBtn) {
removeShape();
}
}
}
private void addShape(int shapeType) {
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
startDrag = new question2.Point(e.getX(), e.getY());
endDrag = startDrag;
repaint();
}
public void mouseReleased(MouseEvent e) {
picture.add(tempShape);
removeMouseMotionListener(this);
for (java.awt.event.MouseListener m: PicturePanel.this.getMouseListeners()){
PicturePanel.this.removeMouseListener(m);
}
repaint();
}
});
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
PicturePanel.this.repaint();
endDrag = new question2.Point(e.getX(), e.getY());
switch (shapeType) {
case LINE:
tempShape =
new Line(startDrag.getX(), startDrag.getY(), endDrag.getX(),
endDrag.getY(), new Color(((int) (Math.random() * 255)), ((int) (Math.random() * 255)),
((int) (Math.random() * 255))));
break;
case OVAL:
tempShape =
new Oval(Math.min(startDrag.getX(), endDrag.getX()),
Math.min(startDrag.getY(), endDrag.getY()),
Math.abs(endDrag.getX() - startDrag.getX()),
Math.abs(endDrag.getY() - startDrag.getY()),
new Color(((int) (Math.random() * 255)), ((int) (Math.random() * 255)), ((int) (Math.random() * 255))), false);
break;
case RECTANGLE:
tempShape =
new Rectangle(Math.min(startDrag.getX(), endDrag.getX()),
Math.min(startDrag.getY(), endDrag.getY()),
Math.abs(endDrag.getX() - startDrag.getX()),
Math.abs(endDrag.getY() - startDrag.getY()),
new Color(((int) (Math.random() * 255)), ((int) (Math.random() * 255)), ((int) (Math.random() * 255))), false);
break;
}
repaint();
}
});
}
private void removeShape() {
System.out.println("click on shape to remove");
MouseListener ml = new MouseListener();
PicturePanel.this.addMouseListener(ml);
}
public Picture<Shape> getPicture() {
return picture;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
picture.show(g);
tempShape.draw(g);
}
public static int chooseShapesType(Component parent) {
int choice = JOptionPane.showOptionDialog(parent, "Choose shape type", null, 0, JOptionPane.QUESTION_MESSAGE
, null, new String[] {"Lines only", "Bounded shapes", "All shapes"}, null);
if (choice == 0)
return LINES_ONLY;
else if (choice == 1)
return BOUNDED_SHAPES;
else if (choice == 2)
return ALL_SHAPES;
return NONE;
}
private static int chooseBoundedShape(Component parent) {
int choice = JOptionPane.showOptionDialog(parent, "Choose shape type", null, 0, JOptionPane.QUESTION_MESSAGE
, null, new String[] {"Rectangle", "Oval"}, null);
if (choice == 0)
return RECTANGLE;
else if (choice == 1)
return OVAL;
return NONE;
}
private static int chooseAnyShape(Component parent) {
int choice = JOptionPane.showOptionDialog(parent, "Choose shape type", null, 0, JOptionPane.QUESTION_MESSAGE
, null, new String[] {"Rectangle", "Oval", "Line"}, null);
if (choice == 0)
return RECTANGLE;
else if (choice == 1)
return OVAL;
else if (choice == 2)
return LINE;
return NONE;
}
}
最佳答案
让我们不要谈谈您的解决方案是否是一种好的做法,而只是解决您的问题。
我注意到,您实现了2个侦听器类。 1)MouseListener
2)ControlsListener
。
监听器的删除仅在MouseListener
实现中完成,而不在ControlsListener
中实现。
在PicturePanel
中,尽管您使用ControlsListener
,但从不使用MouseListener
,但是它从未删除自身,如上所述。
关于java - removeMouseListener()将不起作用(Java),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30628448/