?? jdown.java
字號(hào):
/**
* 程序:JDown下載工具
*/
/**
* @author magic
* @since 2006.7.3
* @version 1.1
* @email maigcsongyang@163.com
*/
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.Vector;
import java.net.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
// 定義公共類
public class Jdown
{
// 定義main方法
public static void main(String[] args)
{
JdownFrame downFrame = new JdownFrame();
downFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
downFrame.setResizable(false);
downFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
downFrame.show();
}
}
// 框架的類
class JdownFrame extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
private final JdownPanel panel;
public JdownFrame()
{
// 得到屏幕尺寸
final Toolkit kit = Toolkit.getDefaultToolkit();
final Dimension screenSize = kit.getScreenSize();
final int screenHeight = screenSize.height;
final int screenWidth = screenSize.width;
// 微調(diào)窗口位置及大小并設(shè)置標(biāo)題
setTitle("JDown下載工具");
setSize(60 + screenWidth / 2, 10 + screenHeight / 3);
setLocation(screenWidth / 4, screenHeight / 4);
// 在容器中加入一個(gè)panel作為最大的面板
final Container contentPane = getContentPane();
panel = new JdownPanel();
contentPane.add(panel);
pack();
}
}
// 面板的類,計(jì)劃北部放Textpanel用于留言顯示,中間放一個(gè)panel里面再設(shè)置姓名與留言內(nèi)容,南部放一個(gè)單選框設(shè)置背景色
class JdownPanel extends JPanel
{
/**
*
*/
private static final long serialVersionUID = 1L;
public JdownPanel()
{
try
{
// 設(shè)置為邊界布局
setLayout(new BorderLayout());
// 初始化一類變量
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(".."));// 設(shè)置當(dāng)前目錄
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
filesavepath = "C:\\";
// 北部的panel面板
JPanel panela = new JPanel();
add(panela, BorderLayout.NORTH);
panela.setLayout(new FlowLayout());
JButton newbutton = new JButton("新建下載地址");
JButton clrbutton = new JButton("清除所有記錄");
JButton setbutton = new JButton("設(shè)置");
JButton aboutbutton = new JButton("關(guān)于");
panela.add(newbutton);
panela.add(clrbutton);
panela.add(setbutton);
panela.add(aboutbutton);
// 中間部分
JPanel panelb = new JPanel();// panela中放name域和message域
add(panelb, BorderLayout.CENTER);
model = new Table_Model(20);
table = new JTable(model);
s_pan = new JScrollPane(table);
panelb.add(s_pan, BorderLayout.CENTER);
// 為按鈕添加監(jiān)聽器部分
newbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
showInput(); // 顯示輸入窗口
}
});
clrbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
removeData();
}
});
setbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
savePath();
}
});
aboutbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,
"本程序由JDOWN小組開發(fā)完成\n @copyright by JDown"
+ "2006-07-02");
}
});
}
catch (Exception exc)
{
;
}
}
private void showInput()
{
InputJFrame inputFrame = new InputJFrame(this);
inputFrame.show();
}
private void savePath()
{
// 顯示保存文件路徑選擇對(duì)話框
int r;
r = chooser.showSaveDialog(null);
if (r == JFileChooser.APPROVE_OPTION)
filesavepath = chooser.getSelectedFile().getPath();
JOptionPane.showMessageDialog(null, "路徑設(shè)置成功: " + filesavepath);
}
public void downLoad()
{
URL myUrl;
try
{
myUrl = new URL(urlStr);
String subStr = myUrl.getFile();
// JOptionPane.showMessageDialog(null, subStr);
File testFile = new File(subStr);
String fileName = testFile.getName();
HttpDown hd = new HttpDown(urlStr, filesavepath + "\\" + fileName);
addData(hd.getFileLength());
}
catch (MalformedURLException exe)
{
// TODO 自動(dòng)生成 catch 塊
exe.printStackTrace();
}
}
public void downLoad(int num)
{
URL myUrl;
try
{
myUrl = new URL(urlStr);
String subStr = myUrl.getFile();
// JOptionPane.showMessageDialog(null, subStr);
File testFile = new File(subStr);
String fileName = testFile.getName();
HttpDown hd = new HttpDown(urlStr, filesavepath + "\\" + fileName,
num);
addData(hd.getFileLength());
}
catch (MalformedURLException exe)
{
// TODO 自動(dòng)生成 catch 塊
exe.printStackTrace();
}
}
public void setUrl(String url)
{
urlStr = url;
}
public void setThread(int num)
{
threadNumber = num;
}
private void addData(long fileLength)
{
model.addRow(urlStr, new Integer(threadNumber), new Long(
fileLength / 1024));
table.updateUI(); // 刷新,將改變后的內(nèi)容顯示出來
}
private void removeData()
{
model.removeRows(0, model.getRowCount());
table.updateUI(); // 刷新,將改變后的內(nèi)容顯示出來
}
// 私有變量部分
private JTable table;
private Table_Model model;
private JScrollPane s_pan;
// 設(shè)定線程數(shù)和URL地址
private int threadNumber;
private String urlStr;
// 下載保存路徑設(shè)置所需的變量
private String filesavepath;
private JFileChooser chooser;
}
class Table_Model extends AbstractTableModel
{
private static final long serialVersionUID = -3094977414157589758L;
private Vector content = null;
private String[] title_name =
{ "序號(hào)", "URL地址", "線程數(shù)", "文件大小(K)" };
public Table_Model()
{
content = new Vector();
}
public Table_Model(int count)
{
content = new Vector(count);
}
public void addRow(String filename, Integer threadnumber, Long speed)
{
Vector v = new Vector(4);
v.add(0, new Integer(content.size()));
v.add(1, filename);
v.add(2, threadnumber);
v.add(3, speed);
content.add(v);
}
public void removeRow(int row)
{
content.remove(row);
}
public void removeRows(int row, int count)
{
for (int i = 0; i < count; i++)
{
if (content.size() > row)
{
content.remove(row);
}
}
}
/**
* 顯示列名
*/
public String getColumnName(int col)
{
return title_name[col];
}
/**
* 覆蓋抽象類里的方法
*/
public int getColumnCount()
{
return title_name.length;
}
public int getRowCount()
{
return content.size();
}
public Object getValueAt(int row, int col)
{
return ((Vector) content.get(row)).get(col);
}
}
class InputJFrame extends JFrame
{
/**
* 顯示輸入下載地址以及線程數(shù)的窗體
*/
private static final long serialVersionUID = 1L;
private JdownPanel jd;
private JTextField text1;
private JTextField text2;
public InputJFrame(JdownPanel j)
{
this.jd = j;
// 得到屏幕尺寸
final Toolkit kit = Toolkit.getDefaultToolkit();
final Dimension screenSize = kit.getScreenSize();
final int screenHeight = screenSize.height;
final int screenWidth = screenSize.width;
// 微調(diào)窗口位置及大小并設(shè)置標(biāo)題
setTitle("輸入框");
setSize(120 + screenWidth / 4, 50 + screenHeight / 5);
setLocation(screenWidth / 4, screenHeight / 4);
setResizable(false);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
dispose();
}
});
// 在容器中加入一個(gè)panel作為最大的面板
final Container contentPane = getContentPane();
JPanel panel = new JPanel();
contentPane.add(panel);
JPanel panela = new JPanel();
JPanel panelb = new JPanel();
JPanel panelc = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(panela, BorderLayout.NORTH);
panel.add(panelb, BorderLayout.CENTER);
panel.add(panelc, BorderLayout.SOUTH);
panela.setLayout(new FlowLayout());
panelb.setLayout(new FlowLayout());
panelc.setLayout(new FlowLayout());
// Label、TextField、Button
JLabel label1 = new JLabel("請(qǐng)輸入U(xiǎn)RL地址:");
JLabel label2 = new JLabel("請(qǐng)指定線程個(gè)數(shù):");
text1 = new JTextField("", 22);
text2 = new JTextField("", 22);
// 給text2的文本框加只能輸入數(shù)字的限制
text2.setDocument(new OnlyDigit(text2));
JButton okButton = new JButton("確定");
JButton resetButton = new JButton("重置");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String url = "";
String thread = "";
int tnum = 0;
try
{
url = text1.getText().trim();
jd.setUrl(url);
thread = text2.getText().trim();
// 當(dāng)用戶沒有填寫下載線程數(shù)時(shí)用默認(rèn)的線程數(shù)3
if (!url.equals("")) // 當(dāng) url 的地址有填寫時(shí) 才調(diào)用下載
{
if (thread.equals(""))
{
jd.setThread(3);
setVisible(false);
jd.downLoad();
}
else
{
// 當(dāng)用戶輸入的線程數(shù)太大的話 可能導(dǎo)致
// 在 字符串轉(zhuǎn)換成整型數(shù)時(shí)出錯(cuò)所以加了次限制保證在轉(zhuǎn)換時(shí)不出錯(cuò)
if (thread.length() < 3)
tnum = Integer.parseInt(thread);
else
tnum = 99;
jd.setThread(tnum);
setVisible(false);
jd.downLoad(tnum);
}
}
}
catch (Exception exe)
{
JOptionPane.showMessageDialog(null, "您的輸入有誤!");
text1.setText("");
text2.setText("");
}
dispose();
}
});
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
text1.setText("");
text2.setText("");
}
});
panela.add(label1);
panela.add(text1);
panelb.add(label2);
panelb.add(text2);
panelc.add(okButton);
panelc.add(resetButton);
pack();
}
private class OnlyDigit extends PlainDocument
{
/**
* JTextField 輸入框只能輸入接收數(shù)字的字符
*/
private static final long serialVersionUID = 1L;
private JTextField jtf;
public OnlyDigit(JTextField f)
{
this.jtf = f;
}
public void insertString(int offset, String str, AttributeSet attSet)
throws BadLocationException
{
StringBuffer tmp = new StringBuffer(jtf.getText());
tmp.insert(offset, str);
Pattern p = Pattern.compile("^\\d*$");// 正則表達(dá)式,這樣就只能輸入整整數(shù)了
Matcher m = p.matcher(tmp.toString());
if (m.find())
{
super.insertString(offset, str, attSet);
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -