2010-03-10 29 views
0

HII 我已經使用圖片作爲一個JButton爲集到面板 ,但現在我想的是圖像 上使用鼠標移動偵聽器的行動,爲此目的我能做些什麼在JButton上使用鼠標Motion Listener?

以下是圖像的代碼

JButton buttonForPicture = new JButton(); 

    buttonForPicture.setBorder(new EmptyBorder(0, 0, 0, 0)); 
    buttonForPicture.setOpaque(false); 
    buttonForPicture.setIcon(new ImageIcon("/Users/hussainalisyed/Documents/Images/pic9.jpg")); 
    panel5.add(buttonForPicture,BorderLayout.CENTER); 

有沒有另一種方式來做到這一點 或 ...

回答

1

我不知道你問什麼?

你的按鈕就像任何其他的JButton:

buttonForPicture.addMouseMotionListener(new MouseMotionListener() { 
    @Override 
    public void mouseMoved(MouseEvent e) { 

    } 

    @Override 
    public void mouseDragged(MouseEvent e) { 

    } 
}); 

,捕捉運動事件對整個按鈕,而不僅僅是圖像。

1

閱讀JButton API有更改鼠標滾動圖標的方法,如果這就是你正在做的事情。在API中搜索包含「icon」的方法以查看您的選項。

如果您只是想知道如何編寫MouseMotionListener,那麼請閱讀How to Write a Mouse Motion Listener上的Swing tutorail中的部分以獲取工作示例。