鼠标坐标不断叠加-Java
我试图在面板中显示鼠标坐标,但每次移动光标时,都会在上一个面板上显示消息和新坐标。我正在使用带JPanel的MouseMotionListener。我想不出这个问题鼠标坐标不断叠加-Java,java,jpanel,Java,Jpanel,我试图在面板中显示鼠标坐标,但每次移动光标时,都会在上一个面板上显示消息和新坐标。我正在使用带JPanel的MouseMotionListener。我想不出这个问题 您正在创建Main两次 public class Main extends JPanel implements MouseMotionListener { public JLabel label; public static void main(String[] args) { Main m = new Main();/
您正在创建
Main
两次
public class Main extends JPanel implements MouseMotionListener {
public JLabel label;
public static void main(String[] args) {
Main m = new Main();// create an object and reference it
JFrame frame = new JFrame();
frame.setTitle("MouseCoordinates");
frame.setSize(400, 400);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
Container contentPane = frame.getContentPane();
contentPane.add(m);
frame.setVisible(true);
}
//...
您的问题是创建了两次
Main
对象(这是一个jpanel),然后写入出现了两次。如果您给Main
对象一个引用,那么您的问题应该得到解决。您理解我的答案了吗?不完全理解。您创建了两次jpanel,并且书写出现了两次。测试我的答案,看它是否有效。。谢谢。没问题,很乐意帮忙
public class Main extends JPanel implements MouseMotionListener {
public JLabel label;
public static void main(String[] args) {
Main m = new Main();// create an object and reference it
JFrame frame = new JFrame();
frame.setTitle("MouseCoordinates");
frame.setSize(400, 400);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
Container contentPane = frame.getContentPane();
contentPane.add(m);
frame.setVisible(true);
}
//...