package cn.tianzry.view;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import cn.tianzry.control.Downloader;
import javax.swing.JProgressBar;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MainWindow extends JFrame {
private static final long serialVersionUID = 5655607455866732572L;
private JPanel contentPane;
// 三个线程的控制按钮
public static JButton toBtn;
public static JButton tsBtn;
public static JButton ttBtn;
private JProgressBar toProgressBar;
private JProgressBar tsProgressBar;
private JProgressBar ttProgressBar;
private JTextField toUrlTextField;
private JTextField tsUrlTextField;
private JTextField ttUrlTextField;
private JLabel toLabel;
private JLabel ttLabel;
private JLabel tsLabel;
// 控制状态变量
private int threadOneStatus = 0;
private int threadSecondStatus = 0;
private int threadThirdStatus = 0;
// 下载器
private Downloader toDownloader;
private Downloader tsDownloader;
private Downloader ttDownloader;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow frame = new MainWindow();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainWindow() {
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 705, 420);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblHttp = new JLabel("Http\u591A\u7EBF\u7A0B\u4E0B\u8F7D\u5DE5\u5177");
lblHttp.setFont(new Font("微软雅黑", Font.PLAIN, 18));
lblHttp.setBounds(255, 13, 207, 18);
contentPane.add(lblHttp);
toUrlTextField = new JTextField();
toUrlTextField.setText("https://github.com/tianzry/Hello-World/raw/master/kongzhiqingxu.MP3");
toUrlTextField.setBounds(135, 73, 352, 24);
contentPane.add(toUrlTextField);
toUrlTextField.setColumns(10);
JLabel label = new JLabel("\u7EBF\u7A0B\u4E00\u5730\u5740\uFF1A");
label.setBounds(37, 76, 102, 18);
contentPane.add(label);
toBtn = new JButton("\u7EBF\u7A0B\u4E00");
toBtn.setBounds(550, 72, 88, 27);
contentPane.add(toBtn);
JLabel label_2 = new JLabel("\u7EBF\u7A0B\u4E8C\u5730\u5740\uFF1A");
label_2.setBounds(37, 125, 102, 18);
contentPane.add(label_2);
tsBtn = new JButton("\u7EBF\u7A0B\u4E8C");
tsBtn.setBounds(550, 121, 88, 27);
contentPane.add(tsBtn);
tsUrlTextField = new JTextField();
tsUrlTextField.setText("https://github.com/tianzry/Hello-World/raw/master/wenmingzhiguang.MP3");
tsUrlTextField.setColumns(10);
tsUrlTextField.setBounds(135, 122, 352, 24);
contentPane.add(tsUrlTextField);
JLabel label_7 = new JLabel("\u7EBF\u7A0B\u4E09\u5730\u5740\uFF1A");
label_7.setBounds(37, 173, 102, 18);
contentPane.add(label_7);
ttBtn = new JButton("\u7EBF\u7A0B\u4E09");
ttBtn.setBounds(550, 169, 88, 27);
contentPane.add(ttBtn);
ttUrlTextField = new JTextField();
ttUrlTextField.setText("https://github.com/tianzry/Hello-World/raw/master/wodeqingxu.MP3");
ttUrlTextField.setColumns(10);
ttUrlTextField.setBounds(135, 170, 352, 24);
contentPane.add(ttUrlTextField);
JLabel label_1 = new JLabel("\u7EBF\u7A0B\u4E00\u8FDB\u5EA6\uFF1A");
label_1.setBounds(63, 235, 102, 18);
contentPane.add(label_1);
toProgressBar = new JProgressBar();
toProgressBar.setBounds(155, 237, 297, 14);
contentPane.add(toProgressBar);
toProgressBar.setMaximum(100);
toLabel = new JLabel("\u672A\u4E0B\u8F7D");
toLabel.setBounds(481, 235, 72, 18);
contentPane.add(toLabel);
JLabel label_4 = new JLabel("\u7EBF\u7A0B\u4E8C\u8FDB\u5EA6\uFF1A");
label_4.setBounds(63, 274, 102, 18);
contentPane.add(label_4);
tsProgressBar = new JProgressBar();
tsProgressBar.setBounds(155, 276, 297, 14);
contentPane.add(tsProgressBar);
tsLabel = new JLabel("\u672A\u4E0B\u8F7D");
tsLabel.setBounds(481, 274, 72, 18);
contentPane.add(tsLabel);
JLabel label_3 = new JLabel("\u7EBF\u7A0B\u4E09\u8FDB\u5EA6\uFF1A");
label_3.setBounds(62, 316, 102, 18);
contentPane.add(label_3);
ttProgressBar = new JProgressBar();
ttProgressBar.setBounds(155, 318, 298, 14);
contentPane.add(ttProgressBar);
ttLabel = new JLabel("\u672A\u4E0B\u8F7D");
ttLabel.setBounds(481, 318, 72, 18);
contentPane.add(ttLabel);
toBtn.addMouseListener(new MouseAdapter() {
//String url = urlTextField.getText(); // 注意作用域问题,这里得到的参数无法传给下面
@Override
public void mouseClicked(MouseEvent e) {
switch (threadOneStatus) {
case 0:
// 每次断点续传都要重建创建一个下载器对象
if (toDownloader == null) {
toDownloader=new Downloader(toUrlTextField.getText(),toBtn, toProgressBar, toLabel);
}
toDownloader.download();
threadOneStatus = 1;
break;
case 1:
// 这里有一个问题值得注意,一旦让SwingWorker返回之后,它的状态就成了done,无法继续执行了
toDownloader.pauseDownload();
toDownloader = null;
threadOneStatus = 0;
break;
default:
break;
}
}
});
// 线程二按钮
tsBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
switch (threadSecondStatus) {
case 0:
if (tsDownloader == null) {
tsDownloader=new Downloader(tsUrlTextField.getText(),tsBtn, tsProgressBar, tsLabel);
}
tsDownloader.download();
threadSecondStatus = 1;
break;
case 1:
tsDownloader.pauseDownload();
tsDownloader = null;
threadSecondStatus = 0;
break;
default:
break;
}
}
});
// 线程三按钮
ttBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
switch (threadThirdStatus) {
case 0:
if (ttDownloader == null) {
ttDownloader=new Downloader(ttUrlTextField.getText(),ttBtn, ttProgressBar, ttLabel);
}
ttDownloader.download();
threadThirdStatus = 1;
break;
case 1:
ttDownloader.pauseDownload();
ttDownloader = null;
threadThirdStatus = 0;
break;
default:
break;
}
}
});
}
}
评论0