JLabel跟不上鼠标

时间:2015-05-25 14:29:34

标签: java drag-and-drop mouseevent

我有一个小程序,可以创建一些可以被视为操纵杆的东西。一切正常,但有一个小故障,我无法解决如何解决。如果鼠标移动太快,我拖动的JLabel会粘住,因为鼠标移动到绘制框之外。我可以增加Jlabel的大小,但是“O”从鼠标偏移太多。 (我甚至会减小尺寸,但是通过这种实现,最大鼠标速度太低)。 任何想法如何解决这一问题?

这是整个代码,顺便说一下,当鼠标移动得太快时,任何人都可以继续编译,看看究竟是什么问题。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainClass implements ActionListener, MouseListener, MouseMotionListener
{
int labelSize = 20;
int screenOffsetX = 58;
int screenOffsetY = 130;
JFrame frame;
JLabel xAxis;
JLabel move;
JLabel drag;
JLabel yAxis;
JLabel xAxisDrag;
JLabel yAxisDrag;
JLabel radio;
JLayeredPane panel;
Robot rob;
public static void main(String[] args)
{
    new MainClass();
}
public MainClass()
{   
    frame = new JFrame("app");
    frame.setLayout(null);
    frame.setBounds(20, 20, 400, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    move = new JLabel("Movement");
    move.setBounds(150, 0, 100, 15);
    frame.add(move);

    xAxis = new JLabel("X");
    xAxis.setBounds(100, 20, 50, 15);
    frame.add(xAxis);

    yAxis = new JLabel("Y");
    yAxis.setBounds(200, 20, 50, 15);
    frame.add(yAxis);

    drag = new JLabel("Dragging");
    drag.setBounds(150, 40, 100, 15);
    frame.add(drag);

    xAxisDrag = new JLabel("X");
    xAxisDrag.setBounds(100, 60, 50, 15);
    frame.add(xAxisDrag);

    yAxisDrag = new JLabel("Y");
    yAxisDrag.setBounds(200, 60, 50, 15);
    frame.add(yAxisDrag);

    radio = new JLabel("O");
    radio.setBounds(0, 0, labelSize, labelSize);
    radio.setOpaque(false);
    radio.setEnabled(false);
    panel = new JLayeredPane();
    panel.setBounds(50, 100, 257, 257);
    panel.setLayout(null);
    panel.setBackground(new Color((float)1.0,(float)1.0,(float)1.0));
    panel.setOpaque(true);
    panel.add(radio);
    radio.setLocation(128, 128);
    frame.add(panel);
    panel.addMouseMotionListener(this);
    panel.addMouseListener(this);
    frame.revalidate();
    frame.repaint();
}
@Override
public void actionPerformed(ActionEvent e) {}
@Override
public void mouseDragged(MouseEvent e)
{
    if(!(e.getPoint().x<0 || e.getPoint().y<0 || e.getPoint().x>257 || e.getPoint().y>257))
    {   
        xAxis.setText("X: "+((e.getPoint().x/4)-32));
        yAxis.setText("Y: "+((e.getPoint().y/4)-32));
    }
    if(e.getPoint().x<0 || e.getPoint().y<0 || e.getPoint().x>257 || e.getPoint().y>257)
    {   //Neļauj iziet ārpus paneļa;
        try
        {
            rob = new Robot();
            if(e.getPoint().x<0)
                rob.mouseMove((screenOffsetX+frame.getX()), (e.getPoint().y+screenOffsetY+frame.getY()));
            if(e.getPoint().y<0)
                rob.mouseMove((e.getPoint().x+screenOffsetX+frame.getX()), (screenOffsetY+frame.getY()));
            if(e.getPoint().x>257)
                rob.mouseMove((257+screenOffsetX+frame.getX()), (e.getPoint().y+screenOffsetY+frame.getY()));
            if(e.getPoint().y>257)
                rob.mouseMove((e.getPoint().x+screenOffsetX+frame.getX()), (257+screenOffsetY+frame.getY()));
        }
        catch (AWTException e1)
        {
            e1.printStackTrace();
        }
    }
    if(     (e.getPoint().x>=radio.getX() && e.getPoint().x<=radio.getX()+labelSize) && 
            (e.getPoint().y>=radio.getY() && e.getPoint().y<=radio.getY()+labelSize)
            )
    {   //Ja kursors ir uz JLabel, tad pārvieto;
        xAxisDrag.setText("X: "+((e.getPoint().x/4)-32));
        yAxisDrag.setText("Y: "+((e.getPoint().y/4)-32));
        radio.setBounds(e.getPoint().x-(labelSize/2), e.getPoint().y-(labelSize/2), labelSize, labelSize);
    }
}
@Override
public void mouseMoved(MouseEvent e)
{
    xAxis.setText("X: "+((e.getPoint().x/4)-32));
    yAxis.setText("Y: "+((e.getPoint().y/4)-32));
}
@Override
public void mouseClicked(MouseEvent arg0) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e)
{
    radio.setLocation(124, 124);
    xAxisDrag.setText("X: "+0);
    yAxisDrag.setText("Y: "+0);
}

}

1 个答案:

答案 0 :(得分:0)

替换

     if(     (e.getPoint().x>=radio.getX() && e.getPoint().x<=radio.getX()+labelSize) && 
        (e.getPoint().y>=radio.getY() && e.getPoint().y<=radio.getY()+labelSize)
        )
{   //Ja kursors ir uz JLabel, tad pārvieto;
    xAxisDrag.setText("X: "+((e.getPoint().x/4)-32));
    yAxisDrag.setText("Y: "+((e.getPoint().y/4)-32));
    radio.setBounds(e.getPoint().x-(labelSize/2), e.getPoint().y-(labelSize/2), labelSize, labelSize);
}

由:

  if(      (e.getPoint().x>=radio.getX() && e.getPoint().x<=radio.getX()+labelSize) && 
        (e.getPoint().y>=radio.getY() && e.getPoint().y<=radio.getY()+labelSize)
        )
{
    //Has the mouse been clicked inside the radio before grabbing started ?
    if(grab==0)  //if it is the first iteration of the function mousedragged
        grab=2;  //proceed to "grab"
}

if(grab==2 &&   (e.getPoint().x>=0 && e.getPoint().x<panel.getWidth()) && 
        (e.getPoint().y>=0 && e.getPoint().y<panel.getHeight())
        )
{   //Ja kursors ir uz JLabel, tad pārvieto;
    System.out.println(radio.getX()+" "+radio.getY());
    xAxisDrag.setText("X: "+((e.getPoint().x/4)-32));
    yAxisDrag.setText("Y: "+((e.getPoint().y/4)-32));
    radio.setBounds(e.getPoint().x-(labelSize/2), e.getPoint().y-(labelSize/2), labelSize, labelSize);

}
if(grab!=2)  // if grabbing is not started
   grab=1; // first iteration is over

将MouseReleased函数更新为:

        @Override
    public void mouseReleased(MouseEvent e)
    {
        //this a new line
        //grabbing is over
        grab=0;  // go back to idle state
        radio.setLocation(124, 124);
        xAxisDrag.setText("X: "+0);
        yAxisDrag.setText("Y: "+0);
    }

最后,在您的类MainClass中添加一个名为grab的新字段:

  private int grab=0;  //idle state

抓斗的意思:
1. grab = 0 =&gt;这是第一次执行MouseDragged函数并且&#34;抓取&#34;尚未开始。事实上,当你拖动鼠标时,这个函数会像无限循环一样迭代,直到你释放鼠标。因此,了解它何时开始非常重要 2. grab = 1 =&gt;迭代已经开始,但没有&#34;抓住&#34;
3. grab = 2 =&gt; &#34;敛&#34;已经开始。你可以在做作之前注意到条件抓取== 0。实际上,我试图避免将鼠标从其他地方拖出并经过&#34; radio&#34;然后收听电台&#34;抓住&#34;。这就是我检查它是第一次迭代的原因。在这种情况下,我们有抓取= 1 =&gt;收音机没有抓住