我正在創建一個程序來抓取圖像網站並將這些圖像文件名和屬性放在列表中,問題是,每當我嘗試使用GUI從URL中獲取數據時,該程序需要大約20-30秒才能在我的表格模型上顯示信息,但是當我在沒有GUI的情況下運行它時(只是控制檯和簡單的系統out println),它只需要2-4秒,在某些時候甚至更快。這裏是我的GUI代碼:在GUI中運行程序很慢
public class ImageDownloader extends JFrame {
private JPanel contentPane;
private JTextField urlTextField;
private JButton btnCheck;
private JButton btnDownload;
private JButton btnDownloadAll;
private JTable table;
private String imgUrl;
private String url;
Document document;
Elements media;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ImageDownloader frame = new ImageDownloader();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ImageDownloader() {
setTitle("Image Downloader");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 565, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
FlowLayout flowLayout = (FlowLayout) panel.getLayout();
flowLayout.setAlignment(FlowLayout.LEFT);
contentPane.add(panel, BorderLayout.NORTH);
JLabel lblWebsiteUrl = new JLabel("Website URL:");
panel.add(lblWebsiteUrl);
urlTextField = new JTextField();
panel.add(urlTextField);
urlTextField.setColumns(30);
btnCheck = new JButton("Check");
btnCheck.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
List<Images> images = new ArrayList<>();
url = urlTextField.getText();
if(url.isEmpty()) {
JOptionPane.showMessageDialog(ImageDownloader.this, "Please enter a website URL", "Input Error", JOptionPane.ERROR_MESSAGE);
} else {
try {
document = Jsoup.connect(urlTextField.getText()).userAgent("Mozilla").timeout(10 * 1000).get();
media = document.select("[src]");
for(Element src : media) {
if(src.tagName().equals("img")) {
imgUrl = src.attr("abs:src");
URL url = new URL(imgUrl);
long size = url.openConnection().getContentLengthLong();
images.add(new Images(src.tagName(), src.attr("abs:src"), src.attr("width"), src.attr("height"), size));
}
}
ImageDownloaderTableModel tableModel = new ImageDownloaderTableModel(images);
table.setModel(tableModel);
} catch (IOException e1) {
JOptionPane.showMessageDialog(ImageDownloader.this, "Error loading website, The site that you are trying to reach is either down or does not exist..", "Error Loading", JOptionPane.ERROR_MESSAGE);
e1.printStackTrace();
}
}
}
});
panel.add(btnCheck);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.SOUTH);
btnDownloadAll = new JButton("Download All");
btnDownloadAll.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
media = document.select("img");
for(Element src : media) {
String strImgUrl = src.attr("abs:src");
downloadImage(strImgUrl);
}
} catch(Exception ex) {
ex.printStackTrace();
}
}
});
panel_1.add(btnDownloadAll);
btnDownload = new JButton("Download");
panel_1.add(btnDownload);
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
table = new JTable();
scrollPane.setViewportView(table);
}
public static void downloadImage(String imgUrl) {
String strImgUrl = imgUrl.substring(imgUrl.lastIndexOf("/") + 1);
try {
URL urlImage = new URL(imgUrl);
InputStream in = urlImage.openStream();
byte[] buffer = new byte[4096];
int n = -1;
OutputStream os = new FileOutputStream(strImgUrl);
while((n = in.read(buffer)) != -1) {
os.write(buffer, 0, n);
}
os.close();
System.out.println("Saved..");
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
您似乎在Swing事件線程中做了所有繁重的工作,而不是在後臺線程(如SwingWorker)中 - 爲什麼? –