为什么我的MouseListener不按顺序递增变量? [英] Why does my MouseListener not increment a variable in sequential order?
问题描述
在我的类 X
中,我扩展了 JPanel
并实现了 MouseListener
, MouseMotionListener
int numPoints = 0;
点控制点[] =新点[10];
public void MousePressed(MouseEvent arg0){
if(numPoints< 5)
controlPoints [numPoints] = arg0.getPoint();
numPoints ++;
}
* 当我System.out.println(numPoints); *
在3次CLICKS后输出;
- 0
- 0
- 2 >
- 2
- 3
- 3 $
- 5 b $ b
为什么不通过1,2,3增加??? b / b>
输出
$ b- 1
- 2
***我会在必要时提供更多资讯。
感谢您的回复。这里是完整编辑的javacode。这个问题对我来说依然没有解决。我期待着新的想法和更正。提前致谢
package work2014.java.all;
java.awt。*;
import java.awt.event。*
import java.util.ArrayList;
import javax.swing。*;
class jdrawPanel扩展JPanel实现MouseListener,MouseMotionListener,ActionListener
{
图形g;
boolean paint = false,edit = false;
点d;
point curvePoints [] = new Point [100];
Point [] controlPoints = new Point [99];
int n = 99,i = 3;
private int numPoints = 0;
public void paintComponent(Graphics g){
addMouseListener(this);
addMouseMotionListener(this);
super.paintComponent(g);
if(numPoints> = 0& numPoints< controlPoints.length)
Dot(numPoints,controlPoints,g);
if(paint == true&& numPoints> = 3){
drawBezier(n,i,controlPoints,g);
}
this.requestFocus();
$ b public void Dot(int numPoints,Point controlPoints [],Graphics g){
draw red dot
}
$ b $ public void drawBezier(int n,int i,Point controlPoints [],Graphics g)
{
draw spline
} / / End drawBezier
$ b @Override
public void mouseMoved(MouseEvent arg0){
Point p = arg0.getPoint();
if(equalPoint(p)== p)
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
else
setCursor(Cursor.getDefaultCursor());
$ b @Override
public void mousePressed(MouseEvent arg0){
paint = true;
Point d = arg0.getPoint();
if(numPoints< = 10){
controlPoints [numPoints] = d;
numPoints ++;
System.out.println(numPoints);
}
repaint();
}
}
类Oving5_Frame扩展JFrame
{public Oving5_Frame()
{setTitle(Hello draW v2);
setSize(600,600);
$ b $ addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);
}
});
容器contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(新的jdrawPanel,BorderLayout.CENTER);
contentPane.add(class_menu(),BorderLayout.NORTH);
public class Oving5_launch
{
public static void main(String [] args)
{
JFrame frame = new Oving5_Frame();
frame.show();
}
}
解决方案您应该检查事件对象的属性以查看您正在响应的事件类型。你很可能需要
if(arg0.getID()== MouseEvent.MOUSE_CLICKED)
在那里。In my Class
X
I have extendedJPanel
and implementedMouseListener
,MouseMotionListener
int numPoints = 0; Point controlPoints[] = new Point[10]; public void MousePressed (MouseEvent arg0) { if (numPoints < 5) controlPoints[numPoints] = arg0.getPoint(); numPoints++; }
*When I System.out.println(numPoints); *
output after 3 CLICKS;
- 0
- 0
- 2
- 2
- 3
- 3
- 5
WHY Does it not INCREMENT by 1, 2 ,3 ???
output (for numPoints) (first mouseclick)
- 1
- 2
***I will provide more information if necessary.
Thanks for the response. Here is the full edited javacode. The problem is still unsolved for me. I look forward to new ideas and corrections. Thanks in advance
package work2014.java.all; java.awt.*; import java.awt.event.* import java.util.ArrayList; import javax.swing.*; class jdrawPanel extends JPanel implements MouseListener, MouseMotionListener, ActionListener { Graphics g; boolean paint = false, edit = false; Point d; Point curvePoints[]= new Point[100]; Point[] controlPoints = new Point[99]; int n = 99, i = 3; private int numPoints = 0; public void paintComponent(Graphics g){ addMouseListener(this); addMouseMotionListener(this); super.paintComponent(g); if(numPoints >= 0 && numPoints < controlPoints.length) Dot(numPoints,controlPoints,g); if(paint == true && numPoints >= 3){ drawBezier(n, i, controlPoints, g); } this.requestFocus(); } public void Dot(int numPoints, Point controlPoints[], Graphics g){ draw red dot } public void drawBezier(int n, int i, Point controlPoints[], Graphics g) { draw spline } // End drawBezier @Override public void mouseMoved(MouseEvent arg0) { Point p = arg0.getPoint(); if (equalPoint(p) == p) setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); else setCursor(Cursor.getDefaultCursor()); } @Override public void mousePressed(MouseEvent arg0) { paint = true; Point d = arg0.getPoint(); if(numPoints <= 10) { controlPoints[numPoints] = d; numPoints++; System.out.println(numPoints); } repaint(); } } class Oving5_Frame extends JFrame { public Oving5_Frame() { setTitle("Hello draW v2"); setSize(600, 600); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); Container contentPane = getContentPane(); contentPane.setLayout(new BorderLayout()); contentPane.add(new jdrawPanel, BorderLayout.CENTER); contentPane.add(class_menu(), BorderLayout.NORTH); } public class Oving5_launch { public static void main(String[] args) { JFrame frame = new Oving5_Frame(); frame.show(); } }
解决方案You should probably be checking the properties of the event object to see what type of event you are responding to. You most likely need
if (arg0.getID() == MouseEvent.MOUSE_CLICKED)
in there somewhere.这篇关于为什么我的MouseListener不按顺序递增变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!