css - javafx 8 使用 css 在悬停按钮上创建图像

标签 css image button hover javafx-8

我正在制作一个马里奥主题的应用程序,我希望按钮的效果是当用户将鼠标悬停在它上面时,按钮文本旁边会出现一个蘑菇(图像),并且当鼠标移开时消失。我正在尝试使用 css 来做到这一点。

如何做到这一点?

最佳答案

使用悬停伪类。

.button:hover{
    -fx-graphic: url('your_path_to_image');
}

完整示例

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class Main extends Application {

    public void start(Stage primaryStage) {
        Button button = new Button("Click");
        HBox container = new HBox(button);
        container.setAlignment(Pos.CENTER);
        Scene scene = new Scene(container, 200, 200);
        scene.getStylesheets().add(getClass().getResource("/style.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}

样式.css

.button:hover{
    -fx-graphic: url('http://files.softicons.com/download/game-icons/super-mario-icons-by-sandro-pereira/png/16/Mushroom%20-%201UP.png');
}

图片

enter image description here

悬停

enter image description here

关于css - javafx 8 使用 css 在悬停按钮上创建图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29866599/

相关文章:

html - IE 10 - 尽管父位置为 :relative,div 仍被下推

html - Bootstrap : Modal alignment issues and no transition effect upon click

python - 从屏幕流式传输帧,生成视频

jQuery 验证适用于输入类型 ="submit",但不适用于 html 按钮元素。为什么?

c# - asp.net按钮点击客户端和服务器端代码

html - 2-3 个 div - 都具有最高 div 的高度 - 最后一个 div 使用其余空间

html - 我如何(可以)使用 attr() 获取 z-index,一个 CSS 属性,以便在我的样式定义中与 calc() 一起使用?

HTML/CSS 错误放置的标题和丢失的图像

javascript - 我怎样才能以 <a href="开始并在此处有一个数组元素然后以 </a> 结束?

java - 处理填充问题的 list