Notifications 生成图标通知javafx

Notifications 生成图标通知javafx,notifications,icons,javafx,Notifications,Icons,Javafx,我想问一下,如何在现有节点上创建通知图标! 这里是一个链接,我用它作为灵感! 如果有人能给我一个帮助,那我真的可以省钱了。^ ^你可以在这里找到一个例子: 你可以在这里找到一个例子: 我刚刚解决了这个问题。 我使用了billman创建的动画,但这次没有扩展StackPane,而是扩展了Button节点,我将通知图标设置为从StackPane扩展,效果非常好 下面是这样做的方法: AnimatedButton.java import javafx.animation.Interpolato

我想问一下,如何在现有节点上创建通知图标! 这里是一个链接,我用它作为灵感!
如果有人能给我一个帮助,那我真的可以省钱了。^ ^

你可以在这里找到一个例子:

你可以在这里找到一个例子:
我刚刚解决了这个问题。 我使用了billman创建的动画,但这次没有扩展StackPane,而是扩展了Button节点,我将通知图标设置为从StackPane扩展,效果非常好

下面是这样做的方法:

AnimatedButton.java

    import javafx.animation.Interpolator;
    import javafx.animation.KeyFrame;
    import javafx.animation.KeyValue;
    import javafx.animation.Timeline;
    import javafx.animation.TranslateTransition;
    import javafx.scene.Node;
    import javafx.scene.control.MenuItem;
    import javafx.util.Duration;


    public class Icons extends MenuItem {

    private final Node icon;

    private AnimationType type;

    public Icons(String text, Node icon, AnimationType type) {
        setText(text);
        setGraphic(icon);
        this.icon = icon;
        this.type = type;
        addAnimation();
    }

    private void addBlinkAnimation() {
        final Timeline timeline = new Timeline();
        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.setAutoReverse(true);
        final KeyValue kv = new KeyValue(icon.opacityProperty(), 0.0);
        final KeyFrame kf = new KeyFrame(Duration.millis(700), kv);
        timeline.getKeyFrames().add(kf);
        timeline.play();
    }

    private void addJumpAnimation() {
        final TranslateTransition translateTransition = new TranslateTransition(Duration.millis(200), icon);
        final double start = 0.0;
        final double end = start - 4.0;
        translateTransition.setFromY(start);
        translateTransition.setToY(end);
        translateTransition.setCycleCount(-1);
        translateTransition.setAutoReverse(true);
        translateTransition.setInterpolator(Interpolator.EASE_BOTH);
        translateTransition.play();
    }

    public enum AnimationType {

        BLINK, JUMP, NONE;
    };

    private void addAnimation() {
        switch (type) {
            case BLINK:
                addBlinkAnimation();
                break;
            case JUMP:
                addJumpAnimation();
                break;
            case NONE:
                break;
            default:
                break;
        }
    }
}
这里是main.java

    import javafx.application.Application;
    import javafx.scene.Node;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.control.MenuButton;
    import javafx.scene.image.Image;
    import javafx.scene.image.ImageView;
    import javafx.scene.layout.StackPane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Circle;
    import javafx.stage.Stage;


    public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        MenuButton menu = new MenuButton("Bienvenue, Yassine", new ImageView(new Image("http://cdn1.iconfinder.com/data/icons/fs-icons-ubuntu-by-franksouza-/32/google-chrome.png", 20, 20, true, true)));
        menu.setCenterShape(true);
        Icons btn = new Icons("Notification", createNotification("5"), Icons.AnimationType.JUMP);
        Icons btn2 = new Icons("Mails", createNotification("4"), Icons.AnimationType.JUMP);
        Icons btn3 = new Icons("Private Messages", createNotification("10"), Icons.AnimationType.JUMP);
        menu.getItems().addAll(btn, btn2, btn3);
        StackPane root = new StackPane();
        Scene scene = new Scene(root, 300, 250);
        scene.getStylesheets().add(getClass().getResource("notification_icon.css").toExternalForm());
        root.getChildren().add(menu);
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    private Node createNotification(String number) {
        StackPane p = new StackPane();
        Label lab = new Label(number);
        lab.setStyle("-fx-text-fill:white");
        Circle cercle = new Circle(10, Color.rgb(200, 0, 0, .9));
        cercle.setStrokeWidth(2.0);
        cercle.setStyle("-fx-background-insets: 0 0 -1 0, 0, 1, 2;");
        cercle.setSmooth(true);
        p.getChildren().addAll(cercle, lab);
        return p;
    }

}
还有css

    .label{
    -fx-font-size: 12;
    -fx-font-weight: bold;
    -fx-text-fill: black;
    -fx-padding:5;
}
我刚刚解决了这个问题。 我使用了billman创建的动画,但这次没有扩展StackPane,而是扩展了Button节点,我将通知图标设置为从StackPane扩展,效果非常好

