2009-10-04 118 views
0

當我創建一個對象時,我可以設置一個函數嗎?在javafx中處理鼠標事件

鑑於我有一個容器類和類的CustomButton:

function doSomething():Void{} 

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

} 

簡單地說:我需要的主容器對象來處理髮生在放置在容器中的對象鼠標事件。

回答

1

你最簡單的變化是改變:顯然,如果你需要一個定製版本,可以延長按鈕

function doSomething():Void{} 

function doSomething(e:MouseEvent):Void{} 

action屬性是好的,但我確定你想要一些自定義翻轉效果或使用onMouseEntered等

+0

這些事情的結合起來了。我確實嘗試了將MouseEvent作爲參數發送,但那不起作用,並且我無法找到關於傳遞函數語法的任何文檔。 var towerButton:GenericButton = GenericButton { onMouseClicked:doSomething } 這將正確調用doSomething(e:MouseEvent)。 – Vargen 2009-10-07 10:16:10

+0

哦,你想傳遞一個函數給另一個? function doSomething(function(MouseEvent):otherfunc):Void {...} – 2009-10-07 15:26:54

1

如果我已經正確理解您的需求,我認爲可以通過一些語法更改來實現。

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 ] 
    } 
}