📄 addtaskdlg.java
字号:
package com.leapget.view;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class AddTaskDlg extends JDialog {
private static final long serialVersionUID = 20080807185430L;
private static AddTaskDlg addTaskDlg;
private JPanel jpMain;
private JPanel jpFileUrl;
private JPanel jpSavePath;
private JPanel jpThreadCount;
private JPanel jpStartDownload;
private JComboBox jcbThreadCount;
private JTextField jtfFileUrl;
private JTextField jtfSavePath;
private JButton jbtnView;
private JButton jbtnStartDownload;
private JFileChooser jfcSavePath;
private String fileUrl,savePath;
private int threadCount;
private boolean canceled;
public AddTaskDlg(JFrame parent, boolean modal){
super(parent,"添加下载任务",modal);
initComponent();
}
public boolean isCanceled(){
return canceled;
}
public String getFileUrl(){
return fileUrl;
}
public String getSavePath(){
return savePath;
}
public int getThreadCount(){
return threadCount;
}
// 初始化界面
private void initComponent(){
// 文件地址
jpFileUrl = new JPanel(new FlowLayout(FlowLayout.LEFT));
jpFileUrl.add(new JLabel("地址:"));
jtfFileUrl = new JTextField(30);
jtfFileUrl.setText("http://www.newskynet.com/HwNetView.zip");
jtfFileUrl.addFocusListener(new FocusAdapter(){
@Override
public void focusLost(FocusEvent evt) {
jtfFileUrlFocusLost(evt);
}
});
jpFileUrl.add(jtfFileUrl);
// 保存路径
jpSavePath = new JPanel(new FlowLayout(FlowLayout.LEFT));
jpSavePath.add(new JLabel("路径:"));
jtfSavePath = new JTextField(23);
jpSavePath.add(jtfSavePath);
jbtnView = new JButton("浏览...");
jbtnView.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent evt){
jbtnViewMouseClicked(evt);
}
});
jpSavePath.add(jbtnView);
// 线程数量
jpThreadCount = new JPanel(new FlowLayout(FlowLayout.LEFT));
jpThreadCount.add(new JLabel("线程数量:"));
jcbThreadCount = new JComboBox(new Integer[]{1,2,3,4,5,10,15,20,25,30,35,40,45,50});
jcbThreadCount.setEditable(true);
jpThreadCount.add(jcbThreadCount);
// 开始下载
jpStartDownload = new JPanel();
jbtnStartDownload = new JButton("开始下载");
jbtnStartDownload.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent evt){
jbtnStartDownloadMouseClicked(evt);
}
});
jpStartDownload.add(jbtnStartDownload);
// 依次添加各个Panel
jpMain = new JPanel();
jpMain.setLayout(new GridLayout(0,1));
jpMain.add(jpFileUrl);
jpMain.add(jpSavePath);
jpMain.add(jpThreadCount);
jpMain.add(jpStartDownload);
jpMain.add(jpStartDownload);
JPanel jpPadding = new JPanel(); // 设置四边与窗口的边距
jpPadding.setLayout(new FlowLayout(FlowLayout.CENTER,5,15));
jpPadding.add(jpMain);
setResizable(false);
setVisible(false);
getContentPane().add(jpPadding);
setBounds(320, 250, 450, 215);
this.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent evt) {
closeAddTaskDlg();
canceled = true;
}
});
}
// 关闭当前窗口
public void closeAddTaskDlg(){
jtfFileUrl.setText("http://");
jtfSavePath.setText("");
jcbThreadCount.setSelectedIndex(0);
jtfFileUrl.setFocusable(true);
this.setVisible(false);
}
// 处理下载地址输入框失去焦点
public void jtfFileUrlFocusLost(FocusEvent evt){
if(jtfSavePath.getText().trim().length()==0){
String fileUrl = jtfFileUrl.getText();
int head=fileUrl.lastIndexOf('/') + 1;
jtfSavePath.setText(fileUrl.substring(head));//获得要下载的文件的文件名
}
}
// 点击浏览按钮选择保存路径
public void jbtnViewMouseClicked(MouseEvent evt){
jfcSavePath = new JFileChooser();
jfcSavePath.showSaveDialog(this);
if(jfcSavePath.getSelectedFile() != null){
jtfSavePath.setText(jfcSavePath.getSelectedFile().getPath());
}
}
// 验证Url正确性
private boolean validUrl(String url) {
boolean result = true;
if("".equals(url)){
JOptionPane.showMessageDialog(null, "文件路径不能为空!", "错误", JOptionPane.ERROR_MESSAGE);
result = false;
}else if (!url.toLowerCase().startsWith("http://")
|| url.substring(url.lastIndexOf("/")+1).length() < 1){
JOptionPane.showMessageDialog(null, "文件 " + url + " 不是有效的路径!", "错误", JOptionPane.ERROR_MESSAGE);
jtfFileUrl.setSelectionStart(0);
jtfFileUrl.setSelectionEnd(jtfFileUrl.getText().length());
result = false;
}
if(!result) jtfFileUrl.setFocusable(true);
return result;
}
// 验证文件是否已经存在
private boolean validSave(String save){
Pattern p=Pattern.compile("[<,>,\\,\\?,\\\\,\\*,\\/:]+"); //正则表达式
Matcher m=p.matcher(save); //操作的字符串
File f = new File(save);
boolean result = true;
if("".equals(save)){
JOptionPane.showMessageDialog(null, "文件名不能为空!", "错误", JOptionPane.ERROR_MESSAGE);
result = false;
}else if(m.find()){
JOptionPane.showMessageDialog(null, "文件名不合法,不能包含 <>\\/*?: 中的任一字符!", "错误", JOptionPane.ERROR_MESSAGE);
result = false;
}else if(f.exists()){
if(JOptionPane.showConfirmDialog(null, "文件 " + save + " 已经存在,是否覆盖?", "提示",
JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE) == JOptionPane.YES_OPTION)
result = true;
else{
jtfSavePath.setSelectionStart(0);
jtfSavePath.setSelectionEnd(jtfSavePath.getText().length()-1);
result = false;
}
}
if(!result) jtfSavePath.setFocusable(true);
return result;
}
// 点击开始下载按钮
public void jbtnStartDownloadMouseClicked(MouseEvent evt){
fileUrl = jtfFileUrl.getText().trim();
savePath = jtfSavePath.getText().trim();
threadCount = ((Integer)jcbThreadCount.getSelectedItem()).intValue();
/*if(validUrl(url) && validSave(save)){
MainAppFrame parent = (MainAppFrame)this.getParent();
parent.addTask(url, save, ((Integer)jcbThreadCount.getSelectedItem()).intValue());
closeAddTaskDlg(); // 关闭当前对话框
}*/
if(validUrl(fileUrl) && validSave(savePath)){
closeAddTaskDlg(); // 关闭当前对话框
canceled = false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -