鼠标悬停时更改图标

Change icon on mouse over

我想创建当我将鼠标移到上方时更改默认图片的按钮。我做了这个例子,但它不能正常工作:

public class MainApp extends Application
{
    @Override
    public void start(Stage stage) throws Exception
    {
        StackPane bp = new StackPane();
        bp.getChildren().add(ReportsIcon());
        bp.setPrefSize(600, 600);

        Scene scene = new Scene(bp);
        scene.setFill(Color.ANTIQUEWHITE);

        stage.setTitle("JavaFX and Maven");
        stage.setScene(scene);
        stage.show();
    }

    private static final ImageView ReportsFirstIcon;

    static
    {
        ReportsFirstIcon = new ImageView(MainApp.class.getResource("/images/monitoring-colour.png").toExternalForm());
    }

    private static final ImageView RportsIconsSecond;

    static
    {
        RportsIconsSecond = new ImageView(MainApp.class.getResource("/images/monitoring-green.png").toExternalForm());
    }

    private HBox ReportsIcon()
    {
        HBox bpi = new HBox();
        bpi.setAlignment(Pos.CENTER);
        // Add Label to the Icon
        Text inftx = new Text("Reports");
        inftx.setFont(Font.font("Verdana", FontWeight.NORMAL, 13));   // Set font and font size
        inftx.setFill(Color.BLACK); // Set font color

        // Zoom into the picture and display only selected area
        Rectangle2D viewportRect = new Rectangle2D(0, 0, 0, 0);
        ReportsFirstIcon.setViewport(viewportRect);
        BorderPane pp = new BorderPane();
        pp.setCenter(ReportsFirstIcon);

        bpi.getChildren().addAll(pp, inftx);

        bpi.setOnMouseEntered(new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent t)
            {
                pp.setCenter(ReportsFirstIcon);
            }
        });

        bpi.setOnMouseExited(new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent t)
            {
                pp.setCenter(RportsIconsSecond);
            }
        });

        bpi.setOnMouseClicked(new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent t)
            {
                // Open new window
            }
        });

        return bpi;
    }

    private HBox mouseOver(final HBox bp)
    {
        bp.setOnMouseEntered(new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent t)
            {
                bp.setStyle("-fx-background-color: linear-gradient(#f2f2f2, #f2f2f2);"
                    + "  -fx-background-insets: 0 0 -1 0, 0, 1, 2;"
                    + "  -fx-background-radius: 3px, 3px, 2px, 1px;");
            }
        });

        bp.setOnMouseExited(new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent t)
            {
                bp.setStyle("-fx-background-color: linear-gradient(#f2f2f2, #d4d4d4);"
                    + "  -fx-background-insets: 0 0 -1 0, 0, 1, 2;"
                    + "  -fx-background-radius: 3px, 3px, 2px, 1px;");
            }
        });

        return bp;
    }

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

}

现在代码无法正常工作,当我将鼠标移到用于保存图片的第二个 BorderPane 之外时,原始图像不会返回。 当我将鼠标移出舞台时,图片发生了变化。有什么解决办法吗?

我想默认显示第一张图片,当我将鼠标移到它上面时用第二张图片替换它。当我将鼠标移到外面时,我想恢复原图。

解决方法

您可以根据按钮的悬停将按钮的图形 属性 绑定到适当的 ImageView 属性。

button.graphicProperty().bind(
    Bindings.when(
        button.hoverProperty()
    )
        .then(meatView)
        .otherwise(lambView)
);

未悬停:

悬停:

可执行样本

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.layout.StackPane;
import javafx.scene.text.*;
import javafx.stage.Stage;

public class MuttonMorph extends Application {

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

    @Override
    public void start(Stage stage) {
        ImageView lambView = new ImageView(
                new Image(
                        lambLoc
                )
        );

        ImageView meatView = new ImageView(
                new Image(
                        meatLoc
                )
        );

        Button button = new Button("Lamb,\nit's what's for dinner");
        button.setContentDisplay(ContentDisplay.TOP);
        button.setTextAlignment(TextAlignment.CENTER);
        button.setFont(Font.font(16));

        button.graphicProperty().bind(
                Bindings.when(
                        button.hoverProperty()
                )
                        .then(meatView)
                        .otherwise(lambView)
        );

        StackPane layout = new StackPane(button);
        layout.setPadding(new Insets(30));

        stage.setScene(new Scene(layout));
        stage.show();
    }

    // Icons are Linkware (Backlink to http://icons8.com required)
    private static final String lambLoc = "http://icons.iconarchive.com/icons/icons8/ios7/96/Animals-Sheep-icon.png";
    private static final String meatLoc = "http://icons.iconarchive.com/icons/icons8/ios7/96/Food-Lamb-Rack-icon.png";
}

替代方法

您可以通过根据按钮的 :hover CSS 伪 class 和 CSS 定义适当的 CSS 样式规则进行设置,从而在没有绑定的情况下做类似的事情=13=]属性。