简体   繁体   中英

Handling mouse events in javafx

Can i set a function when creating an object like i can with variables?

Given i have a container class and a CustomButton class:

function doSomething():Void{}

var button:CustomButton = CustomButton{
   posX : 50;
   posY = 100;
   onMouseClicked: doSomething;

}

Short story: i need the main container object to handle mouse events that occurs in objects placed in the containers.

If I've understood your requirement correctly, I think it can be achieved with some syntax changes. Obviously you can extend Button if you need a custom version:

function doSomething():Void{
    println("clicked");
}

var button:Button = Button{
    text: "Click Me"
    translateX: 50;
    translateY: 100;
    action: doSomething
}
Stage {
    title : "ButtonTest"
    scene: Scene {
        width: 200
        height: 200
        content: [ button ]
    }
}

The easiest change for you is to change:

function doSomething():Void{}

to

function doSomething(e:MouseEvent):Void{}

The action property is nice but I'm sure you want some custom rollover effect or something using onMouseEntered etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM