多窗格鼠标事件

时间:2017-06-27 11:19:43

标签: javafx javafx-8

我们要求有一个“主节点”,当鼠标悬停在其中时,另一个节点(“悬停弹出节点”)出现在场景中。当鼠标悬停在“主节点”之外时,“悬停弹出节点”消失。此外,“主节点”在其中有一个“内部节点”,当点击时,另一个节点(“点击弹出节点”)出现在场景中,如果再次点击,“点击弹出节点”消失。

我们遇到的问题是,当鼠标悬停在“内部节点”内部时,“悬停弹出节点”消失了我们不想要的内容。当鼠标悬停在“主节点”之外时,我们只希望“悬停弹出节点”消失。即当鼠标位于“内部节点”内部时,我们仍然希望“悬停弹出节点”可见。

参见示例代码:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class MultiPaneMouseTest extends Application {

    private Rectangle hoverPopupNode;
    private Rectangle clickPopupNode;

    @Override
    public void init() throws Exception {
        super.init();
    }

    @Override
    public void start(Stage primaryStage) {

        BorderPane borderPane = new BorderPane();

        Rectangle mainNode = new Rectangle(200, 100);
        mainNode.setArcWidth(6);
        mainNode.setArcHeight(6);
        mainNode.setFill(Color.web("0x00ff00"));
        mainNode.setOnMouseEntered(e -> {
            System.out.println("mouse entered " + e);
            hoverPopupNode.setVisible(true);
        });
        mainNode.setOnMouseExited(e -> {
            System.out.println("mouse exited " + e);
            hoverPopupNode.setVisible(false);
        });

        Rectangle innerNode = new Rectangle(50, 25);
        innerNode.setArcWidth(6);
        innerNode.setArcHeight(6);
        innerNode.setFill(Color.web("0xffffff"));
        innerNode.setOnMouseClicked(e -> {
            System.out.println("mouse clicked " + e);
            clickPopupNode.setVisible(!clickPopupNode.isVisible());
        });

        hoverPopupNode = new Rectangle(100, 100);
        hoverPopupNode.setArcWidth(6);
        hoverPopupNode.setArcHeight(6);
        hoverPopupNode.setFill(Color.web("0x0000ff"));
        hoverPopupNode.setVisible(false);
        borderPane.setRight(hoverPopupNode);

        clickPopupNode = new Rectangle(100, 100);
        clickPopupNode.setArcWidth(6);
        clickPopupNode.setArcHeight(6);
        clickPopupNode.setFill(Color.web("0xff0000"));
        clickPopupNode.setVisible(false);
        borderPane.setLeft(clickPopupNode);


        StackPane stackPane = new StackPane();
        stackPane.getChildren().add(mainNode);
        stackPane.getChildren().add(innerNode);
        borderPane.setCenter(stackPane);

        Scene scene = new Scene(borderPane);

        primaryStage.setTitle("Sample Test");
        primaryStage.setScene(scene);
        primaryStage.setX(500.0);
        primaryStage.setY(500.0);
        primaryStage.setWidth(500.0);
        primaryStage.setHeight(500.0);
        primaryStage.show();
    }

    @Override
    public void stop() throws Exception {
        super.stop();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

1 个答案:

答案 0 :(得分:1)

使 let fileManager : FileManager = FileManager.default let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask let paths : NSArray = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true) as NSArray let documentsDirectory = paths.object(at: 0) as! NSString print(documentsDirectory) let contents : NSArray = try! fileManager.contentsOfDirectory(atPath: documentsDirectory as String) as NSArray let enumerator : NSEnumerator = contents.objectEnumerator() while let element = enumerator.nextObject() as? String { let fileName = element as NSString if fileName.pathExtension == "m4a" { let pathOfFile = documentsDirectory.appendingPathComponent(fileName as String) try! fileManager.removeItem(atPath: pathOfFile) } } 成为mainNode(或合适的子类),并将Pane添加为子节点。如果innerNodemainNode之间存在父子关系,那么当鼠标超过childNode时,鼠标仍将被视为悬停在mainNode上。您可以设置innerNode(或任何区域)的背景,以获得与Pane相同的填充和转角半径效果。

Rectangle