Java Swing:将鼠标指针悬停在矩形上时行为不当 [英] Java Swing: Mouse cursor misbehaves when held over a rectangle

查看:132
本文介绍了Java Swing:将鼠标指针悬停在矩形上时行为不当的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过contain(p)方法更改光标在矩形数组列表上的移动.问题是

I need to change the cursor while it moves over an array-list of rectangles by the contain(p) method.The problem is

  1. 我的第一个使用迭代器迭代矩形的算法无法正常工作.光标仅在将鼠标悬停在第一个矩形上时才会更改,在其他矩形中,它既不会通过显示光标变化来响应,也不会通过控制台光标悬停在它们上方?!
  2. 我的第二个解决方案也拒绝正常工作.我使用for循环遍历矩形,尽管矩形通过控制台指示鼠标悬停在矩形上方,但光标拒绝更改(最后一个矩形除外)
  3. 我在此SSCCE中使用了一个JPanel,只是因为它重现了使用JTextPane遇到的问题...假设我的编码方法有问题.

我在想可能我需要一个线程来改善响应和性能,但不确定该方法.在此先感谢大家.

I am thinking may be I may need to a thread to improve response and performance but not sure about the approach.Thanks in advance people.

public class UnstableCursor extends JPanel{

    Rectangle2D rec;
    ArrayList<Rectangle2D> recList = new ArrayList<>();

    public UnstableCursor(){

    }

    public static void main(String[] args) {
        UnstableCursor uc = new UnstableCursor();
        JFrame frame = new JFrame();

        Mover mv = new Mover(uc);
        uc.addMouseListener(mv);
        uc.addMouseMotionListener(mv);
        JScrollPane jx = new JScrollPane(uc);

        frame.getContentPane().add(jx);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500,500);
        frame.setVisible(true);
    }


    @Override
    public void paintComponent(Graphics g) {

        super.paintComponents(g); 

        Graphics2D g2d = (Graphics2D)g;
        int x = 5;
        for(int i = 0;i < 4;i++){
            g2d.setColor(Color.red);
            rec = new Rectangle2D.Double(20,x,100,5);
            g2d.draw(rec);

            recList.add(rec);

            x += 50;
        }
        System.out.println("RecList is: " +recList.size());
    }  

}

class Mover extends MouseInputAdapter{
    UnstableCursor uc;
    Rectangle2D rec;
    ArrayList<Rectangle2D> reList;

    public Mover(UnstableCursor ucc){
        uc = ucc;
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        super.mouseDragged(e); 
        Point p = e.getPoint();
        System.out.println("xxxx");
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        Point p = e.getPoint();
        reList = uc.recList;
        //System.out.println("List is: "+reList.size());
        Iterator <Rectangle2D> recs = reList.iterator();

        //--------------------- First Algorithm ----------------------//
        if(recs.hasNext()){
            rec = recs.next();
            if(rec.contains(p)){
                System.out.println("inside the rectangle....");
                uc.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            }               
            else{
            uc.setCursor(Cursor.getDefaultCursor());
            }

        }


        //--------------------- Second Algorithm  ---------------------//
        int r = 0;
        for(int i = 0;i<(reList.size());i++){
            rec = reList.get(r);
            //System.out.println("Rect No: "+r+"X: "+rec.getX()+"Y: "+rec.getY());
            r++;
            if(rec.contains(p)){
                System.out.println("inside the rectangle....");                    
                uc.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            }
            else{
            uc.setCursor(Cursor.getDefaultCursor());
            }
        }

    }

}

推荐答案

除了最后一个矩形,光标拒绝更改.

the cursor refuses to change with the exception of the last rectangle.

您的基本搜索算法是错误的.一旦找到包含该点的矩形,就应该设置光标并跳出循环,否则,您检查的下一个矩形将不匹配,并且光标将再次重置.

Your basic search algorithm is wrong. Once you find a rectangle that contains the point you should set the cursor and break out of the loop, otherwise the next rectangle you check will not be a match and the cursor will be reset again.

还...

for(int i = 0;i < 4;i++){
     g2d.setColor(Color.red);
     rec = new Rectangle2D.Double(20,x,100,5);
     g2d.draw(rec);

     recList.add(rec);

     x += 50;
}

...绘画方法仅用于绘画.

... A painting method is for painting only.

您不应创建矩形并将其添加到数组中,因为当Swing确定需要重新绘制面板时,将连续调用paintComponent()方法.

You should NOT be creating rectangles and adding them to the array, since the paintComponent() method is continually called when Swing determines the panel needs to be repainted.

应该在类的构造函数中将矩形添加到列表中,以便每个矩形仅添加一次.

The rectangles should be added to the List in the constructor of your class so each rectangle is only added once.

这篇关于Java Swing:将鼠标指针悬停在矩形上时行为不当的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