[英]How to change an appearance of the button, when a key on a keyboard is pressed. JavaFx
I've already asked similar question here . 我已经在这里问过类似的问题。 The answer was correct, but still it seems like it's not applicable to my case.
答案是正确的,但似乎仍然不适用于我的情况。 Have a look at my code:
看一下我的代码:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
FlowPane root = new FlowPane();
root.setPadding(new Insets(40, 40, 40, 40));
Button btn = new Button("NEW BUTTON");
btn.setId("button");
root.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
btn.arm();
}
});
root.setOnKeyReleased(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
btn.disarm();
}
});
root.getChildren().addAll(btn);
primaryStage.setScene(new Scene(root, 310, 200));
primaryStage.getScene().getStylesheets().add("style.css");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Then I specified stylesheet: 然后我指定了样式表:
#button {
-fx-background-color: #403e40;
-fx-border-width: 0;
-fx-font-size: 30px;
-fx-text-fill: #b3b1b3;
-fx-background-radius: 0;
}
#button:hover {
-fx-background-color: #595759;
}
#button:pressed {
-fx-background-color: #ffffff;
-fx-text-fill: #403e40;
}
And in the end, it doesn't work. 最后,它不起作用。 I'm absolutely frustrated.
我绝对感到沮丧。 It works just fine if .css file isn't linked and doesn't work otherwise.
如果未链接.css文件,则此方法工作正常,否则将不起作用。 So, is it possible at all to change the appearance of the button if a key pressed with customized view?
因此,如果以自定义视图按下某个键,是否有可能改变按钮的外观?
You could create your own styles for armed and disarmed. 您可以创建自己的武装和解除武装风格。 Initially you set the disarmed style and depending on your key states you first remove all relevant styles and set the armed or disarmed style.
最初,您设置撤防样式,然后根据关键状态首先删除所有相关样式,然后设置撤防样式。 Like this:
像这样:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
FlowPane root = new FlowPane();
root.setPadding(new Insets(40, 40, 40, 40));
Button btn = new Button("NEW BUTTON");
btn.setId("button");
btn.getStyleClass().add("disarmed");
root.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
// remove existing styles
btn.getStyleClass().remove("armed");
btn.getStyleClass().remove("disarmed");
// add new style
btn.getStyleClass().add("armed");
btn.arm();
}
});
root.setOnKeyReleased(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
// remove existing styles
btn.getStyleClass().remove("armed");
btn.getStyleClass().remove("disarmed");
// add new style
btn.getStyleClass().add("disarmed");
btn.disarm();
}
});
root.getChildren().addAll(btn);
primaryStage.setScene(new Scene(root, 310, 200));
primaryStage.getScene().getStylesheets().add(getClass().getResource("style.css").toExternalForm());
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
And the css: 和CSS:
#button {
-fx-border-width: 0;
-fx-font-size: 30px;
-fx-text-fill: #b3b1b3;
-fx-background-radius: 0;
}
#button:hover {
-fx-background-color: #595759;
}
#button:pressed {
-fx-background-color: #ffffff;
-fx-text-fill: #403e40;
}
.armed {
-fx-background-color: #ff0000;
}
.disarmed {
-fx-background-color: #403e40;
}
Note that if you set the background color in the id selector (#), it will get higher precedence and you can't overwrite it. 请注意,如果您在ID选择器(#)中设置背景色,它将获得更高的优先级,并且无法覆盖它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.