当按下键盘上的键时,如何更改按钮的外观。了JavaFx

时间:2015-07-19 13:18:44

标签: java css button view javafx

我已经问了类似的问题here。答案是正确的,但似乎它不适用于我的情况。看看我的代码:

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);
}
}

然后我指定了样式表:

#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;
}

最后,它不起作用。我非常沮丧。如果.css文件没有链接,它可以正常工作,否则不起作用。那么,如果按下自定义视图的按键,是否可以改变按钮的外观?

1 个答案:

答案 0 :(得分:0)

你可以为武装和解除武装创建自己的风格。最初设置撤防的样式,根据您的关键状态,您首先删除所有相关样式并设置武装或撤防样式。像这样:

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);
    }
}

和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;
}

请注意,如果在id选择器(#)中设置背景颜色,它将获得更高的优先级,并且您无法覆盖它。