鼠标监听器获取点在跟踪上的位置

时间:2016-06-15 14:01:55

标签: java swing

我已经构建了一个GUI。它有一个图表面板,由一条轨迹组成。现在我需要设计一个鼠标监听器,当我点击跟踪时,应该在gui图表面板上显示一个点。为此,我的代码是这样的。它显示错误。如果澄清,将不胜感激。

package jchart2Dt;

import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Event;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import info.monitorenter.gui.chart.traces.Trace2DSimple;

@SuppressWarnings("unused")
public class MouseEvents extends Jchart2Dt_GUI{
    public static void listener () {


    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        public void eventDispatched(AWTEvent event) {
            if(event instanceof MouseEvent){
                MouseEvent evt = (MouseEvent)event;
                if(evt.getID() == MouseEvent.MOUSE_CLICKED){

                    float x=(float)getX(trace_0);
                    float y=(float)getY(trace_0);
                    System.out.println(x+","+y);
                }
            }
        }

        private float getY(Trace2DSimple trace_0) {
            // TODO Auto-generated method stub
            return NTRACES;
            //return 0;
        }
    }, AWTEvent.MOUSE_EVENT_MASK);

}

    protected static float getX(Trace2DSimple trace_0) {
        // TODO Auto-generated method stub
        return NTRACES;
        //return 0;
    }
}

0 个答案:

没有答案