(如何).setOnMouseClicked覆盖以前的事件并返回旧事件?

时间:2017-01-17 13:41:01

标签: java javafx mouseevent

我有一堆(游戏)牌。其中三个显示(gui),水平显示为矩形对象,javaFx。默认情况下,我可以通过单击左侧或右侧卡片来浏览卡片。我的FXML文件中的单个矩形是setOnMouseClicked。他们每个人都有自己的ID - > (fstCard,scdCard,trdCard)。矩形旁边是一个“选择卡片”按钮。现在,如果我按下此按钮,我必须选择其中一个显示的卡,我想激活一个方法并返回默认的setOnMouseClicked。要激活“选择按钮”:

public void switchChooseDevCard(MouseEvent event) {


    if(event.getSource().equals(chooseButton)){

        fstDeve.setOnMouseClicked(event1 -> System.out.print("hello"));
 **----->> how can i return to the default ?**
    }

    if (event.getSource().equals(fstCard)) {
        ba = devCardStack.size() - 1;
        if (devCardStack.size() <= 3) {
            giveTooltip();
        } else if (devCardStack.size() > 3) {
            int passages = devCardStack.size();
            fstDeve.setFill(scdDeve.getFill());
            scdDeve.setFill(trdDeve.getFill());

            if (a >= 3 && a < passages) {
                trdDeve.setFill(devCardStack.get(a));
                a++;
                giveTooltip();
            } else if (a == passages) {
                a = 0;
                trdDeve.setFill(devCardStack.get(a));
                a++;
                giveTooltip();
            } else {
                trdDeve.setFill(devCardStack.get(a));
                a++;
                giveTooltip();
            }
        }
    }  *rest of the code*

谢谢!

2 个答案:

答案 0 :(得分:1)

您可以将处理程序存储在临时变量中,并在以后需要时将其还原:

    Button btn = new Button("Test");

    //default event handler
    btn.setOnMouseClicked(new EventHandler<Event>() {
        @Override
        public void handle(Event event) {
            System.out.println("hello");
        }
    });

    //store default event handler
    EventHandler<Event> oldHandler = (EventHandler<Event>) btn.getOnMouseClicked();

    //set new event handler
    btn.setOnMouseClicked(new EventHandler<Event>() {
        @Override
        public void handle(Event event) {
            System.out.println("test");
        }
    });

    //restore default event handler
    btn.setOnMouseClicked(oldHandler);

答案 1 :(得分:0)

修正了它: 只需调用默认方法..

fstDeve.setOnMouseClicked(event1 -> switchChooseDevCard(event1));