📄 downloadsetting.java
字号:
/*DownloadSetting.java*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.net.*;
class DownloadSetting extends JDialog implements ActionListener,
WindowListener
{
//定义私有成员对象
private JButton okButton=null;
private JButton cancelButton=null;
private JButton searchButton=null;
private JLabel urlLabel=null;
private JLabel saveLabel=null;
private JLabel priorityLabel=null;
private JLabel threadsLabel=null;
private JTextField urlText=null;
private JTextField saveText=null;
private JComboBox priorityComboBox=null;
private JComboBox threadsComboBox=null;
private Vector dataVector=null;
//构造函数
public DownloadSetting(JFrame parent)
{
super(parent,"无限下载任务设置",true);
Container pane =getContentPane(); //取得对话框的内部容器
pane.setLayout(null); //屏蔽掉默认的版面管理器
//建立标签
urlLabel=new JLabel("网络路径",JLabel.CENTER);
saveLabel=new JLabel("保存为",JLabel.CENTER);
priorityLabel=new JLabel("优先权",JLabel.CENTER);
threadsLabel=new JLabel("分片数",JLabel.CENTER);
//建立命令按钮
okButton= new JButton("确定(O)");
okButton.setMnemonic('O');
cancelButton= new JButton("取消(C)");
cancelButton.setMnemonic('C');
searchButton=new JButton(new ImageIcon("save.jpg"));
okButton.addActionListener(this);
cancelButton.addActionListener(this);
searchButton.addActionListener(this);
//建立输入文本框
urlText= new JTextField(20);
saveText=new JTextField(20);
//建立复合文本框并初始化数据项
Vector number=new Vector(10);
for (int i=0;i<10;i++)
number.addElement(new Integer(i+1).toString());
priorityComboBox=new JComboBox(number);
threadsComboBox=new JComboBox(number);
priorityComboBox.setSelectedIndex(4);
threadsComboBox.setSelectedIndex(4);
//将各个成员对象添加到容器中
pane.add(urlLabel);
pane.add(saveLabel);
pane.add(priorityLabel);
pane.add(threadsLabel);
pane.add(urlText);
pane.add(saveText);
pane.add(priorityComboBox);
pane.add(threadsComboBox);
pane.add(okButton);
pane.add(cancelButton);
pane.add(searchButton);
//设置各个成员对象的边界位置和外观大小
urlLabel.setBounds(5,5,60,30);
saveLabel.setBounds(5,40,60,30);
priorityLabel.setBounds(5,80,60,30);
threadsLabel.setBounds(200,80,60,30);
urlText.setBounds(70,10,300,30);
saveText.setBounds(70,50,265,30);
priorityComboBox.setBounds(70,85,100,30);
threadsComboBox.setBounds(270,85,100,30);
searchButton.setBounds(340,50,30,30);
okButton.setBounds(100,130,80,30);
cancelButton.setBounds(200,130,80,30);
pack();
setBounds(100,100,380,190);
setVisible(false);
//通过inner class方式添加WindowAdapter事件处理类
addWindowListener(this);
/*new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
setVisible(false);
}
});*/
}//结束构造函数DownloadSetting()
/*以下部分是公共成员函数*/
//设置工作方式:1为添加任务方式,2为显示任务信息方式,detail为显示信息
public void setMode(int mode,Vector detail)
{
if (mode==2)
{
okButton.setEnabled(false);
searchButton.setEnabled(false);
urlText.setEnabled(false);
saveText.setEnabled(false);
priorityComboBox.setEnabled(false);
threadsComboBox.setEnabled(false);
urlText.setText(detail.elementAt(0).toString());
saveText.setText(detail.elementAt(1).toString());
for(int i=0;i<priorityComboBox.getItemCount();i++)
{
String tempString1,tempString2;
tempString1=priorityComboBox.getItemAt(i).toString();
tempString2=detail.elementAt(2).toString();
if(tempString1.equals(tempString2))
priorityComboBox.setSelectedIndex(i);
tempString1=threadsComboBox.getItemAt(i).toString();
tempString2=detail.elementAt(3).toString();
if(tempString1.equals(tempString2))
threadsComboBox.setSelectedIndex(i);
}
}
if(mode==1)
{
okButton.setEnabled(true);
searchButton.setEnabled(true);
urlText.setEnabled(true);
saveText.setEnabled(true);
priorityComboBox.setEnabled(true);
threadsComboBox.setEnabled(true);
urlText.setText("");
saveText.setText("");
priorityComboBox.setSelectedIndex(4);
threadsComboBox.setSelectedIndex(4);
urlText.requestFocus();
}
setVisible(true);
}//结束公共成员函数setMode()
//取得任务数据
public Vector getDataVector()
{ return dataVector; }
/*以下部分是对各个接口的实现*/
//实现ActionListener接口
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==okButton)
{
try
{
URL url=new URL(urlText.getText());
//判断输入的信息是否有效,若无效则抛出异常
if((url.getProtocol()==null) || (url.getHost()==null))
throw new MalformedURLException();
if(!url.getProtocol().equalsIgnoreCase("HTTP"))
throw new MalformedURLException();
if((url.getHost().length()<1)||(url.getProtocol().length()<1))
throw new MalformedURLException();
//如果输入的保存文件路径不完整,则补充完整
String fileString=saveText.getText();
if((fileString.endsWith("\\")) || (fileString==null) ||
(fileString.equals("")))
{
String tempString=urlText.getText();
int indexInt=tempString.lastIndexOf('/');
fileString=fileString+tempString.substring(indexInt+1);
}
File tempFile=new File(fileString);
String parentString=tempFile.getParent();
if((parentString!=null)&&(!parentString.equals("")))
{
File parentFile=new File(parentString);
if(!parentFile.exists()) //若目录不存在,则建立新目录
if(!parentFile.mkdirs()) throw new IOException();
} //无法建立目录是抛出IO异常
dataVector=new Vector(); //取得输入的信息,存入成员变量中
dataVector.addElement("运行");
dataVector.addElement(url.toString());
dataVector.addElement(fileString);
dataVector.addElement("0");
String tempString;
tempString=priorityComboBox.getSelectedItem().toString();
dataVector.addElement(tempString);
tempString=threadsComboBox.getSelectedItem().toString();
dataVector.addElement(tempString);
setVisible(false);
}
catch(MalformedURLException urlexception)
{
JOptionPane.showMessageDialog(null,"输入有错误,请修改后重新提交"
+"\n程序目前只能支持HTTP协议","错误",JOptionPane.ERROR_MESSAGE);
}
catch(NullPointerException nullpoint)
{
JOptionPane.showMessageDialog(null,"输入有错误,文件名不能为空",
"错误",JOptionPane.ERROR_MESSAGE);
}
catch(IOException ioe)
{
JOptionPane.showMessageDialog(null,"发生意外I/O错误,可能是无法"+
"建立相应文件目录","错误", JOptionPane.ERROR_MESSAGE);
}
}
if (e.getSource()==cancelButton)
{
dataVector=null;
setVisible(false);
}
if (e.getSource()==searchButton)
{
String tempString=System.getProperty("user.dir");
JFileChooser fileChooser=new JFileChooser(tempString);
fileChooser.setApproveButtonText("确定");
fileChooser.setDialogTitle("选择下载文件保存路径");
int result=fileChooser.showOpenDialog(this.getParent());
if(result==JFileChooser.APPROVE_OPTION)
saveText.setText(fileChooser.getSelectedFile().getPath());
}
}//结束接口函数actionPerformed
//实现 WindowListener接口
public void windowClosing(WindowEvent e)
{
setVisible(false);
}
public void windowOpened(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
}//结束类DownloadSetting
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -