-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainJFrame.java
More file actions
454 lines (399 loc) · 20.4 KB
/
Copy pathMainJFrame.java
File metadata and controls
454 lines (399 loc) · 20.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.ctrip.spider;
import com.ctrip.entity.City;
import com.ctrip.util.Constant;
import com.ctrip.util.Toolkit;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.xml.sax.SAXException;
import javax.swing.*;
import javax.xml.parsers.ParserConfigurationException;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.io.File;
/**
* @author LucasX
*/
public class MainJFrame extends javax.swing.JFrame {
private static Logger logger = LogManager.getLogger();
public static List<String> listCityData = new ArrayList<>();
protected static boolean isEnable = true;
/**
* Creates new form MainJFrame
*/
public MainJFrame() {
Toolkit.checkJdkVersion();
try {
initComponents();
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception err) {
err.getStackTrace();
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() throws IOException, SAXException, ParserConfigurationException {
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jComboBox1 = new javax.swing.JComboBox<>();
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList<>();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem4 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jCheckBoxMenuItem1 = new javax.swing.JCheckBoxMenuItem();
jMenu3 = new javax.swing.JMenu();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem();
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
setTitle("携程数据采集系统");
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
setFont(new java.awt.Font("Microsoft YaHei UI", 0, 10)); // NOI18N
setPreferredSize(new java.awt.Dimension(580, 480));
setResizable(false);
setSize(new java.awt.Dimension(580, 480));
jLabel1.setFont(new java.awt.Font("微软雅黑", 0, 12)); // NOI18N
jLabel1.setText("保存目录");
jTextField1.setEditable(false);
jTextField1.setBackground(new java.awt.Color(255, 255, 255));
jButton1.setFont(new java.awt.Font("微软雅黑", 0, 12)); // NOI18N
jButton1.setText("浏览");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel2.setFont(new java.awt.Font("微软雅黑", 0, 12)); // NOI18N
jLabel2.setText("城市列表");
jComboBox1.setFont(new java.awt.Font("微软雅黑", 0, 12)); // NOI18N
List<City> cityList = Toolkit.readCityXmlConfig(Constant.CITY_XML_CONFIG);
String[] cities = new String[cityList.size()];
for (int i = 0; i < cityList.size(); i++) {
cities[i] = cityList.get(i).getName();
}
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(cities));
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});
jList1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jList1MouseClicked(evt);
}
});
jScrollPane1.setViewportView(jList1);
jLabel3.setFont(new java.awt.Font("微软雅黑", 0, 12)); // NOI18N
jLabel3.setText("已选城市");
jLabel4.setFont(new java.awt.Font("微软雅黑", 0, 12)); // NOI18N
jLabel4.setText("单击删除");
jButton2.setFont(new java.awt.Font("微软雅黑", 0, 12)); // NOI18N
jButton2.setText("开始获取");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jLabel5.setFont(new java.awt.Font("微软雅黑", 0, 12)); // NOI18N
jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel5.setText("copyright LucasX");
jLabel6.setBackground(new java.awt.Color(255, 255, 255));
jLabel6.setFont(new java.awt.Font("微软雅黑", 0, 12)); // NOI18N
jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel6.setText(" ");
jMenu1.setText("文件");
jMenuItem4.setText("退出");
jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem4ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem4);
jMenuBar1.add(jMenu1);
jMenu2.setText("设置");
jCheckBoxMenuItem1.setSelected(true);
jCheckBoxMenuItem1.setText("多线程");
jCheckBoxMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jCheckBoxMenuItem1ActionPerformed(evt);
}
});
jMenu2.add(jCheckBoxMenuItem1);
jMenuBar1.add(jMenu2);
jMenu3.setText("帮助");
jMenuItem2.setText("bug反馈");
jMenuItem2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jMenuItem2MouseClicked(evt);
}
});
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
jMenu3.add(jMenuItem2);
jMenuItem3.setText("使用说明");
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem3ActionPerformed(evt);
}
});
jMenu3.add(jMenuItem3);
jMenuBar1.add(jMenu3);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(40, 40, 40)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 333, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 333, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel4))
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 333, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE)))))
.addContainerGap(22, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(40, 40, 40)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(23, 23, 23)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(27, 27, 27)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(32, 32, 32)
.addComponent(jLabel6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
.addComponent(jLabel5))
);
//set icon
Image image = java.awt.Toolkit.getDefaultToolkit().getImage("agt_web.ico");
setIconImage(image);
//set the city.xml read only
new File("city.xml").setReadOnly();
pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
//bug反馈
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Runtime runtime = Runtime.getRuntime();
String command = "C:\\Program Files\\Internet Explorer\\iexplore.exe http://www.weibo.com/xldev";
try {
runtime.exec(command);
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "浏览器设置错误");
}
}
//关于
private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("explorer " + new File("").getAbsolutePath().toString() + "\\manual.pdf");
logger.trace("explorer " + new File("").getAbsolutePath().toString() + "\\manual.pdf");
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "未安装Adobe Reader!");
logger.error("未安装Adobe Reader!");
}
}
//多线程
private void jCheckBoxMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String selectedItemName = jComboBox1.getSelectedItem().toString();
if (!listCityData.contains(selectedItemName)) {
listCityData.add(selectedItemName);
}
jList1.setListData(Toolkit.convertListToArray(listCityData));
}
private void jList1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
String selectedItemValue = jList1.getSelectedValue();
int selectedItemIndex = jList1.getSelectedIndex();
if (listCityData.contains(selectedItemValue)) {
listCityData.remove(selectedItemIndex);
}
jList1.setListData(Toolkit.convertListToArray(listCityData));
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (null != jTextField1.getText() && !"".equals(jTextField1.getText()) && listCityData.size() != 0) {
MainJFrame.isEnable = false;
CtripSpider.folder = jTextField1.getText().trim();
MultiThreadCrawlerOne multiThreadCrawlerOne = new MultiThreadCrawlerOne();
MultiThreadCrawlerTwo multiThreadCrawlerTwo = new MultiThreadCrawlerTwo();
Thread thread1 = new Thread(multiThreadCrawlerOne);
Thread thread2 = new Thread(multiThreadCrawlerTwo);
thread1.start();
logger.debug("线程1开始运行...");
thread2.start();
logger.debug("线程2开始运行...");
this.changeEnableState(isEnable, jButton1, jButton2, jComboBox1, jList1, jLabel6);
} else {
JOptionPane.showMessageDialog(null, "目录或城市不能为空");
}
}
protected void changeEnableState(boolean enable, JButton jButton1, JButton jButton2, JComboBox jComboBox, JList jList, JLabel jLabel6) {
if (enable == false) {
jButton2.setEnabled(enable);
jButton1.setEnabled(enable);
jComboBox.setEnabled(enable);
jList.setEnabled(enable);
jButton2.setText("正在爬取,请稍后...");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
jLabel6.setText("程序开始于 " + simpleDateFormat.format(new Date()));
} else if (enable == true) {
jButton2.setText("开始获取");
jButton2.setEnabled(enable);
jButton1.setEnabled(enable);
jComboBox.setEnabled(enable);
jList.setEnabled(enable);
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JFileChooser jFileChooser = new JFileChooser(".");
jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jFileChooser.showOpenDialog(this);
if (null != jFileChooser.getSelectedFile()) {
String path = jFileChooser.getSelectedFile().getAbsolutePath();
jTextField1.setText(path);
}
}
private void jMenuItem2MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
MainJFrame mainJFrame = new MainJFrame();
mainJFrame.setVisible(true);
mainJFrame.addWindowListener(new WindowAdapter() {
/**
* Invoked when a window is in the process of being closed.
* The close operation can be overridden at this point.
*
* @param e
*/
@Override
public void windowClosing(WindowEvent e) {
int exit = JOptionPane.showConfirmDialog(null, "确定退出?", "警告", JOptionPane.OK_CANCEL_OPTION);
if (exit == 0) {
System.exit(0);
}
}
});
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem1;
private javax.swing.JComboBox<String> jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JList<String> jList1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}