删除鼠标事件处理程序JavaFX

时间:2015-03-13 08:16:41

标签: javafx

我有一个响应鼠标的代码,例如当我进入图像区域时,图像变得更暗,当我退出它时,它变得正常,问题是当我运行代码时,我有9个图像,当鼠标在第一张图像上变暗,当它变为另一张图像时,第一张图像变暗,第二张图像不变。

以下是代码:

public class FrameController implements Initializable {

@FXML
private GridPane album;
ImageView im;

/**
 * Initializes the controller class.
 */
@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}

@FXML
private void mouseEntered(MouseEvent event) {
    im = (ImageView) event.getSource();
    System.out.println("Entered: " + im.getId());
    im.setOnMouseEntered(new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent event) {
            ColorAdjust ca = new ColorAdjust();
            ca.setBrightness(-0.5);
            im.setEffect(ca);
            }
        });
    }
    @FXML
    private void mouseExited(MouseEvent event) {
        im = (ImageView) event.getSource();
        System.out.println("Exited: " + im.getId());
        im.setOnMouseExited(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                im.setEffect(null);
                System.out.println("normal");
            }
        });
    }

}

1 个答案:

答案 0 :(得分:0)

我需要检查你的fxml,但你的代码中有一个基本错误。您需要移除onMouseEntered()onMouseExit()的重复使用。

尝试

@FXML
private void mouseEntered(MouseEvent event) {
    im = (ImageView) event.getSource();
    System.out.println("Entered: " + im.getId());
    ColorAdjust ca = new ColorAdjust();
    ca.setBrightness(-0.5);
    im.setEffect(ca);
}

@FXML
private void mouseExited(MouseEvent event) {
    im = (ImageView) event.getSource();
    System.out.println("Exited: " + im.getId());
    im.setEffect(null);
}