我有一个带有ActionListener的JButton。单击按钮时执行操作,但我希望在单击后执行操作(即释放鼠标按钮时)。我怎么能这样做?
答案 0 :(得分:4)
您无法使用ActionListener
执行此操作。您必须添加MouseListener
并处理mouseReleased
事件。
示例:
addMouseListener(new MouseListener() {
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
// TODO: add your code here
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
});
或者更简单,使用MouseAdapter
:
addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
// TODO: add your code here
}
});