简体   繁体   English

JavaFX8如何将“按”状态传递给孩子?

[英]JavaFX8 How to pass the “press” status to child?

I have an ImageView in a Pane, and the pane has been set a MouseEventHandler to do some actions. 我在窗格中有一个ImageView,并且已将窗格设置为MouseEventHandler来执行某些操作。

The ImageView's Image is defined in a CSS file as below: ImageView的图像在CSS文件中定义如下:

.imageView{
    -fx-image: url("image/normal.png");
}

.imageView:pressed{
    -fx-image: url("/image/press.png");
}

Under normal status, the ImageView's image is 'normal.png'. 在正常状态下,ImageView的图像为“ normal.png”。 When the user presses the ImageView, its image will be changed to 'press.png'. 当用户按下ImageView时,其图像将更改为'press.png'。 Then, when released, its image will be changed back to 'normal.png'. 然后,释放后,其图像将更改回'normal.png'。

However, if the user does not press on the ImageView's image exactly, even though Pane's MouseEvent is trigged, the image will not be changed. 但是,如果用户未完全按下ImageView的图像,即使触发了Pane的MouseEvent,该图像也不会更改。

How can I do to change ImageView's image when its parent Pane is pressed. 当按下其父窗格时,如何更改ImageView的图像。 For instance, on Android, the image will be switched automatically. 例如,在Android上,图像将自动切换。 It is very convenient. 非常方便。

Add a style class to the pane ("image-container", for example), then in the css do 将样式类添加到窗格中(例如,“ image-container”),然后在CSS中执行

.imageView{
    -fx-image: url("image/normal.png");
}

.image-container:pressed .imageView {
    -fx-image: url("/image/press.png");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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