package bbsFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.TextArea;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
public class MainFrame extends JFrame implements ActionListener {
private static final String HOT = “HOT”;
private static final String SECTION = “SECTION”;
private static final String POST = “POST”;
private static final String EFF = “EFFECTIVE”;
private static final String SELECT = “HOSTSELECT”;
private static final String HOSTINFO = “HOSTINFO”;
private static final String HOSTPOST = “HOSTPOST”;
private static final String MODEL = “MODEL”;
private static final String CANCEL = “CANCEL”;
private static final String SAVE = “Save”;
private static final long serialVersionUID = -196950585230726337L;
private JMenuBar myBar;
private JMenu menu;
private JMenu menu1;
private JMenu menu2;
private JMenu menu3;
private JMenu menu4;
private JMenu menu5;
private JMenu menu6;
private JLabel state_lab;
private JLabel Declare_lab;
private JLabel FilePath_lab;
private JTextField FilePath_jf;
public static TextArea state;
public static JProgressBar Schedule_jb;
private JButton hot;
private JButton section;
private JButton postinfo;
private JButton eff;
private JButton select;
private JButton hostinfo;
private JButton hostpost;
private JButton model;
private JButton stop;
private JButton Save;
public static void main(String[] args) {
new MainFrame();
}
public void setBg() {
((JPanel) this.getContentPane()).setOpaque(false);
ImageIcon img = new ImageIcon("bg.jpg");
JLabel background = new JLabel(img);
background.setBounds(0, 0, 960, 600);
this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
}
private void initComponents() {
setBg();
myBar = new JMenuBar();
setTitle("天涯简易爬虫系统");
setLayout(null);
add(getstate());
// state.setEditable(false);
add(getstate_lab());
add(getFilePath_jf());
add(getFilePath_lab());
add(getSave());
add(getstop());
add(getDeclare_lab());
menu = new JMenu(" || ");
menu1 = new JMenu(" || ");
menu2 = new JMenu(" || ");
menu3 = new JMenu(" || ");
menu4 = new JMenu(" || ");
menu5 = new JMenu(" || ");
menu6 = new JMenu(" || ");
gethot();
getsection();
getpostinfo();
geteff();
getselect();
gethostinfo();
gethostpost();
getmodel();
myBar.add(hot);
myBar.add(menu);
myBar.add(section);
myBar.add(menu5);
myBar.add(postinfo);
myBar.add(menu1);
myBar.add(eff);
myBar.add(menu2);
myBar.add(select);
myBar.add(menu3);
myBar.add(hostinfo);
myBar.add(menu4);
myBar.add(hostpost);
myBar.add(menu6);
myBar.add(model);
setSize(960, 600);
this.setVisible(true);
this.setJMenuBar(myBar);
this.setLocation(150, 80);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
}
private JButton gethot() {
if (hot == null) {
hot = new JButton();
hot.setText("热帖分析");
hot.setActionCommand(HOT);
// stop.setBounds(203, 153, 81, 36);
hot.addActionListener(this);
}
return hot;
}
private JButton getsection() {
if (section == null) {
section = new JButton();
section.setText("版块抓取");
section.setActionCommand(SECTION);
// section.setBounds(203, 153, 81, 36);
section.addActionListener(this);
}
return section;
}
private JButton getpostinfo() {
if (postinfo == null) {
postinfo = new JButton();
postinfo.setText("贴子分析");
postinfo.setActionCommand(POST);
// section.setBounds(203, 153, 81, 36);
postinfo.addActionListener(this);
}
return postinfo;
}
private JButton geteff() {
if (eff == null) {
eff = new JButton();
eff.setText("链接验证");
eff.setActionCommand(EFF);
// eff.setBounds(203, 153, 81, 36);
eff.addActionListener(this);
}
return eff;
}
private JButton getselect() {
if (select == null) {
select = new JButton();
select.setText("用户选取");
select.setActionCommand(SELECT);
// select.setBounds(203, 153, 81, 36);
select.addActionListener(this);
}
return select;
}
private JButton gethostinfo() {
if (hostinfo == null) {
hostinfo = new JButton();
hostinfo.setText("用户抓取");
hostinfo.setActionCommand(HOSTINFO);
// hostinfo.setBounds(203, 153, 81, 36);
hostinfo.addActionListener(this);
}
return hostinfo;
}
private JButton gethostpost() {
if (hostpost == null) {
hostpost = new JButton();
hostpost.setText("舆情预警");
hostpost.setActionCommand(CANCEL);
// hostpost.setBounds(203, 153, 81, 36);
hostpost.addActionListener(this);
}
return hostpost;
}
private JButton getmodel() {
if (model == null) {
model = new JButton();
model.setText("模型分析");
model.setActionCommand(MODEL);
// hostpost.setBounds(203, 153, 81, 36);
model.addActionListener(this);
}
return model;
}
private JTextField getFilePath_jf() {
if (FilePath_jf == null) {
FilePath_jf = new JTextField();
File file = new File("BBS.config");
if(!file.exists())
JOptionPane.showMessageDialog(this, "配置文件不存在,系统采用默认存储路径!");;
String tempString = "E:\\天涯论坛数据";
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
tempString = reader.readLine();
reader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FilePath_jf.setText(tempString);
FilePath_jf.setEditable(false);
FilePath_jf.setBounds(125, 155, 280, 25);
}
return FilePath_jf;
}
private JLabel getFilePath_lab() {
if (FilePath_lab == null) {
FilePath_lab = new JLabel();
FilePath_lab.setText("默认存储路径:");
FilePath_lab.setBounds(120, 120, 100, 20);
}
return FilePath_lab;
}
private JLabel getDeclare_lab() {
if (Declare_lab == null) {
Declare_lab = new JLabel();
Declare_lab
.setText("声明:
天涯论坛的网络爬虫纯属个人制作,爬虫版本为V0.0.1
爬取请参照说明书");
Declare_lab.setBounds(120, 200, 300, 100);
}
return Declare_lab;
}
private JButton getSave() {
if (Save == null) {
Save = new JButton();
Save.setText("设置");
Save.setActionCommand(SAVE);
Save.setBounds(140, 340, 81, 36);
Save.addActionListener(this);
}
return Save;
}
private JButton getstop() {
if (stop == null) {
stop = new JButton();
stop.setText("退出");
stop.setActionCommand(CANCEL);
stop.setBounds(320, 340, 81, 36);
stop.addActionListener(this);
}
return stop;
}
private JLabel getstate_lab() {
if (state_lab == null) {
state_lab = new JLabel();
state_lab.setText("当前状态:");
state_lab.setBounds(580, 40, 100, 20);
}
return state_lab;
}
private TextArea getstate() {
if (state == null) {
state = new TextArea();
state.setText("状态为空...\n");
state.setBounds(600, 80, 300, 400);
}
return state;
}
public MainFrame() {
initComponents();
}
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
System.out.println(action);
if (action != null && SAVE.equals(action)) {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fc.setDialogTitle("选择路径");
fc.setMultiSelectionEnabled(false);
int result = fc.showSaveDialog(fc);
if (result == JFileChooser.APPROVE_OPTION)
FilePath_jf.setText(fc.getSelectedFile().getPath());
if (JOptionPane.showConfirmDialog(this, FilePath_jf.getText(),
"是否保存路径?", 0) == 0)
SavePath(FilePath_jf.getText());
// JOptionPane.showMessageDialog(this, "设置文件不存在,系统需重新设置");//
// showConfirmDialog(this, filepath)==0);
} else if (action != null && action.equals(CANCEL)) {
try {
state.append("你选择了退出,1秒后退出!");
Thread.sleep(1000);
System.exit(0);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else if (action != null && action.equals(HOT)) {
state.append("你选择了热帖抓取,正在运行!\n");
new HotFrame();
setVisible(false);
} else if (action != null && action.equals(SECTION)) {
state.append("你选择了版块抓取,正在运行!\n");
new SectionFrame();
setVisible(false);
} else if (action != null && action.equals(POST)) {
state.append("你选择了帖子分析,正在运行!\n");
new PostInfoFrame();
setVisible(false);
} else if (action != null && action.equals(EFF)) {
state.append("你选择了链接验证,正在验证!\n");
new EffectiveFrame();
setVisible(false);
} else if (action != null && action.equals(SELECT)) {
if (JOptionPane.showConfirmDialog(this, "用户筛选来自模块帖子,确认此前用过模块抓取。",
"提示", 0) == 0) {
new SelectFrame();
state.append("你选择了抓取用户选取,正在运行!\n");
setVisible(false);
}
} else if (action != null && action.equals(HOSTINFO)) {
state.append("你选择了抓取用户抓取,正在运行!\n");
new HostInfoFrame();
setVisible(false);
} else if (action != null && action.equals(HOSTPOST)) {
state.append("你选择了舆情分析,正在运行!\n");
// new HostPostFrame();
setVisible(false);
} else if (action != null && action.equals(MODEL)) {
state.append("你选择了模型分析模块,正在运行!\n");
new ModelFrame();
setVisible(false);
}
}
public static boolean SavePath(String s) {
File f = new File("BBS.config");
FileWriter fileOut;
try {
fileOut = new FileWriter(f);
fileOut.write(s);
fileOut.close();
state.append("默认路径保存成功!\n");
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.println("Error writing to file BBS.config");
}
return true;
}
}