62,614
社区成员
发帖
与我相关
我的任务
分享
import some...
public class FileUploadClientFrame extends JFrame {
private static final long serialVersionUID = 1L;
private Container contentPane;
private JScrollPane scrollPane;
private JFileChooser jfc;
private JLabel speedLab;
private JLabel speedInfo;
private JLabel timeLab;
private JLabel timeInfo;
private JLabel fileLab;
private JTextField fileTxt;
private JButton fileBtn;
private JLabel progressLab;
private JProgressBar progressBar;
private JButton upload;
private JButton cancel;
public FileUploadClientFrame() {
setTitle("文件上传控件");
setSize(320, 240);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screen = getToolkit().getScreenSize(); // 得到屏幕尺寸
setLocation((screen.width - getSize().width) / 2,(screen.height - getSize().height) / 2);
contentPane = getContentPane();
contentPane.setLayout(null);
jfc = new JFileChooser();
jfc.setCurrentDirectory(new File("d:\\"));
scrollPane = new JScrollPane();
scrollPane.add(jfc);
add(scrollPane);
/*首行*/
speedLab = new JLabel("速度:");
speedInfo = new JLabel("0.0 KB/s");
speedInfo.setHorizontalAlignment(JTextField.CENTER);
speedLab.setBounds(20, 20, 40, 20);
speedInfo.setBounds(70, 20, 70, 20);
contentPane.add(speedLab);
contentPane.add(speedInfo);
timeLab = new JLabel("用时:");
timeInfo = new JLabel("0 ms");
timeInfo.setHorizontalAlignment(JTextField.CENTER);
timeLab.setBounds(160, 20, 40, 20);
timeInfo.setBounds(210, 20, 70, 20);
contentPane.add(timeLab);
contentPane.add(timeInfo);
/*首行结束*/
/*第二行*/
fileLab = new JLabel("文件:");
fileTxt = new JTextField();
fileBtn = new JButton("浏览");
fileBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
jfc.setFileSelectionMode(0);
int state = jfc.showOpenDialog(null);
if (1 == state) {
return;
} else {
fileTxt.setText(jfc.getSelectedFile().getAbsolutePath());
}
}
});
fileLab.setBounds(20, 50, 40, 20);
fileTxt.setBounds(70, 50, 140, 20);
fileBtn.setBounds(220, 50, 50, 20);
contentPane.add(fileLab);
contentPane.add(fileTxt);
contentPane.add(fileBtn);
/*第二行结束*/
/*第三行*/
progressLab = new JLabel("进度:");
progressLab.setBounds(20, 80, 40, 20);
contentPane.add(progressLab);
/*第三行结束*/
/*第四行*/
progressBar = new JProgressBar(1,100);
progressBar.setBounds(20, 110, 260, 20);
progressBar.setStringPainted(true);
contentPane.add(progressBar);
/*第四行结束*/
/*第五行*/
upload = new JButton("upload");
cancel = new JButton("cancel");
upload.setBounds(75, 140, 80, 20);
cancel.setBounds(165, 140, 80, 20);
upload.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (null != fileTxt.getText() && !"".equals(fileTxt.getText())) {
int state = JOptionPane.showConfirmDialog(null, "确定要上传此文件么?", "提示", JOptionPane.YES_NO_OPTION);
if (0 == state) {
new Thread(new Runnable(){
public void run() {
HashMap<Integer,Object> plugins = new HashMap<Integer,Object>();
plugins.put(IHeadKeyName.HKEY_PLUGIN_SPEED, speedInfo);
plugins.put(IHeadKeyName.HKEY_PLUGIN_TIME, timeInfo);
plugins.put(IHeadKeyName.HKEY_PLUGIN_PERCENT, progressBar);
UploadHandler loader = new UploadHandler();
loader.doEvent(jfc.getSelectedFile(), plugins);
}
}).start();
}
} else {
JOptionPane.showMessageDialog(null, "没有选择要上传的文件!", "错误", JOptionPane.ERROR_MESSAGE);
}
}
});
cancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(1);
}
});
contentPane.add(upload);
contentPane.add(cancel);
/*第五行结束*/
}
public static void main(String[] args) {
new FileUploadClientFrame();
}
}
contentPane.setLayout(null);