为什么只显示鼠标的坐标?

时间:2012-07-27 05:44:03

标签: java swing mouseevent mouseclick-event mouse-coordinates

当Thread线程运行时(点击记录后)它只显示线程启动时鼠标所处的位置?如何让它不断更新,显示鼠标在哪里,即使我在框架周围移动它?

@Override public void actionPerformed(ActionEvent e)
{       
    thread = new Thread(this);

    if(e.getSource() == record)
    {
        thread.start();
        System.out.println("record");
    }

    if(e.getSource() == stopRecording)
    {
        setVisible(false);
        System.out.println("stop recording");
    }

}

@Override public void run()
{       
    setTitle("979");

    setSize(screen.width, screen.height);
    addMouseListener(this);
    setLocationRelativeTo(null);
    setLayout(transFlo);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
    add(stopRecording);     
    setOpacity(0.50f);      
    setVisible(true);

    while(true)
    {
        repaint();
    }

}

@Override public void paint(Graphics g)
{
    g.drawString(mousePOS + x + space + y, 250, 250);
}

2 个答案:

答案 0 :(得分:0)

好的,重申一下;请阅读The Event Dispatching Thread

然后阅读Concurrency in Swing

最后阅读How to Write a Mouse Listener

<强> ADDINTIONAL

如果要监控全局鼠标事件(通过系统的所有鼠标事件),那么您需要查看Toolkit.addAWTEventListener

这将允许您监视所有鼠标事件,而无需将鼠标侦听器附加到所有组件

简单的鼠标示例

以下是监视鼠标的面板的简单示例:P

public class CrayPanel extends javax.swing.JPanel implements MouseMotionListener, MouseListener {

    private List<Point> lstPoints;

    /**
     * Creates new form CrayPanel
     */
    public CrayPanel() {

        lstPoints = new ArrayList<Point>(25);

        addMouseListener(this);
        addMouseMotionListener(this);

    }

    @Override
    protected void paintComponent(Graphics g) {

        super.paintComponent(g);

        if (lstPoints.size() > 1) {

            Graphics2D g2d = (Graphics2D) g;

            g2d.setColor(Color.RED);
            Point startPoint = lstPoints.get(0);
            for (int index = 1; index < lstPoints.size(); index++) {

                Point toPoint = lstPoints.get(index);
                g2d.drawLine(startPoint.x, startPoint.y, toPoint.x, toPoint.y);

                startPoint = toPoint;

            }

        }

    }

    @Override
    public void mouseDragged(MouseEvent e) {
    }

    @Override
    public void mouseMoved(MouseEvent e) {

        lstPoints.add(e.getPoint());
        repaint();

    }

    @Override
    public void mouseClicked(MouseEvent e) {

        lstPoints.clear();
        lstPoints.add(e.getPoint());
        repaint();

    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {

        lstPoints.add(e.getPoint());
        repaint();

    }

    @Override
    public void mouseExited(MouseEvent e) {

        lstPoints.add(e.getPoint());
        repaint();

    }
}

答案 1 :(得分:0)

我把另一个例子放在一起。这本质上是一个鼠标监视器,它表明,如果正确完成,您不需要Thread

public class MouseFrame extends javax.swing.JFrame implements AWTEventListener, ActionListener {

    private boolean monitor = false;
    private Point mousePoint;

    /**
     * Creates new form MouseFrame
     */
    public MouseFrame() {

        setLayout(new GridBagLayout());

        JButton btnToggle = new JButton("Start");
        add(btnToggler);

        btnToggle.addActionListener(this);

        setSize(400, 400);

    }

    public void actionPerformed(java.awt.event.ActionEvent evt) {

        monitor = !monitor;

        if (monitor) {

            btnTrigger.setText("Stop");
            Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.MOUSE_MOTION_EVENT_MASK);

        } else {

            btnTrigger.setText("Start");
        Toolkit.getDefaultToolkit().removeAWTEventListener(this);

    }

    }

    @Override
    public void paint(Graphics grphcs) {

        super.paint(grphcs);

        Graphics2D g2d = (Graphics2D) grphcs;

        if (monitor) {

            g2d.setColor(Color.RED);
            FontMetrics fm = g2d.getFontMetrics();
        g2d.drawString(mousePoint.x + "x" + mousePoint.y, 25, fm.getHeight() + 25);

    }

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MouseFrame().setVisible(true);
            }
        });
    }

    @Override
    public void eventDispatched(AWTEvent evt) {

        if (evt instanceof MouseEvent) {

            MouseEvent me = (MouseEvent) evt;
            mousePoint = SwingUtilities.convertPoint(me.getComponent(), me.getPoint(), this);

            repaint();

        }
    }
}