JavaFX按钮悬停

问题描述 投票:0回答:1

我的JavaFX按钮有以下代码。

Button playButton = new Button("Play");
    playButton.setLayoutX(980);
    playButton.setLayoutY(rectangle.getLayoutY());
    playButton.setFont(new Font(45));
    playButton.setTextFill(Color.BLACK);
    playButton.setPrefSize(200, 125);
    playButton.setStyle("-fx-border-color:black;" + "-fx-border-width: 3 3 3 3");
    playButton.setBackground(new Background(blackJackBackgroundImage));
    playButton.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {

        }
    });
    m_layout.getChildren().add(playButton);

我希望当你悬停在按钮上时,文字和边框的颜色变成灰色,我该怎么做?

java button javafx
1个回答
1
投票

样式化按钮的例子

package buttonhover;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;


public class ButtonHover extends Application {

@Override
public void start(Stage primaryStage) {
    Button btn = new Button();
    btn.setText("Button Hover");


    StackPane root = new StackPane();
    root.getChildren().add(btn);

    Scene scene = new Scene(root, 300, 250);
    scene.getStylesheets()
    .add(this.getClass().getResource("button.css").toExternalForm());
    primaryStage.setTitle("button");
    primaryStage.setScene(scene);
    primaryStage.show();
}


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

按钮.css

.button {  
-fx-border-color:black;
-fx-border-width: 3 3 3 3 ;  
}
.button .text {
-fx-fill:black;
 }
.button:hover .text{
-fx-fill: grey ;    


 }
 .button:hover {
 -fx-border-color: grey ;    

 }

enter image description here

hover

© www.soinside.com 2019 - 2024. All rights reserved.