下面是这样做的方法:

AnimatedButton.java

    import javafx.animation.Interpolator;
    import javafx.animation.KeyFrame;
    import javafx.animation.KeyValue;
    import javafx.animation.Timeline;
    import javafx.animation.TranslateTransition;
    import javafx.scene.Node;
    import javafx.scene.control.MenuItem;
    import javafx.util.Duration;


    public class Icons extends MenuItem {

    private final Node icon;

    private AnimationType type;

    public Icons(String text, Node icon, AnimationType type) {
        setText(text);
        setGraphic(icon);
        this.icon = icon;
        this.type = type;
        addAnimation();
    }

    private void addBlinkAnimation() {
        final Timeline timeline = new Timeline();
        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.setAutoReverse(true);
        final KeyValue kv = new KeyValue(icon.opacityProperty(), 0.0);
        final KeyFrame kf = new KeyFrame(Duration.millis(700), kv);
        timeline.getKeyFrames().add(kf);
        timeline.play();
    }

    private void addJumpAnimation() {
        final TranslateTransition translateTransition = new TranslateTransition(Duration.millis(200), icon);
        final double start = 0.0;
        final double end = start - 4.0;
        translateTransition.setFromY(start);
        translateTransition.setToY(end);
        translateTransition.setCycleCount(-1);
        translateTransition.setAutoReverse(true);
        translateTransition.setInterpolator(Interpolator.EASE_BOTH);
        translateTransition.play();
    }

    public enum AnimationType {

        BLINK, JUMP, NONE;
    };

    private void addAnimation() {
        switch (type) {
            case BLINK:
                addBlinkAnimation();
                break;
            case JUMP:
                addJumpAnimation();
                break;
            case NONE:
                break;
            default:
                break;
        }
    }
}
这里是main.java

    import javafx.application.Application;
    import javafx.scene.Node;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.control.MenuButton;
    import javafx.scene.image.Image;
    import javafx.scene.image.ImageView;
    import javafx.scene.layout.StackPane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Circle;
    import javafx.stage.Stage;


    public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        MenuButton menu = new MenuButton("Bienvenue, Yassine", new ImageView(new Image("http://cdn1.iconfinder.com/data/icons/fs-icons-ubuntu-by-franksouza-/32/google-chrome.png", 20, 20, true, true)));
        menu.setCenterShape(true);
        Icons btn = new Icons("Notification", createNotification("5"), Icons.AnimationType.JUMP);
        Icons btn2 = new Icons("Mails", createNotification("4"), Icons.AnimationType.JUMP);
        Icons btn3 = new Icons("Private Messages", createNotification("10"), Icons.AnimationType.JUMP);
        menu.getItems().addAll(btn, btn2, btn3);
        StackPane root = new StackPane();
        Scene scene = new Scene(root, 300, 250);
        scene.getStylesheets().add(getClass().getResource("notification_icon.css").toExternalForm());
        root.getChildren().add(menu);
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    private Node createNotification(String number) {
        StackPane p = new StackPane();
        Label lab = new Label(number);
        lab.setStyle("-fx-text-fill:white");
        Circle cercle = new Circle(10, Color.rgb(200, 0, 0, .9));
        cercle.setStrokeWidth(2.0);
        cercle.setStyle("-fx-background-insets: 0 0 -1 0, 0, 1, 2;");
        cercle.setSmooth(true);
        p.getChildren().addAll(cercle, lab);
        return p;
    }

}
还有css

    .label{
    -fx-font-size: 12;
    -fx-font-weight: bold;
    -fx-text-fill: black;
    -fx-padding:5;
}

但你应该先向我们展示你的努力。显示测试演示代码,至少是css和/或动画的代码。我尝试过使用工具提示,但结果不太好,这就是为什么我首先要问的是,男士^^^^也尝试一下。如何使用弹出窗口^^^?任何人都有想法,也许?但您应该首先向我们展示您的努力。显示测试演示代码,至少是css和/或动画的代码。我尝试过使用工具提示,但结果不太好,这就是为什么我首先要问的是,男士^^^^也尝试一下。如何使用弹出窗口^^^^?有人有想法吗??