英文:
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("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);
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("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);
}}
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 ;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论