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

📄 musicext.java

📁 一个Mp3在线搜索器
💻 JAVA
字号:
/* * MusicExt.java * * Created on 2008年4月7日, 上午1:53 */package biz.tbuy.huliqing.jloading.ext.mp3;import biz.tbuy.huliqing.jloading.JLoadingException;import biz.tbuy.huliqing.jloading.TaskManager;import biz.tbuy.huliqing.jloading.ext.Extension;import biz.tbuy.huliqing.jloading.mycell.ColumnCellRenderer;import biz.tbuy.huliqing.share.StringFilter;import java.awt.Color;import java.net.URLEncoder;import java.util.Enumeration;import java.util.List;import javax.swing.SwingUtilities;import javax.swing.table.TableColumn;import javax.swing.table.TableModel;/** * * @author  huliqing */public class MusicExt extends javax.swing.JPanel implements Extension{            /** Creates new form MusicExt */    public MusicExt() {        initComponents();        myInitComponents();    }        /** This method is called from within the constructor to     * initialize the form.     * WARNING: Do NOT modify this code. The content of this method is     * always regenerated by the Form Editor.     */    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents    private void initComponents() {        jTextField1 = new javax.swing.JTextField();        jButton1 = new javax.swing.JButton();        jScrollPane1 = new javax.swing.JScrollPane();        jTable1 = new javax.swing.JTable();        jButton2 = new javax.swing.JButton();        jProgressBar1 = new javax.swing.JProgressBar();        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(jloading.JLoadingApp.class).getContext().getResourceMap(MusicExt.class);        setBackground(resourceMap.getColor("Form.background")); // NOI18N        setName("Form"); // NOI18N        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N        jTextField1.setName("jTextField1"); // NOI18N        jTextField1.addMouseListener(new java.awt.event.MouseAdapter() {            public void mousePressed(java.awt.event.MouseEvent evt) {                jTextField1MousePressed(evt);            }        });        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {            public void keyPressed(java.awt.event.KeyEvent evt) {                jTextField1KeyPressed(evt);            }        });        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N        jButton1.setName("jButton1"); // NOI18N        jButton1.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                jButton1ActionPerformed(evt);            }        });        jScrollPane1.setName("jScrollPane1"); // NOI18N        jTable1.setModel(            new MusicTableModel()        );        jTable1.setName("jTable1"); // NOI18N        jTable1.setShowHorizontalLines(false);        jTable1.setShowVerticalLines(false);        jScrollPane1.setViewportView(jTable1);        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N        jButton2.setToolTipText(resourceMap.getString("jButton2.toolTipText")); // NOI18N        jButton2.setName("jButton2"); // NOI18N        jButton2.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                jButton2ActionPerformed(evt);            }        });        jProgressBar1.setName("jProgressBar1"); // NOI18N        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);        this.setLayout(layout);        layout.setHorizontalGroup(            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(layout.createSequentialGroup()                .addContainerGap()                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 533, Short.MAX_VALUE)                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)                        .addComponent(jButton1)                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                        .addComponent(jButton2)                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)                        .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)))                .addContainerGap())        );        layout.setVerticalGroup(            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(layout.createSequentialGroup()                .addContainerGap()                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)                        .addComponent(jButton1)                        .addComponent(jButton2))                    .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE)                .addContainerGap())        );    }// </editor-fold>//GEN-END:initComponents    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed        search();    }//GEN-LAST:event_jButton1ActionPerformed    private void jTextField1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextField1MousePressed        if (jTextField1.getText().equals("输入关键词")) {            jTextField1.setText("");        }    }//GEN-LAST:event_jTextField1MousePressed    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyPressed        if (evt.getKeyCode() == 10) {            search();        }    }//GEN-LAST:event_jTextField1KeyPressed    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed        try {            addTasks();//GEN-LAST:event_jButton2ActionPerformed        } catch (JLoadingException ex) {            System.out.println("添加任务时遇到错误");        }    }                                                    // Variables declaration - do not modify//GEN-BEGIN:variables    private javax.swing.JButton jButton1;    private javax.swing.JButton jButton2;    private javax.swing.JProgressBar jProgressBar1;    private javax.swing.JScrollPane jScrollPane1;    private javax.swing.JTable jTable1;    private javax.swing.JTextField jTextField1;    // End of variables declaration//GEN-END:variables    // ----------        private void myInitComponents() {        this.jScrollPane1.getViewport().setBackground(Color.WHITE);        this.jTable1.setRowHeight(22);                Enumeration<TableColumn> cols = this.jTable1.getColumnModel().getColumns();        while (cols.hasMoreElements()) {            cols.nextElement().setCellRenderer(new ColumnCellRenderer());        }                this.jProgressBar1.setVisible(false);    }        private void search() {        search = new Thread(new SearchMp3());        search.start();    }        /**     * 进行MP3搜索     */    private class SearchMp3 implements Runnable{        private int read;        //private int total;        Runnable rp1 = new Runnable() {                        public void run() {                jTable1.updateUI();            }                    };        Runnable rp_onSearch = new Runnable() {                        public void run() {                jProgressBar1.setVisible(true);                jProgressBar1.setIndeterminate(true);            }                    };        Runnable rp_onSearchOk = new Runnable() {            public void run() {                jProgressBar1.setVisible(false);                jProgressBar1.setIndeterminate(false);            }                    };        public void run() {            if (codeFilter == null) {                codeFilter = new CodeFilter();            }            SwingUtilities.invokeLater(rp_onSearch);            try {                String keyword = URLEncoder.encode(jTextField1.getText(), "GBK");                String uStr = "http://mp3.baidu.com/m?f=ms&tn=baidump3&ct=134217728&lf=&rn=&word=" + keyword + "&lm=-1";                String listPageCode = StringFilter.getHtmlCode(uStr);                String[] temp = listPageCode.split("链接速度[\r\n\t]*</th>[\r\n\t]*</tr>[\r\n\t]*<tr>");                if (temp.length >= 2) { // temp小于2则表示找不到数据                    temp = temp[1].split("</tr>[\r\n\t]*</table>");                    temp = temp[0].split("</tr><tr>");                    if (temp.length > 0 ) {                        //total = temp.length;                        MusicTableModel mtm = (MusicTableModel) jTable1.getModel();                        mtm.clear();                        for (String group : temp) {                            read++;                            MusicModel mm = codeFilter.filter(group);                            mtm.addValue(mm);                            SwingUtilities.invokeLater(rp1);                            //if (read >= 10) break;                        }                    }                }                SwingUtilities.invokeLater(rp_onSearchOk);            } catch (Exception e) {                SwingUtilities.invokeLater(rp_onSearchOk);                System.out.println("Exception e");            }        }    }        /**     * 添加任务     */    private void addTasks() throws JLoadingException {        TaskManager tManager = TaskManager.getInstance();        TableModel tableModel = jTable1.getModel();        int[] keys = jTable1.getSelectedRows();        if (tableModel instanceof MusicTableModel) {            MusicTableModel mtm = (MusicTableModel) tableModel;            List<MusicModel> musics = mtm.getValues();            for (int key : keys) {                MusicModel mm = musics.get(key);                tManager.addTask(mm.getName(), mm.getUrl());            }        }    }        private Thread search;    private CodeFilter codeFilter;}

⌨️ 快捷键说明

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