簡體   English   中英

當鼠標移至2個面板的交叉區域時,更改光標

[英]Change the cursor when the mouse's over the intersection area of 2 panels

我在JFrame上有2個Jpanel(leftpanel和rightpanel)。 當鼠標移至2個面板的交叉區域時,如何更改光標?

在此處輸入圖片說明

到目前為止,我嘗試過:

 ...
 public void mouseMoved(MouseEvent e) {
           if (leftpanel.contains(e.getPoint()) && rightpanel.contains(e.getPoint())){

                frame.setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));

          }
           else{ frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        };

但是沒有用..

您的問題是如何檢測兩個面板的交點並更改光標。 這是一個如何做的例子


    public static void overlapTest() {
        final JPanel p1 = new JPanel();
        final JPanel p2 = new JPanel();
        p1.setBackground(Color.RED);
        p2.setBackground(Color.BLUE);
        final JPanel container = new JPanel();
        container.setLayout(null);
        container.add(p1);
        container.add(p2);
        p1.setBounds(0,0,120,100);
        p2.setBounds(80,0,120,100);
        Dimension size = new Dimension(200,100);
        container.setPreferredSize(size);
        container.addMouseMotionListener(new MouseMotionListener() {

            @Override
            public void mouseDragged(MouseEvent arg0) {
            }

            @Override
            public void mouseMoved(MouseEvent e) {
                Point pt1 = e.getPoint();
                pt1.translate(-p1.getX(), -p1.getY());
                Point pt2 = e.getPoint(); 
                pt2.translate(-p2.getX(), -p2.getY());
                if (p1.contains(pt1) && p2.contains(pt2)) {
                        System.out.println("both contain: " + e.getPoint());
                        container.setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
                  }
                  else{ 
                      container.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                  };
            }

        });
    }

正如Jeffey的建議,我應該使用JSlitPane進行處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM