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

📄 mainframe.java~5~

📁 用java编写的压缩程序
💻 JAVA~5~
字号:
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();





   // private static final long serialVersionUID = 1L;
    WindowsLookAndFeel lookandFeel;
   // private MainPanel mainPanel;
    private FileChooser fileChooser;
    protected DeCompress decompress;
    protected Compress compress;
    protected Progress progress=new Progress() ;
    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);
//        //jButton5.setBounds(new Rectangle(492, 2, 69, 32));
//        //jButton5.setText("jButton5");
     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);
        //this.getContentPane().add(jButton5);
        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);
        //progress.jPB =new JProgressBar() ;

        progress.jPB .setBounds(new Rectangle(32,210,495,26) );
       // progress.jPB .setBackground(Color.BLACK );
        label .add(progress.jPB );

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

            // System.out.println(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception exc) {
            System.err.println("Error loading L&F: " + exc);
        }
        // 初始化界面类
       // this.mainPanel = new MainPanel();
        // 初始化线成类
       // this.progress = new Progress();
        // 添加事件监听
       // this.mainPanel.jBCancel.addActionListener(this);
       // this.mainPanel.jBOpen.addActionListener(this);
        //this.mainPanel.jBStart.addActionListener(this);
       // this.mainPanel.jTF.addActionListener(this);

        this.fileChooser = new FileChooser();

        //this.setSize(new Dimension(407, 234));

        // set position
        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.setContentPane(mainPanel);
        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);
                    //progress.start();
                }


        }
        if (e.getSource() == jButton2){
            if (filename.endsWith(".dat")) {
                    this.isCompress = false;
                    this.decompress.setFile(this.filename, this.filename
                                            .replaceAll(".dat", ".txtx"));
                    //progress.start(); // 启动线成
                    //this.decompress.start();
                }

        }
        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(); // 获得文件名
               // System.out.println(filename);
                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 + -