GUI编程--04--jFrame工具类

工具类

GUI编程--04--jFrame工具类_第1张图片

简单实现

pom

<dependencies>
        <dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>poiartifactId>
            <version>3.9version>
        dependency>
        <dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>poi-ooxmlartifactId>
            <version>3.9version>
        dependency>
        <dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>poi-ooxml-schemasartifactId>
            <version>3.9version>
        dependency>
        <dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>poi-scratchpadartifactId>
            <version>4.0.0version>
        dependency>
        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-collections4artifactId>
            <version>4.1version>
        dependency>
        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-lang3artifactId>
            <version>3.8.1version>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.13version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.junit.jupitergroupId>
            <artifactId>junit-jupiterartifactId>
            <version>RELEASEversion>
            <scope>compilescope>
        dependency>
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-apiartifactId>
            <version>1.7.26version>
        dependency>


    dependencies>

PageCreateUtil

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;

public class PageCreateUtil extends JFrame implements ActionListener, MouseListener {


    private String fileDirPath;

    private static volatile  PageCreateUtil pageCreateUtil;


    private static final Logger logger=LoggerFactory.getLogger(PageCreateUtil.class);


    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }




    JPanel jPanel1=new JPanel();  //浏览
    JPanel jPanel2=new JPanel();  //浏览
    JPanel jPanel3=new JPanel();

    JScrollPane jPanel4=new JScrollPane(); //日志

    JPanel jPanel5=new JPanel(); //按钮
    // JPanel jPanel6=new JPanel(); //变长文件选项

    JFileChooser fileChooser1= new JFileChooser();
    JFileChooser fileChooser2= new JFileChooser();
    JFileChooser fileChooser3= new JFileChooser();

    JButton dr1=new JButton("浏览");

    JButton dr3=new JButton("浏览");

    // JButton dc1=new JButton("导出Excel");
    JButton dc2=new JButton("对比Compare");

    JTextArea ta=new JTextArea();

    JTextField tx1=new JTextField(30);
    JTextField tx2=new JTextField(30);
    JTextField tx3=new JTextField(30);

    JFrame jFrame;
    JMenuBar jMenuBar= new JMenuBar();
    JMenu gengxin= new JMenu("设置");
    JMenu assint= new JMenu("帮助");
    JMenuItem doc= new JMenuItem("使用手册");
    JMenuItem gx= new JMenuItem("更新");


    private void createPage(String title){
        jFrame=new JFrame(title);
        jFrame.setSize(620,225);
        jFrame.setResizable(false);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setLayout(null);


        //模块一
        jPanel1.setBounds(0, 20, 550, 35);

        JLabel jLabel1 = new JLabel("旧 EXcel文件:");
//      JLabel jLabel2 = new JLabel("   EXcel文件:");
        JLabel jLabel3 = new JLabel("新 EXcel文件:");

        tx1.setBackground(Color.white);

        addTransferHandler(tx1);
        dr1.addActionListener(this);
        jPanel1.add(jLabel1);
        jPanel1.add(tx1);
        jPanel1.add(dr1);



        // //模块二
        // jPanel2.setBounds(0, 35, 550, 35);
        //
        // tx2.setBackground(Color.white);
        //
        // addTransferHandler(tx2);
        // dr2.addActionListener(this);
        // jPanel2.add(jLabel2);
        // jPanel2.add(tx2);
        // jPanel2.add(dr2);


        //模块三
        jPanel3.setBounds(0, 70, 550, 35);

        tx3.setBackground(Color.white);

        addTransferHandler(tx3);
        dr3.addActionListener(this);
        jPanel3.add(jLabel3);
        jPanel3.add(tx3);
        jPanel3.add(dr3);



        //模块5
        jPanel5.setBounds(350, 120, 250, 35);
        addTransferHandler(tx2);
        dc2.addActionListener(this);
        jPanel5.add(dc2);


        ta.setEnabled(false);
        ta.setLineWrap(true);
        ta.setDisabledTextColor(Color.black);
        Font f = new Font("宋体", 0, 15);
        ta.setFont(f);
        ta.setCaretPosition(ta.getText().length());
        ta.addMouseListener(this);
        jPanel4.setBounds(0,150,620,40);
        jPanel4.setBorder(null);
        jPanel4.setViewportView(ta);

        gengxin.add(gx);
        gengxin.setFont(f);
        gengxin.setBackground(Color.lightGray);
        gx.addActionListener(this);
        gx.setFont(f);
        doc.addActionListener(this);
        doc.setFont(f);
        assint.add(doc);
        assint.setFont(f);
        assint.setBackground(Color.lightGray);
        jMenuBar.add(gengxin);
        jMenuBar.add(assint);

        jFrame.setJMenuBar(jMenuBar);
        jFrame.add(jPanel1);
        // jFrame.add(jPanel2);
        jFrame.add(jPanel3);
        jFrame.add(jPanel4);
        jFrame.add(jPanel5);

        jFrame.setLocationRelativeTo(null);
        jFrame.setVisible(true);

    }

    private void addTransferHandler(final JTextField field) {

        field.setTransferHandler(new TransferHandler(){
            @Override
            public boolean importData(JComponent comp, Transferable t) {
                try {
                    Object o = t.getTransferData(DataFlavor.javaFileListFlavor);
                    String filepath=o.toString();
                    if(filepath.startsWith("[")){
                        filepath=filepath.substring(1);
                    }
                    if(filepath.endsWith("]")){
                        filepath=filepath.substring(0,filepath.length()-1);
                    }
                    field.setText(filepath);
                    return true;
                } catch (Exception e) {
                    logger.info("Data Load Failure!");
                    logger.info(e.getMessage());
                }
                return false;
            }

            @Override
            public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {

                for (int i = 0; i < transferFlavors.length; i++) {
                    if(DataFlavor.javaFileListFlavor.equals(transferFlavors[i])){
                        return true;
                    }
                }
                return false;
            }
        });
    }



    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==dr1){
            fileChooser1.setFileSelectionMode(0);  //0文件 1文件夹
            //打开文件浏览器,点击取消返回1
            int status = fileChooser1.showOpenDialog(null);
            if(status==1){
                return;
            }else {
                File file = fileChooser1.getSelectedFile();
                tx1.setText(file.getAbsolutePath());
            }
        }else if(e.getSource()==dr3) {
            fileChooser3.setFileSelectionMode(0);  //0文件 1文件夹
            //打开文件浏览器,点击取消返回1
            int status = fileChooser3.showOpenDialog(null);
            if (status == 1) {
                return;
            } else {
                File file = fileChooser3.getSelectedFile();
                tx3.setText(file.getAbsolutePath());
            }
        }else if(e.getSource()==dc2){  //对比逻辑
            printMsg("开始导出文件");
            String txval=tx1.getText();
            File copyFile=null;
            if(StringUtils.isNotBlank(txval)){
                copyFile=new File(txval.trim());
                if(!copyFile.exists()){
                    printMsg("文件不存在");
                    return;
                }else if(copyFile.length()==0){
                    printMsg("文件大小为0");
                    return;
                }
            }else {
                printMsg("未选择文件");
                return;
            }

            String tx3Val=tx3.getText();
            File excelFile=null;
            if(StringUtils.isNotBlank(tx3Val)){
                excelFile=new File(tx3Val);
                if(!excelFile.exists()){
                    printMsg("未选择文件");
                    return;
                }else if(excelFile.length()==0){
                    printMsg("文件大小为0");
                    return;
                }
            }else {
                printMsg("excelFile");
                return;
            }

            //导出文件逻辑
            try {
                boolean b=exportBin(copyFile,excelFile);
                printMsg("文件导出成功");
            } catch (Exception exception) {
             logger.info("文件导出失败: "+exception.getMessage());
                printMsg("文件导出失败");
            }

        }else if(e.getSource()==gx){ //文件更新
            updateVersion(false);

        }else if(e.getSource()==doc){
            String path=System.getProperty("user.dir")+File.separator+"document"+"\\"+"操作手册.doc";
            File file = new File(path);

            try {
                Desktop.getDesktop().open(file);
            } catch (IOException exception) {
                logger.info("操作手册打开失败: "+exception.getMessage());
                printMsg("操作手册打开失败");
            }
        }


    }

    private void updateVersion(boolean b) {
    }

    private boolean exportBin(File oldFile, File newFile) {
        //导出逻辑
        System.out.println("对比开始!");
        String oldPath=oldFile.getAbsolutePath();
        String newPath=newFile.getAbsolutePath();
        String dirPath=fileDirPath+File.separator+"test.xls";


        CompareUtil.parseExcel(oldPath,newPath,dirPath);

        File file = new File(dirPath);

        try {
            Desktop.getDesktop().open(file);
        } catch (IOException e) {
            logger.info("Excel打开失败: "+e.getMessage());
            e.printStackTrace();
            printMsg("Excel打开失败");
        }

        return true;
    }


    private void printMsg(String msg) {
        if(msg.length()>20){
            ta.setText(msg.substring(0,20)+"...");
        }else {
            ta.setText(msg);
        }

        ta.setCaretPosition(ta.getText().length());
        ta.isOptimizedDrawingEnabled();
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    if(e.getSource()==ta){
        String path=System.getProperty("user.dir")+File.separator+"document"+"\\"+"app.log";
        File file = new File(path);

        try {
            Desktop.getDesktop().open(file);
        } catch (IOException exception) {
            logger.info("日志文件读取失败: "+exception.getMessage());
            exception.printStackTrace();
            printMsg("日志文件读取失败");
        }
    }
    }


    private  static PageCreateUtil getInstance(){
        if(pageCreateUtil==null){
            synchronized (PageCreateUtil.class){
                if(pageCreateUtil==null){
                    pageCreateUtil=new PageCreateUtil();
                }
            }
        }
        return  pageCreateUtil;
    }

    public static void main(String[] args) {

        try {
            PageCreateUtil pageCreateUtil = getInstance();
            pageCreateUtil.fileDirPath=System.getProperty("user.dir")+File.separator+"tarFileDir";
            pageCreateUtil.createPage("EXcel_Compare v");
        } catch (Exception e) {
            logger.info(e.getMessage());
            e.printStackTrace();
        }

        System.out.println("--------启动成功--------");

    }

}

你可能感兴趣的:(前端,java)