Java 下面的程序为什么单击图片的不同位置,得到的坐标都是(0,0)?
importjava.awt.Color;importjava.awt.event.MouseAdapter;importjava.awt.event.MouseEven...
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.util.*;
import java.awt.geom.*;
import java.awt.Point;
public class DiTu
{
static int x,y;
public static final int DEFAULT_WIDTH = 200;
public static final int DEFAULT_HEIGHT = 200;
public static void main(String[] args)throws Exception
{
JFrame f=new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(null);
f.setVisible(true);
f.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
Icon icon=new ImageIcon("C:\\1.jpg");
JLabel l=new JLabel(icon);
l.setSize(icon.getIconWidth(),icon.getIconHeight());
l.setBorder(BorderFactory.createLineBorder(Color.red));
f.getContentPane().add(l);
l.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
x=e.getX();
y=e.getY();
}
});
l.addMouseMotionListener(new MouseMotionListener()
{
public void mouseDragged(MouseEvent e)
{
JLabel l=(JLabel)e.getSource();
l.setLocation(l.getX()+e.getX()-x,l.getY()+e.getY()-y);
}
public void mouseMoved(MouseEvent e){}
});
l.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
Point p=new Point();
System.out.println(p);
}
});
}
} 展开
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.util.*;
import java.awt.geom.*;
import java.awt.Point;
public class DiTu
{
static int x,y;
public static final int DEFAULT_WIDTH = 200;
public static final int DEFAULT_HEIGHT = 200;
public static void main(String[] args)throws Exception
{
JFrame f=new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(null);
f.setVisible(true);
f.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
Icon icon=new ImageIcon("C:\\1.jpg");
JLabel l=new JLabel(icon);
l.setSize(icon.getIconWidth(),icon.getIconHeight());
l.setBorder(BorderFactory.createLineBorder(Color.red));
f.getContentPane().add(l);
l.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
x=e.getX();
y=e.getY();
}
});
l.addMouseMotionListener(new MouseMotionListener()
{
public void mouseDragged(MouseEvent e)
{
JLabel l=(JLabel)e.getSource();
l.setLocation(l.getX()+e.getX()-x,l.getY()+e.getY()-y);
}
public void mouseMoved(MouseEvent e){}
});
l.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
Point p=new Point();
System.out.println(p);
}
});
}
} 展开
1个回答
2015-08-08
展开全部
Point p=new Point();
这个构造方法new 出来的就是0,0的 你看下源码就懂的
public Point() {
this(0, 0);
}
这个构造方法new 出来的就是0,0的 你看下源码就懂的
public Point() {
this(0, 0);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询