JavaFX按钮悬停

huangapple go评论74阅读模式
英文:

JavaFX Button Hover

问题

以下是您的JavaFX Button代码的翻译:

Button playButton = new Button("播放");
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);

我希望当您将鼠标悬停在按钮上时,文本和边框的颜色会变为灰色。您该如何实现这一点呢?

英文:

I have the following code for my JavaFX Button.

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

		@Override
		public void handle(ActionEvent event) {

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

I want that when you hover over the button that the color of the text and the border turns gray.
How can I do that?

答案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("悬停按钮");
        
        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("按钮");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
button.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;    
}

图片链接已被删除。

英文:

An example of styled button

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(&quot;Button Hover&quot;);
    
    
    StackPane root = new StackPane();
    root.getChildren().add(btn);
    
    Scene scene = new Scene(root, 300, 250);
    scene.getStylesheets()
    .add(this.getClass().getResource(&quot;button.css&quot;).toExternalForm());
    primaryStage.setTitle(&quot;button&quot;);
    primaryStage.setScene(scene);
    primaryStage.show();
}


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

button.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 ;    
 
 }

JavaFX按钮悬停

JavaFX按钮悬停

huangapple
  • 本文由 发表于 2020年5月19日 17:21:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/61887557.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定