javafx 2 and css pseudo-classes: setting hover attributes in setStyle method

前端 未结 2 783
悲哀的现实
悲哀的现实 2021-02-04 05:32

I have a simple Scene with this code:

scene.getStylesheets().add(\"packagename/testcss.css\");

And my testcss.css is:

.button {         


        
2条回答
  •  逝去的感伤
    2021-02-04 06:06

    You can optionally set them css to be changed/updated programmatically when different events are triggered.. I added the following functions to the fxml files events against my checkin button.

     @FXML
        private void onHover(MouseEvent event)
        {
            if(btnCheckin.getText().contentEquals("Check Out"))
            {
                btnCheckin.setStyle("-fx-font-size: 20; -fx-font-weight: bold; -fx-padding: -5 -5 -5 -5;   \n" +
                                "    -fx-border-color: #e2e2e2;\n" +
                                "    -fx-border-width: 2;\n" +
                                "    -fx-border-radius: 5;\n" +
                                "    -fx-background-radius: 5;\n" +
                                "    -fx-background-color: #3a3a3a;\n" +
                                "    -fx-font-family: \"Lato Bold\", Helvetica, Arial, sans-serif;\n" +
                                "    -fx-text-fill: white;\n" +
                                "    -fx-background-insets: 0 0 0 0, 0, 1, 2;");
            }
        }
    
        @FXML
        private void onExit(MouseEvent event)
        {
            if(btnCheckin.getText().contentEquals("Check Out"))
            {
                btnCheckin.setStyle("-fx-font-size: 20; -fx-font-weight: bold; -fx-padding: -5 -5 -5 -5;   \n" +
                                "    -fx-border-width: 2;\n" +
                                "    -fx-border-radius: 5;\n" +
                                "    -fx-background-radius: 5;\n" +
                                "    -fx-font-family: \"Lato Bold\", Helvetica, Arial, sans-serif;\n" +
                                "    -fx-text-fill: white;\n" +
                                "    -fx-background-insets: 0 0 0 0, 0, 1, 2;\n" +
                                "    -fx-background-color: #EE6559;\n" +
                                "    -fx-border-color: #EE6559;");
            }
        }
    
        @FXML
        private void onPress(MouseEvent event)
        {
            if(btnCheckin.getText().contentEquals("Check Out"))
            {
                btnCheckin.setStyle("-fx-font-size: 20; -fx-font-weight: bold; -fx-padding: -5 -5 -5 -5;   \n" +
                                "    -fx-border-color: #e2e2e2;\n" +
                                "    -fx-border-width: 2;\n" +
                                "    -fx-border-radius: 5;\n" +
                                "    -fx-background-radius: 5;\n" +
                                "    -fx-background-color: white;\n" +
                                "    -fx-font-family: \"Lato Bold\", Helvetica, Arial, sans-serif;\n" +
                                "    -fx-text-fill: #1d1d1d;\n" +
                                "    -fx-background-insets: 0 0 0 0, 0, 1, 2;");
            }
        }
    
        @FXML
        private void onRelease(MouseEvent event)
        {
            if(btnCheckin.getText().contentEquals("Check Out"))
            {
                onHover(event);
            }
        }
    

提交回复
热议问题