我有一堆(游戏)卡。其中三个显示(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*


谢谢!!

最佳答案

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

    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);

10-08 13:31