JAVAFX实现像安卓一样的Toast

JAVAFX消息弹窗

一个像安卓的Toast弹窗小说,定时消失

package com.unclezs.UI.Utils;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.util.Timer;
import java.util.TimerTask;

/*
 *@author unclezs.com
 *@date 2019.07.06 12:46
 */
public class ToastUtil {
    private static Stage stage=new Stage();
    private static Label label=new Label();
    static {
        stage.initStyle(StageStyle.TRANSPARENT);//舞台透明
    }
    //默认3秒
    public static void toast(String msg) {
        toast(msg,3000);
    }

    /**
     * 指定时间消失
     * @param msg
     * @param time
     */
    public static void toast(String msg, int time) {
        label.setText(msg);
        TimerTask task= new TimerTask() {
            @Override
            public void run() {
                Platform.runLater(()->stage.close());
            }
        };
        init(msg);
        Timer timer=new Timer();
        timer.schedule(task,time);
        stage.show();
    }

    //设置消息
    private static void init(String msg) {
  	    Label label=new Label(msg);//默认信息
        label.setStyle("-fx-background: rgba(56,56,56,0.7);-fx-border-radius: 25;-fx-background-radius: 25");//label透明,圆角
        label.setTextFill(Color.rgb(225,255,226));//消息字体颜色
        label.setPrefHeight(50);
        label.setPadding(new Insets(15));
        label.setAlignment(Pos.CENTER);//居中
        label.setFont(new Font(20));//字体大小
        Scene scene=new Scene(label);
        scene.setFill(null);//场景透明
        stage.setScene(scene);
    }
}


效果图

在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaFX实现多选功能可以使用CheckBox或者ListView来实现。 1. CheckBox实现多选功能: CheckBox是JavaFX中提供的一个复选框控件,可以用来实现多选功能。下面是一个简单的示例代码: ```java import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.CheckBox; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class CheckBoxDemo extends Application { @Override public void start(Stage primaryStage) throws Exception { VBox root = new VBox(); root.setSpacing(10); root.setPadding(new Insets(10)); CheckBox cb1 = new CheckBox("选项1"); CheckBox cb2 = new CheckBox("选项2"); CheckBox cb3 = new CheckBox("选项3"); root.getChildren().addAll(cb1, cb2, cb3); Scene scene = new Scene(root, 300, 200); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ``` 2. ListView实现多选功能: ListView是JavaFX中提供的一个列表控件,可以用来实现多选功能。下面是一个简单的示例代码: ```java import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.ListView; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class ListViewDemo extends Application { @Override public void start(Stage primaryStage) throws Exception { VBox root = new VBox(); root.setSpacing(10); root.setPadding(new Insets(10)); ObservableList<String> items = FXCollections.observableArrayList( "选项1", "选项2", "选项3", "选项4", "选项5" ); ListView<String> listView = new ListView<>(items); listView.getSelectionModel().setSelectionMode(javafx.scene.control.SelectionMode.MULTIPLE); root.getChildren().add(listView); Scene scene = new Scene(root, 300, 200); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ``` 在ListView中设置SelectionMode为MULTIPLE,即可实现多选功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值