当我拖动Jpanel并调整其大小时,如何更改鼠标光标

时间:2015-07-26 04:43:56

标签: java

我遇到了问题但无法解决 我想在拖动JPanel并调整其大小时更改鼠标光标,但是当我按下JPanel并将其拖动时,鼠标光标将恢复为默认光标。
这是我的代码:

public boolean drag = false;
public Point dragLocation = new Point(); 

private JLabel test = new JLabel("Release");
private JLabel eGetPoint = new JLabel();
private JLabel dragLocationPoint = new JLabel();
public JLabel showSize = new JLabel();
private Cursor e_w_Cursor = new Cursor(Cursor.W_RESIZE_CURSOR);
private Cursor d_Cursor = new Cursor(Cursor.DEFAULT_CURSOR);

public drawPanel(){
setBounds(0,0,500,500);
setBackground(Color.WHITE);
showSize.setText(getWidth()+","+getHeight());
add(showSize);
add(test);
add(eGetPoint);
add(dragLocationPoint);

addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent e) {
        test.setText("Drag");
        dragLocation = e.getPoint();
        dragLocationPoint.setText((int)(dragLocation.getX())+","+(int)(dragLocation.getY()));
        drag = true;
        setCursor(e_w_Cursor);
    }

    public void mouseReleased(MouseEvent e) {
        test.setText("Release");
        drag = false;
        setCursor(d_Cursor);
    }
});

addMouseMotionListener(new MouseMotionAdapter() {
    public void mouseDragged(MouseEvent e) {
        if (drag) {
            eGetPoint.setText(e.getX()+","+e.getY());
            showSize.setText(getWidth()+","+getHeight());   

            if (getWidth()-10 < dragLocation.getX() && dragLocation.getX() <= getWidth()) {
                dragLocation = e.getPoint();
                setSize(e.getX(), getHeight());
                setCursor(e_w_Cursor);  
            }

            if (getWidth() == e.getX()) {
                setCursor(e_w_Cursor);
            }
        }
    }

    public void mouseMoved(MouseEvent e) {
        if(getWidth()-10 < e.getX() && e.getX() <= getWidth()) {
            setCursor(e_w_Cursor);  
        } else {
            setCursor(d_Cursor);    
        }
    }
}); 

谢谢。

0 个答案:

没有答案