지도 편집기를 만들고 있는데, 클릭하면지도에 데이터 요소를 추가하는 데 사용됩니다.클릭 할 때마다 java MouseListener가 동일한 값 x 및 y 값을 반환하는 이유는 무엇입니까?
public void mouseClicked(MouseEvent e) {
int x = getX();
int y = getY();
System.out.println("clicked at (" + x + ", " + y + ")");
}
그러나, 나는 창에 클릭에 상관없이, 그것은 동일한 값을 반환
public MapEditor() throws HeadlessException, FileNotFoundException, XMLStreamException {
super("MapEditor");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set JFrame properties.
this.setTitle("Map Editor");
this.setSize(PREFERRED_WIDTH, PREFERRED_HEIGHT);
this.setBackground(Color.gray);
this.setJMenuBar(makeMenuBar());
JPanel mainPanel = new JPanel(new BorderLayout());
Icon image = new ImageIcon("map.jpg");
JLabel label = new JLabel(image);
scrollPane = new JScrollPane();
scrollPane.getViewport().add(label);
scrollPane.addMouseListener(this);
mainPanel.add(scrollPane, BorderLayout.CENTER);
this.getContentPane().add(mainPanel);
this.getContentPane().add(makeStatusBar(), BorderLayout.SOUTH);
setVisible(true);
}
는 또한 마우스를 클릭했을 때의 다음과 같은 이벤트가 있습니다. 화면 전체에서 다른 창으로 전체 창을 이동하면 다른 값을 반환하는 것으로 나타났습니다. 창 왼쪽 상단에 해당하는 것으로 나타납니다. 다른 구성 요소에 MouseListener를 추가하려고 시도했지만 각각의 결과가 동일합니다. 도움을 주시면 감사하겠습니다.
getX와 getY() 메소드 안에 무엇을 발생한 위치 : 그것은 할 필요가? – r0ast3d