⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainframe.java~9~

📁 用java编写的压缩程序
💻 JAVA~9~
字号:
package compress;

import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.UIManager;
import java.awt.Dimension;
import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
import java.awt.Color;
import java.awt.*;
import java.io.File;

public class MainFrame extends JFrame implements ActionListener {
    JButton jButton1, jButton2, jButton3, jButton4, jButton5;
    JTextField jTextField1 = new JTextField();
    WindowsLookAndFeel lookandFeel;
    private FileChooser fileChooser;
    protected DeCompress decompress;
    protected Compress compress;

    boolean isCompress = false;
    String filename = "";

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                MainFrame thisClass = new MainFrame();
                thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                thisClass.setVisible(true);
            }
        });
    }


    public MainFrame() {
        super();
        initialize();
    }
    private void initialize() {//初始化界面
        getContentPane().setLayout(null);
        setTitle("文本文件压缩器");
        setSize(572, 276);
        Toolkit tool;
        setLocation(350, 200);
        ImageIcon icon;
        icon = new ImageIcon("image.gif");
        tool = getToolkit();
        JLabel label = new JLabel(icon);
        this.getLayeredPane().add(label);
        label.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());
        ((JPanel) getContentPane()).setOpaque(false);
        this.setUndecorated(true);
        jTextField1.setText("");
        jTextField1.setBounds(new Rectangle(27, 112, 238, 30));
        this.getContentPane().add(jTextField1);
        jButton1 = new JButton(new ImageIcon("yasuo.gif"));
        jButton1.setBounds(new Rectangle(288, 91, 83, 61));
        this.getContentPane().add(jButton1);
        jButton1.addActionListener(this);
        jButton2 = new JButton(new ImageIcon("jieya.gif"));
        jButton2.setBounds(new Rectangle(378, 91, 87, 61));
        this.getContentPane().add(jButton2);
        jButton2.addActionListener(this);
        jButton3 = new JButton(new ImageIcon("concel.gif"));
        jButton3.setBounds(new Rectangle(474, 91, 87, 61));
        this.getContentPane().add(jButton3);

        jButton3.addActionListener(this);
        jButton4 = new JButton(new ImageIcon("guanbi.gif"));
        jButton4.setBounds(new Rectangle(508, 2, 62, 38));
        this.getContentPane().add(jButton4);
        jButton4.addActionListener(this);
        jButton5 = new JButton(new ImageIcon("liulan.gif"));
        jButton5.setBounds(new Rectangle(210, 63, 60, 30));
        this.getContentPane().add(jButton5);
        jButton5.addActionListener(this);

        // 初始化算法主类
        this.decompress = new DeCompress();
        this.compress = new Compress();
        // set look and feel
        this.lookandFeel = new WindowsLookAndFeel();
        try {

            UIManager.setLookAndFeel(this.lookandFeel);

        } catch (Exception exc) {
            System.err.println("Error loading L&F: " + exc);
        }

        this.fileChooser = new FileChooser();

        double width = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        double height = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        this.setLocation((int) (width - this.getWidth()) / 2,
                         (int) (height - this.getHeight()) / 2);

        this.setTitle("Compress V-1.0");
    }

    public void actionPerformed(ActionEvent e) {//按钮事件监听器
        if (e.getSource() == jButton1) {//压缩
            if (filename.endsWith(".txt")) {

                this.isCompress = true;
                compress.setFile(this.filename,
                                 this.filename.replaceAll(".txt", ".dat"));
                compress.compress(compress.codeFile, compress.textFile);

            }

        }
        if (e.getSource() == jButton2) {//解压
            if (filename.endsWith(".dat")) {
                this.isCompress = false;
                this.decompress.setFile(this.filename, this.filename
                                        .replaceAll(".dat", ".txtx"));

                this.decompress.run();
            }

        }
        if (e.getSource() == jButton3) {

        }
        if (e.getSource() == jButton4) {
            System.exit(0);
        }
        if (e.getSource() == jButton5) {
            // 打开文件的事件
            int result = this.fileChooser.showOpenDialog(this);
            if (result == this.fileChooser.APPROVE_OPTION) {
                filename = this.fileChooser.getSelectedFile().getAbsolutePath(); // 获得文件名

                jTextField1.setText(filename);

            }

        }

//        if (e.getSource().equals(this.mainPanel.jBOpen)) {
//            // 打开文件的事件
//            int result = this.fileChooser.showOpenDialog(this);
//            if (result == this.fileChooser.APPROVE_OPTION) {
//                filename = this.fileChooser.getSelectedFile().getAbsolutePath(); // 获得文件名
//                System.out.println(filename);
//                this.mainPanel.jTF.setText(filename);
//            }
//        }
//        if (e.getSource().equals(this.mainPanel.jBStart)) {
//            //this.filename=this.mainPanel.jTF.getText().trim();
//            if (!filename.equals("")) {
//                // progress.start();//启动线成
//                if (filename.endsWith(".dat")) {
//                    this.isCompress = false;
//                    this.decompress.setFile(this.filename, this.filename
//                                            .replaceAll(".dat", ".txtx"));
//                    progress.start(); // 启动线成
//                    this.decompress.start();
//                } else if (filename.endsWith(".txt")) {
//
//                    this.isCompress = true;
//                    compress.setFile(this.filename,
//                                     this.filename.replaceAll(".txt", ".dat"));
//                    compress.start();
//                    progress.start();
//                } else {
//                    System.out.println("Cannot solve this file of this kind");
//                }
//            }
//        }
//
    } // @jve:decl-index=0:visual-constraint="10,10"

//    class Progress {//extends Thread {//下面的进度条(由于线程有问题影响程序运行所以暂没有添加)
//        JProgressBar jPB ;
//        public Progress(){
//             jPB = new JProgressBar();
//        }

//        public void run() {
//            try {
//                System.out.println("线程已被启动");
//                if (!isCompress) { // 解压缩过程
//                    while (jPB.getValue() < jPB
//                           .getMaximum()) {
//                        Thread.sleep(100); // 线程休眠
//
//                        //System.out
//                        //		.println((int) (100 * decompress.currentlength / decompress.filelength));
//                       jPB
//                                .setValue((int) (100 * decompress.currentlength /
//                                                 decompress.filelength)); // 设置进度条值
//                    }
//                } else { // 压缩过程
//                    while (jPB.getValue() <jPB
//                           .getMaximum()) {
//                        Thread.sleep(100); // 线程休眠
//                        //System.out
//                        //		.println((int) (100 * compress.currentLength / compress.fileLength));
//                        jPB
//                                .setValue((int) (100 * compress.currentLength /
//                                                 compress.fileLength)); // 设置进度条值
//
//                    }
//
//                }
//            } catch (Exception ex) {
//                ex.printStackTrace();
//            }
//
//        }
    //}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -