如何在JavaFX(JFoenix)中显示类似Android的toast消息?

如何在JavaFX(JFoenix)中显示类似Android的toast消息?,javafx,desktop-application,toast,jfoenix,Javafx,Desktop Application,Toast,Jfoenix,当我运行JFoenix演示时,我发现可以在JavaFX中生成材料设计吐司消息。我如何在自己的应用程序中实现这一点?(我是JavaFX初学者)以下是一个小的入门示例: 导入javafx.animation.*; 导入javafx.application.application; 导入javafx.event.EventHandler; 导入javafx.geometry.Insets; 导入javafx.geometry.Pos; 导入javafx.scene.scene; 导入javafx.s

当我运行JFoenix演示时,我发现可以在JavaFX中生成材料设计吐司消息。我如何在自己的应用程序中实现这一点?(我是JavaFX初学者)

以下是一个小的入门示例:

导入javafx.animation.*;
导入javafx.application.application;
导入javafx.event.EventHandler;
导入javafx.geometry.Insets;
导入javafx.geometry.Pos;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.Label;
导入javafx.scene.effect.DropShadow;
导入javafx.scene.input.MouseEvent;
导入javafx.scene.layout.Background;
导入javafx.scene.layout.BackgroundFill;
导入javafx.scene.layout.CornerRadii;
导入javafx.scene.layout.Pane;
导入javafx.scene.paint.Color;
导入javafx.stage.stage;
导入javafx.util.Duration;
公共类主扩展应用程序{
公共类Toast扩展标签{
专用浮点显示时间=0.90f;
私有浮动初始位置=0;
private float downbasia=10f;//标签出现前应位于下方的距离
插值器插值器=新插值器(){
@凌驾
受保护双曲线(双v){
//向上运动的二次曲线
返回-4*(v-0.5)*(v-0.5)+1;
}
};
公共吐司(串味精){
超级();
这个.setText(msg);
this.setOpacity(0);//以零开始,因为我们不希望在将其添加到根目录时显示它
this.setBackground(新背景(新背景填充(Color.WHEAT,cornerradi.EMPTY,Insets.EMPTY));//一些背景化妆品
此参数设置对齐(位置中心);
}
公共空间出现(){
this.setEffect(新的DropShadow(20,Color.BLACK));//Shadow
//在Y轴上创建动画
时间线=新时间线(
新关键帧(Duration.seconds(this.displayTime)、新键值(this.translateYProperty()、this.initialPositon this.getMinHeight()-20,插值器))
);
FadeTransition fd=新的FadeTransition(持续时间.s(this.displayTime),this);
fd.setCycleCount(1);
fd.setFromValue(0);
fd.setToValue(1.0);
fd.设置内插器(内插器);
fd.play();
timeline.play();
}
公共无效设置初始位置(浮动位置){
this.initialposition=position+downbasia;
此.setTranslateY(正电子+下偏压);
}
}
@凌驾
public void start(Stage primaryStage)引发异常{
浮球高度=230f;
浮栅宽度=230f;
吐司吐司=新吐司(“你好世界”);
烤面包片。设置最小宽度(场景宽度-40);
烤面包片。设定高度(30);
吐司。设置初始位置(场景高度);
toast.setTranslateX(sceneWidth/2-toast.getMinWidth()/2);
按钮showToast=新按钮(“显示toast”);
setOnMouseClicked(新的EventHandler(){
@凌驾
公共无效句柄(MouseEvent MouseEvent){
吐司;
}
});
窗格根=新窗格();
root.getChildren().addAll(toast,showtoos);
Scene myscene=新场景(根、sceneWidth、sceneWidth);
初生阶段:集新世(myscene);
primaryStage.show();
}
公共静态void main(字符串[]args){
发射(args);
}
}

别忘了重构toast类,以便在插值器和其他外观特性方面具有更大的灵活性。

ControlsFX有一些类似但不相同的功能。看看他们的通知是否足够好。看起来你可以只使用一个进入和淡出的标签。这就是我要尝试的。
import javafx.animation.*;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.effect.DropShadow;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Main extends Application {

    public class Toast extends Label {
        private float displayTime = 0.90f;
        private float initialPositon =0;
        private float downBias = 10f;// the distance the label should sit below before appearing

        Interpolator interpolator = new Interpolator() {
            @Override
            protected double curve(double v) {
                //quadratic curve for upward motion
                return -4 * ( v - 0.5 ) * ( v - 0.5 ) +1;
            }
        };
        public Toast(String msg){
            super();
            this.setText(msg);
            this.setOpacity(0);// starting it with zero because we dont want it to show up upoun adding it to the root
            this.setBackground(new Background(new BackgroundFill(Color.WHEAT, CornerRadii.EMPTY, Insets.EMPTY)));// some background cosmetics
            this.setAlignment(Pos.CENTER);
        }
        public void appear(){
            this.setEffect(new DropShadow(20, Color.BLACK));// Shadow
            //creating animation in the Y axis
            Timeline timeline = new Timeline(
                    new KeyFrame(Duration.seconds(this.displayTime), new KeyValue(this.translateYProperty(),this.initialPositon-this.getMinHeight()-20, interpolator))
            );
            FadeTransition fd  = new FadeTransition(Duration.seconds(this.displayTime),this);
            fd.setCycleCount(1);
            fd.setFromValue(0);
            fd.setToValue(1.0);
            fd.setInterpolator(interpolator);
            fd.play();
            timeline.play();
        }
        public  void  setInitialPositon(float positon){
            this.initialPositon = positon+downBias;
            this.setTranslateY(positon+downBias);
        }
    }

    @Override
    public void start(Stage primaryStage) throws Exception{
        float sceneHeight = 230f;
        float sceneWidth =230f;
        Toast toast = new Toast("Hello World");
        toast.setMinWidth(sceneWidth-40);
        toast.setMinHeight(30);

        toast.setInitialPositon(sceneHeight);

        toast.setTranslateX(sceneWidth/2-toast.getMinWidth()/2);
        Button showToast = new Button("show toast");
        showToast.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                toast.appear();
            }
        });
        Pane root = new Pane();
        root.getChildren().addAll(toast,showToast);
        Scene myscene =new Scene(root, sceneWidth, sceneWidth);
        primaryStage.setScene(myscene);
        primaryStage.show();
    }


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