- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在本应是简单的任务上遇到了问题。下面的类代表一个带有图片的 JPanel。每次我通过拖动和释放绘制形状后,我希望鼠标对框架/面板/组件没有响应。我试图通过以各种可能的方式删除鼠标监听器来做到这一点,正如您在方法 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/
removeMouseListener 不起作用:如果我单击旧的 JLabel,它仍然会添加到变量分数中。每次经过一段时间后,旧对象都应该被删除,而新对象应该是唯一拥有它的对象。 Timer picT
我在本应是简单的任务上遇到了问题。下面的类代表一个带有图片的 JPanel。每次我通过拖动和释放绘制形状后,我希望鼠标对框架/面板/组件没有响应。我试图通过以各种可能的方式删除鼠标监听器来做到这一点,
我是一名优秀的程序员,十分优秀!