简体   繁体   English

如何处理鼠标事件FXML

[英]How to handle mouse event FXML

So I'm building this application for a school project and I just can't seem to figure out the following: 因此,我正在为学校项目构建此应用程序,但似乎无法弄清楚以下内容:

(Using scenebuilder by the way) (顺便使用SceneBuilder)

I want to use an image as a button, so I made a button and set an image on the button. 我想将图像用作按钮,所以我制作了一个按钮并在该按钮上设置了图像。 I then made the background color for the button transparent so you would only see the image. 然后,我将按钮的背景色设为透明,这样您只会看到图像。 Sadly enough when doing this, the image remains there as a static image that does not move whenever you click on it so you don't really have the click effect. 令人遗憾的是,执行此操作时,该图像仍保留为静态图像,该静态图像在您单击时不会移动,因此您实际上没有单击效果。

First we were coding the application in Fx but then later we decided to work with fxml. 首先,我们使用Fx对应用程序进行编码,但随后,我们决定使用fxml。 But, when I was still coding in fx I did get this working by using eventhandlers to simulate the "click" like: 但是,当我仍在使用fx进行编码时,确实可以通过使用事件处理程序来模拟“点击”来完成此工作,例如:

button5.addEventHandler(MouseEvent.MOUSE_ENTERED, (MouseEvent e) -> {
        button5.setScaleX(1.1);
        button5.setScaleY(1.1);
    });


    button5.addEventHandler(MouseEvent.MOUSE_PRESSED, (MouseEvent e) -> {
        button5.setEffect(shadow);
    });

But that doesn't seem to work now, or I just don't know how to do it haha. 但这似乎现在不起作用,或者我只是不知道该怎么做哈哈。 I'm very new to this and I can't seem to get the controller to make this work. 我对此很陌生,似乎无法让控制器来完成这项工作。

Hope somebody knows what I mean and how to solve this! 希望有人知道我的意思以及如何解决这个问题!

Use CSS. 使用CSS。 For example, you could do something like 例如,您可以执行以下操作

.button:pressed > .image-view {
    -fx-opacity: 80%;
}

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

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