简体   繁体   English

按下键盘上的键时如何更改按钮的外观。 JavaFX

[英]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.

相关问题 当按下按钮时,ProgressIndicator将冻结。 JavaFX - ProgressIndicator freezes when button is pressed. JavaFX 当按下键盘上的相应键时如何使按钮按下? JavaFX - How make a button pressed when corresponding key on a keyboard is pressed? JavaFX 当按下一个键时,重新绘制JPanel。 (java) - Repaintng a JPanel when a key is pressed. (java) 按下键盘键时如何改变焦点在不同的Jbutton上? - How to change focus different Jbuttons when a keyboard key is pressed? 如何更改Java中按下/单击/选中按钮的外观? - How to change appearance of pressed/clicked/selected button in Java? 仅当按下Enter键时,按键侦听器才起作用。 对于其他键不起作用 - Key listener only works when the Enter key is pressed. Doesn't work for other keys 按下 javafx 按钮时不执行 - No execution when javafx button pressed 按下返回按钮时定时器未关闭。 按下按钮时强制关闭错误 - Timer not shutting down when back button is pressed. Forceclose error on button press 在 JavaFX 中按下按钮时如何创建随机圆圈位置? - How to create random circle locations when Button is pressed in JavaFX? 如何以编程方式更改 Google 键盘外观? - How to change Google Keyboard appearance programmatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM