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

📄 mainpanel.java

📁 相似度匹配程序。可以用来匹配任何你想得到的信息格式
💻 JAVA
字号:
////  MainPanel.java//  Simphile////  Created by Brian Risk on Thu May 09 2002.//  Copyright (c) 2002 Geneffects. All rights reserved.//import java.io.*;import java.util.*;import java.net.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;public class MainPanel extends JFrame implements ActionListener{        //Step 1    JButton chooseFile;    JLabel fileName;    File mainFile = null;    //step 2    JList list;    JScrollPane scroll;    JButton addFiles;    JButton removeFiles;    File otherFile = null;    Vector comparisonFiles = new Vector();    //step 3    JButton go;    JProgressBar jpb;    public MainPanel() {        //step 1        chooseFile = new JButton("Choose file");        chooseFile.addActionListener(this);        fileName = new JLabel();        //step 2        list = new JList();        scroll = new JScrollPane(list);        addFiles =  new JButton("Add file(s)");        addFiles.addActionListener(this);        removeFiles =  new JButton("Remove selected file(s)");        removeFiles.addActionListener(this);        //step 3        go =  new JButton("Go!");        jpb = new JProgressBar();        go.addActionListener(this);                Container majorPane = getContentPane();        majorPane.setLayout(new BoxLayout(majorPane, BoxLayout.Y_AXIS));                JPanel titlePanel = new JPanel();        titlePanel.setMaximumSize(new Dimension(300,40));        URL imageURL = ClassLoader.getSystemResource("simphile_title.gif");        Image splashImage = Toolkit.getDefaultToolkit().getImage(imageURL);        JLabel label = new JLabel(new ImageIcon(splashImage));        label.setAlignmentX(Component.CENTER_ALIGNMENT);        titlePanel.add(label);                majorPane.add(titlePanel);        majorPane.add(step1());        majorPane.add(step2());        majorPane.add(step3());        pack();    }        public void actionPerformed(ActionEvent ex) {        if (ex.getSource().equals(chooseFile)) {chooseFile();}        if (ex.getSource().equals(addFiles)) {addFiles();}        if (ex.getSource().equals(removeFiles)) {removeFiles();}        if (ex.getSource().equals(go)) {go();}    }        private JPanel step1() {        JPanel jp = new JPanel();        jp.setMaximumSize(new Dimension(300,60));        jp.add(chooseFile);        jp.add(new JLabel("File:  "));        jp.add(fileName);        jp.setBorder(new TitledBorder("Step 1"));        return jp;    }         private JPanel step2() {        JPanel jp = new JPanel();        jp.setLayout(new BoxLayout(jp, BoxLayout.Y_AXIS));        jp.add(scroll);        JPanel bottomStep2 = new JPanel();        bottomStep2.add(addFiles);        bottomStep2.add(removeFiles);        bottomStep2.setAlignmentX(Component.CENTER_ALIGNMENT);        jp.add(bottomStep2);        jp.setBorder(new TitledBorder("Step 2"));        return jp;    }        private JPanel step3() {        JPanel jp = new JPanel();        jp.setLayout(new BoxLayout(jp, BoxLayout.Y_AXIS));        go.setAlignmentX(Component.CENTER_ALIGNMENT);        jp.add(go);        jp.add(jpb);        jp.setBorder(new TitledBorder("Step 3"));        return jp;    }        private void chooseFile() {        if (mainFile == null) {mainFile = otherFile;}        JFileChooser chooser = new JFileChooser(mainFile);        chooser.setAcceptAllFileFilterUsed(true);        chooser.setDialogTitle("Choose a file");        chooser.setMultiSelectionEnabled(false);        int option = chooser.showOpenDialog(this);        if (option == JFileChooser.APPROVE_OPTION) {            mainFile = chooser.getSelectedFile();            fileName.setText(mainFile.getName());        }            }        private void addFiles() {        if (otherFile == null) {otherFile = mainFile;}        JFileChooser chooser = new JFileChooser(otherFile);        chooser.setAcceptAllFileFilterUsed(true);        chooser.setDialogTitle("Select file(s)");        chooser.setMultiSelectionEnabled(true);        int option = chooser.showOpenDialog(this);        if (option == JFileChooser.APPROVE_OPTION) {            File [] newFiles = chooser.getSelectedFiles();            otherFile = newFiles[0];            for (int i = 0; i < newFiles.length; i++) {                comparisonFiles.add(new Candidate(newFiles[i]));            }            list.setListData(comparisonFiles);        }    }        private void removeFiles() {        int [] indices = list.getSelectedIndices();        for (int i = 0; i < indices.length; i++) {            comparisonFiles.removeElementAt(indices[indices.length - i - 1]);        }        list.setListData(comparisonFiles);    }        private void go() {        Thread appThread = new Thread() {            public void run() {                if ((mainFile == null) || (comparisonFiles.size() == 0)) {return;}                long mainLength = ZipStuff.GZip(mainFile);                for (int i = 0; i < comparisonFiles.size(); i++) {                    final int selectIndex = i;                    try {                        EventQueue.invokeAndWait( new Runnable() {public void run() {                            list.setSelectedIndex(selectIndex);                            JScrollBar jsb = scroll.getVerticalScrollBar();                            int lvi = list.getLastVisibleIndex();                            if (lvi < selectIndex) {                                int block = jsb.getBlockIncrement(1);                                int presentValue = jsb.getValue();                                jsb.setValue(presentValue + block);                            }                        }});                        Thread.sleep(100);                    } catch (Exception e) {}                    Candidate presentCandidate = (Candidate) comparisonFiles.get(i);                    File subFile = presentCandidate.getFile();                    long subLength = ZipStuff.GZip(subFile);                    long compressedLength = ZipStuff.GZip(subFile, mainFile);                    double compressionRate = ((double) (mainLength + subLength)) / ((double) compressedLength);                    presentCandidate.setScore(compressionRate);                    final int percentDone = (int) ((100.0) * (((double) i) / ((double) comparisonFiles.size())));                    try {                        EventQueue.invokeAndWait( new Runnable() {public void run() {                            jpb.setValue(percentDone);                        }});                        Thread.sleep(100);                    } catch (Exception e) {}                }                Collections.sort(comparisonFiles, new Candidate());                list.setListData(comparisonFiles);                scroll.getVerticalScrollBar().setValue(0);                jpb.setValue(0);              }        };        appThread.start();    }    }

⌨️ 快捷键说明

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