zoukankan      html  css  js  c++  java
  • Java FX组件

    进度条组件:

    import javafx.geometry.Insets;
    import javafx.scene.control.Label;
    import javafx.scene.control.ProgressBar;
    import javafx.scene.layout.HBox;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import util.Handler;
    import java.math.BigDecimal;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    
    /**
     * 进度条组件
     */
    public class MyProgressBar {
    
        private static final Logger LOG = LoggerFactory.getLogger(MyProgressBar.class);
    
        private HBox hBox;
    
        /**
         * 进度条标题
         */
        private Label title;
    
        /**
         * 进度条
         */
        private ProgressBar progressBar;
    
        /**
         * 进度条提示
         */
        private Label message;
    
        /**
         * 每个步骤所占的比例
         */
        private Double step;
    
        /**
         * 进度条总量
         */
        private Double total = 0.0;
    
        /**
         * 更新进度提示
         * @param message
         */
        public void setLabel(String message) {
            this.message.setText(message);
        }
    
        /**
         * 更新进度条
         * @param value
         */
        public void setValue(double value) {
            if(value == 0) {
                total = 0.0;
            }
            progressBar.setProgress(value);
        }
    
        /**
         * 控制进度条组件显示
         * @param flag
         */
        public void setVisible(boolean flag) {
            progressBar.setVisible(flag);
            title.setVisible(flag);
            message.setVisible(flag);
            hBox.setDisable(!flag);
        }
    
        /**
         * 获取进度条组件
         * @return
         */
        public HBox getProgressBar() {
            hBox = new HBox();
            hBox.setPadding(new Insets(5));
            progressBar = new ProgressBar();
            message = new Label();
            if(title != null) {
                hBox.getChildren().addAll(title);
            }
            hBox.getChildren().addAll(progressBar, message);
            //默认不显示
            setVisible(false);
            return hBox;
        }
    
        public MyProgressBar(String title) {
            this.title = new Label(title);
        }
    
        public MyProgressBar() {}
    
        /**
         * 计算进度条步长
         * @param stepTotal
         * @return
         */
        public void calculationStep(int stepTotal) {
            BigDecimal dividend = new BigDecimal("1");
            BigDecimal divisor = new BigDecimal(stepTotal);
            LOG.info("进度组件: 计算进度条步长 除数: {}", divisor);
            LOG.info("被除数: {}, 除数: {}, 结果: {}", dividend, divisor, dividend.divide(divisor, 2, BigDecimal.ROUND_UP).doubleValue());
            step = dividend.divide(divisor, 2, BigDecimal.ROUND_UP).doubleValue();
        }
    
        /**
         * 自动累加
         */
        public void autoAdd() {
            if(null == step) {
                new RuntimeException("未计算进度条step!");
            }
            total += step;
            LOG.info("进度条进度: {}", total);
            setValue(total);
        }
    
    }

    进度条调用:

    //计算进度条step
    batchProgressBar.calculationStep();
    
    //显示进度条并给初始值
    batchProgressBar.setVisible(true);
    batchProgressBar.setValue(0.0);
    batchProgressBar.setLabel("开始执行");
    
    //在后台代码执行时需要更新界面UI,用此方法                
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            batchProgressBar.autoAdd();
            batchProgressBar.setLabel("正在截取视频图片 " + Handler.getFileName(path));
        }
    });

    文件选择器:

    import javafx.stage.DirectoryChooser;
    import javafx.stage.FileChooser;
    import javafx.stage.FileChooser.ExtensionFilter;
    
    /**
     * @author xinhai.ma
     * @description  文件选择器
     * @date 2020/5/9 9:04
     */
    public class MyChooser {
    
        public static DirectoryChooser getDirectoryChooser() {
            DirectoryChooser directoryChooser = new DirectoryChooser();
            return directoryChooser;
        }
    
        /**
         * 功能描述 返回视频选择器
         * @author xinhai.ma
         * @date 2020/5/9 22:27
         * @return javafx.stage.FileChooser
         */
        public static FileChooser getFileChooser() {
            FileChooser fileChooser = new FileChooser();
            //在文件选择器做了格式限制
            fileChooser.getExtensionFilters().addAll(
                    new ExtensionFilter("MP4", "*.mp4"));
                    /*new ExtensionFilter("AVI", "*.avi"),*/
                    /*new ExtensionFilter("RM", "*.rm"),*/
                    /*new ExtensionFilter("WMV", ".wmv"),*/
                    /*new ExtensionFilter("FLV", ".flv"),*/
                    /*new ExtensionFilter("MPEG", ".mpeg")*/
            return fileChooser;
        }
    
        /**
         * 功能描述 返回exe选择器
         * @author xinhai.ma
         * @date 2020/5/9 22:27
         * @return javafx.stage.FileChooser
         */
        public static FileChooser getExeFileChooser() {
            FileChooser fileChooser = new FileChooser();
            //在文件选择器做了格式限制
            fileChooser.getExtensionFilters().addAll(
                    new ExtensionFilter("EXE", "*.exe"));
            return fileChooser;
        }
    
        /**
         * 功能描述 返回图片选择器
         * @author xinhai.ma
         * @date 2020/5/9 22:27
         * @return javafx.stage.FileChooser
         */
        public static FileChooser getImageChooser() {
            FileChooser fileChooser = new FileChooser();
            //在文件选择器做了格式限制
            fileChooser.getExtensionFilters().addAll(
                    new ExtensionFilter("JPEG", "*.jpg"),
                    new ExtensionFilter("PNG", "*.png"),
                    new ExtensionFilter("GIF", "*.gif"),
                    new ExtensionFilter("BMP", "*.bmp"));
            return fileChooser;
        }
    
    
        /**
         * 功能描述 返回所有文件选择器
         * @author xinhai.ma
         * @date 2020/5/9 22:27
         * @return javafx.stage.FileChooser
         */
        public static FileChooser getAllFileChooser() {
            FileChooser fileChooser = new FileChooser();
            return fileChooser;
        }
    
    }

    文件选择器调用:

    // 选择的文件夹
    File file = MyChooser.getDirectoryChooser().showDialog(primaryStage);

    弹窗提示组件:

    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.layout.HBox;
    import javafx.scene.layout.VBox;
    import javafx.stage.Modality;
    import javafx.stage.Stage;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import util.Handler;
    import java.util.List;
    
    /**
     * @author xinhai.ma
     * @description  弹窗提示组件
     * @date 2020/5/9 9:05
     */
    public class MyAlertBox {
    
        private static final Logger LOG = LoggerFactory.getLogger(MyAlertBox.class);
    
        public static void display(String title, String message) {
            LOG.info("弹窗提示: {}", message);
            Stage window = new Stage();
            window.setTitle(title);
            // modality要使用Modality.APPLICATION_MODEL
            window.initModality(Modality.APPLICATION_MODAL);
            window.setMinWidth(300);
            window.setMinHeight(150);
    
            Button button = new Button("知道了");
            button.setOnAction(e -> window.close());
    
            Label label = new Label(message);
    
            VBox layout = new VBox(10);
            layout.getChildren().addAll(label, button);
            layout.setAlignment(Pos.CENTER);
    
            Scene scene = new Scene(layout);
            window.setScene(scene);
            // 使用showAndWait()先处理这个窗口,而如果不处理,main中的那个窗口不能响应
            window.showAndWait();
        }
    
    }

    弹窗组件调用:

    MyAlertBox.display("提示", "处理视频数量大于100,请减少视频数量!");

    列表组件:

    import java.io.File;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import javafx.beans.value.ObservableValue;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.scene.control.Label;
    import javafx.scene.control.ListView;
    import javafx.scene.layout.Priority;
    import javafx.scene.layout.VBox;
    import javafx.scene.text.Font;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import util.Handler;
    
    /**
     * @author xinhai.ma
     * @description  列表组件
     * @date 2020/5/9 9:01
     */
    public class MyListView {
    
        private static final Logger LOG = LoggerFactory.getLogger(MyListView.class);
    
        /**
         * 列表视图
         */
        private ListView<String> listView = new ListView<>();
    
        /**
         * 显示选中项label
         */
        private final Label label = new Label();
    
        /**
         * 所有视频地址(不带省略号)
         */
        private List<String> filePathList = new ArrayList<>();
    
        /**
         * 所有简洁路径地址
         */
        private List<String> simplePathList = new ArrayList<>();
    
        /**
         * 当前播放的视频地址
         */
        private String currentVideoPath;
    
        public List<String> getFilePathList() {
            return filePathList;
        }
    
        /**
         * 获得当前正在播放的视频文件地址
         * @return
         */
        public String getCurrentVideoPath() {
            return currentVideoPath;
        }
    
        /**
         * 设置列表视图大小
         * @param width
         * @param height
         */
        public void setListVideoSize(double width, double height) {
            listView.setPrefSize(width, height);
        }
    
    
        /**
         * 传入数据集合获得列表视图
         */
        public VBox getListView() {
            VBox box = new VBox();
            box.getChildren().addAll(listView, label);
            VBox.setVgrow(listView, Priority.ALWAYS);
            label.setLayoutX(10);
            label.setLayoutY(115);
            label.setMaxWidth(200);
            label.setWrapText(true);
            label.setFont(Font.font("Verdana", 12));
            listView.setPrefWidth(120);
            listView.setMaxWidth(200);
            listView.setPrefHeight(280);
            ObservableList<String> data = FXCollections.observableArrayList(simplePathList);
            listView.setItems(data);
            listView.getSelectionModel().selectedItemProperty()
                    .addListener((ObservableValue<? extends String> ov, String old_val, String new_val) -> {
                        LOG.info("您点了第{}项,视频名称是{}", listView.getSelectionModel().getSelectedIndex(), listView.getSelectionModel().getSelectedItem());
                        currentVideoPath = filePathList.get(listView.getSelectionModel().getSelectedIndex());
                        label.setText(new_val);   
                    });
            return box;
        }
    
        public MyListView(List<String> dataList) {
            if(null == dataList || 0 == dataList.size()) {
                LOG.info("初始化filePathList,dataList为空,填入暂无数据");
                dataList = Arrays.asList("暂无数据");
                simplePathList.add("暂无数据");
                filePathList.addAll(dataList);
            } else {
                LOG.info("初始化filePathList,dataList不为空,填入dataList");
                filePathList.addAll(dataList);
                //使文件列表展示更加简洁
                LOG.info("初始化simplePathList");
                simplePathList = Handler.getSimplePathList(dataList);
            }
        }
    
    }

    列表组件调用:

    //构造方法传入要显示的内容List
    MyListView videoView = new MyListView(unProcessedList);
    //拿到列表视图对象
    videoView.getListView()

     更多Java Fx组件运用参考项目:https://github.com/mxh1997java/VideoProcess

     Java Fx官方文档:http://www.javafxchina.net/main/ 

  • 相关阅读:
    大三小学期 Android开发的一些经验
    高德地图显示不出来
    imageview无法显示图片:java.lang.RuntimeException: Canvas: trying to draw too large(281520000bytes) bitmap
    设置ImageView显示的图片铺满全屏
    大三小学期 web前端开发的一些小经验
    HTML5----响应式(自适应)网页设计
    div闪一下就消失
    登录界面输入判断为空的bug
    Solr之.net操作
    Java Web之路(一)Servlet
  • 原文地址:https://www.cnblogs.com/mxh-java/p/12774137.html
Copyright © 2011-2022 走看看