簡體   English   中英

如何在進入和退出窗格的鼠標事件上更改顏色標簽java fxml控制器

[英]how to mouse event entered and exited pane change color label java fxml controller

我想在按下鼠標時更改標簽“ Kategori”的顏色,然后在“ paneKategori”上退出(看圖片)。 我在fxml控制器中的代碼

    @FXML
void btnProdukMouseEntered(javafx.scene.input.MouseEvent event) {
    if ( event.getSource() == paneKategori) {
        labelKategori.setStyle("-fx-background-color: #FF0000;");
    }

}

那對我沒有用。

這是我在場景構建器中的GUI。 GUI

MOUSE_PRESSEDMOUSE_DRAGGEDMOUSE_RELEASED處理程序創建處理程序。 檢查事件的位置是否在MOUSE_DRAGGED的節點內。

@Override
public void start(Stage primaryStage) throws Exception {
    Button button = new Button("Drag me");
    final String style = "-fx-background-color: red;";

    button.setOnMousePressed(evt -> {
        if (evt.isPrimaryButtonDown()) {
            button.setStyle(style);
        }
    });
    button.setOnMouseDragged(evt -> {
        if (evt.isPrimaryButtonDown()) {
            button.setStyle(button.contains(evt.getX(), evt.getY()) ? style : null);
        }
    });
    button.setOnMouseReleased(evt -> {
        button.setStyle(null);
    });

    Scene scene = new Scene(new StackPane(button), 300, 300);
    primaryStage.setScene(scene);
    primaryStage.show();
}

順便說一句:在大多數情況下,在事件處理程序中檢查源是不好的做法,因為您只需為要比較事件的source屬性的節點注冊事件處理程序,就可以輕松避免檢查。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM