2012-03-11 2 views
2

В моем коде должны отображаться координаты мыши, относящиеся к окну enitre, но метод, который я использовал, будет давать координаты только для каждого отдельного компонента. как получить координаты для всего окна и всех компонентов?координаты мыши по всему окну

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

import java.util.*; 
import java.text.*; 

public class Clock extends JFrame implements ActionListener, Runnable, MouseMotionListener { 
    public static final int w = 500; 
    public static final int l = 200; 
    public JLabel label; 
    String msg= null; 
    public int color=0; 
     int xpos; 
     int ypos; 
     JTextField xf = new JTextField("b"); 
     JTextField yf = new JTextField("d"); 
     JTextField butf = new JTextField("f"); 
    Date date = new Date(); 

     String DATE_FORMAT = "hh:mm:ss"; 

     SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT); 

     public void mouseMoved(MouseEvent e) 
     { 

      xpos = e.getX(); 
      ypos = e.getY(); 
      xf.setText(""+xpos); 
      yf.setText(""+ypos); 

     } 
     public void mouseDragged(MouseEvent me) 
     { 
       xpos = me.getX(); 
       ypos = me.getY(); 
     } 
    public Clock() { 
     super("CS 302 Lab 7"); 
     addMouseMotionListener(this); 
     JPanel main = new JPanel(); 
     JPanel top = new JPanel(); 
     main.setLayout(new GridLayout(1,0)); 
     top.setLayout(new GridLayout(1,0)); 
     setLayout(new BorderLayout()); 
     setSize(w, l); 

     JLabel x = new JLabel("X"); 
     x.addMouseMotionListener(this); 
     JLabel y = new JLabel("Y"); 
     y.addMouseMotionListener(this); 
     JLabel but = new JLabel("Button"); 
     but.addMouseMotionListener(this); 
     xf.addMouseMotionListener(this); 
     yf.addMouseMotionListener(this); 
     butf.addMouseMotionListener(this); 

     top.add(x); 
     top.add(xf); 
     top.add(y); 
     top.add(yf); 
     top.add(but); 
     top.add(butf); 

     msg = (sdf.format(date)); 
     label = new JLabel(msg, SwingConstants.CENTER); 
     label.setFont(new Font("Serif", Font.PLAIN, 40)); 

     add(BorderLayout.CENTER, label); 

     JButton red = new JButton("Red"); 
     main.add(red); 
     red.addActionListener(this); 


     JButton green = new JButton("Green"); 
     main.add(green); 
     green.addActionListener(this); 

     JButton blue = new JButton("Blue"); 
     main.add(blue); 
     blue.addActionListener(this); 

     JButton orange = new JButton("Orange"); 
     main.add(orange); 
     orange.addActionListener(this); 

     red.addMouseMotionListener(this); 
     blue.addMouseMotionListener(this); 
     green.addMouseMotionListener(this); 
     orange.addMouseMotionListener(this); 

     SwingUtilities.convertPoint(red, x, y, destination) 
     add(BorderLayout.SOUTH, main); 
     add(BorderLayout.NORTH, top); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setVisible(true); 
    } 

    public static void main(String[] args) { 
    // Clock clock = new Clock(); 
     Runnable rn = new Clock(); 
     Thread th = new Thread(rn); 
     th.start(); 

    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     if(e.getActionCommand().equals("Red")){ 
      Date date = new Date(); 
      msg = (sdf.format(date)); 
      label.setText(msg); 
      label.setForeground(Color.RED); 
      color=1; 
     } 
     else if (e.getActionCommand().equals("Blue")){ 
      Date date = new Date(); 
      msg = (sdf.format(date)); 
      label.setText(msg); 
      label.setForeground(Color.BLUE); 
      color=2; 
     } 
     else if (e.getActionCommand().equals("Green")){ 
      Date date = new Date(); 
      msg = (sdf.format(date)); 
      label.setText(msg); 
      label.setForeground(Color.GREEN); 
      color=3; 
     } 
     else if (e.getActionCommand().equals("Orange")){ 
      Date date = new Date(); 
      msg = (sdf.format(date)); 
      label.setText(msg); 
      label.setForeground(Color.ORANGE); 
      color=4; 
     } 
    } 

    @Override 
    public void run() { 
     while (true){ 
      Date date = new Date(); 
     msg = (sdf.format(date)); 
     label.setText(msg); 
     if (color ==1) 
      label.setForeground(Color.RED); 
     else if (color ==2) 
      label.setForeground(Color.BLUE); 
     else if (color ==3) 
      label.setForeground(Color.GREEN); 
     else if (color ==4) 
      label.setForeground(Color.ORANGE); 
     try { 
      Thread.sleep(1000); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     } 
    } 

} 

ответ

2

Вы можете использовать SwingUtilities.convertPoint. Попробуйте это в методе mouseMoved():

Точка р = SwingUtilities.convertPoint (e.getComponent(), e.getPoint(), getContentPane());

Обновление компонента назначения по мере необходимости, я использовал getContentPane() для преобразования в координаты contentPane фрейма.

+0

+1 для электронной почты Ой быстрее ответить – mKorbel

2

Я надеюсь, что этот код отрезал возврат координату в JFrame, есть разочарование получить JFrame, так как удлиняются

public void mouseMoved(MouseEvent e) { 
     Container container = this.getContentPane(); 
     Point p = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), container); 
     System.out.println(p); 
     System.out.println(p.x); 
     System.out.println(p.y); 
     /*public static Point convertPoint(Component source, int x, int y, Component destination) 
     Convert the point (x,y) in source coordinate system to destination coordinate system. 
     If source is null, (x,y) is assumed to be in destination's root component coordinate 
     system. If destination is null, (x,y) will be converted to source's root component 
     coordinate system. If both source and destination are null, return (x,y) without any conversion.*/ 
     xpos = e.getX(); 
     ypos = e.getY(); 
     xf.setText("" + xpos); 
     yf.setText("" + ypos); 
    } 
+0

+1 того же идеи :) – tenorsax