日期:2014-05-20 浏览次数:20961 次
package swing;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.UIManager;
import javax.swing.tree.DefaultTreeModel;
/**
* 舆情分析主界面
*
* @author Administrator
*
*/
public class MainFrame extends JPanel {
private String savePath = null; // 特征树要保存的路径
private String loadPath = null; // 要加载的特征树所在的路径
private String opinionPath = null; // 评论所在的文件路径
private String resultPath = null; // 分析结果的存放路径
private int newNodeIndex = 1; // 给特征树添加节点时用于唯一标示一个节点
private JPanel treeEditPane = new JPanel(); // 窗口中上半部分用于构造树的面板
private JPanel buttonPane = new JPanel(); // 窗口下半部分用于放置操作按钮的面板
private static JFrame frame = new JFrame("舆情分析系统"); // 用于显示的窗口
private FeatureTree treePane; // 放置树的面板
public MainFrame() {
super(new GridLayout(1, 0));
// 初始化“树操作区”和“按钮区”
initTreeView(null);
initButtonView();
JScrollPane treeView = new JScrollPane(treeEditPane);
JScrollPane buttonView = new JScrollPane(buttonPane);
// 中间分隔线
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setTopComponent(treeView);
splitPane.setBottomComponent(buttonView);
// 设置“树操作区”和“按钮区”大小及分隔线的位置
Dimension minimumSize = new Dimension(100, 50);
treeView.setMinimumSize(minimumSize);
buttonView.setMinimumSize(minimumSize);
splitPane.setDividerLocation(310);
splitPane.setPreferredSize(new Dimension(430, 370));
add(splitPane);
}
/**
* 初始化“树操作区”内容
*
*/
private void initTreeView(DefaultTreeModel argTreeModel) {
treePane = new FeatureTree(argTreeModel);
JPanel buttonPane = new JPanel();
JButton btnAdd = new JButton("添加");
JButton btnDelete = new JButton("删除");
JButton btnClear = new JButton("清空");
MyActionListener actionListener = new MyActionListener();
btnAdd.addActionListener(actionListener);
btnDelete.addActionListener(actionListener);
btnClear.addActionListener(actionListener);
buttonPane.add(btnAdd);
buttonPane.add(btnDelete);
buttonPane.add(btnClear);
treeEditPane.setLayout(new BorderLayout());
treeEditPane.add(treePane, BorderLayout.CENTER);
treeEditPane.add(buttonPane, BorderLayout.SOUTH);
}
/**
* 初始化“按钮区”的内容,并为按钮添加事件监听器
*
*/
private void initButtonView() {
JButton btnExportTree = new JButton("保存特征树");
JButton btnImportTree = new JButton("导入特征树");
JButton btnOpinionFile = new JButton("选择评论文件");
JButton btnResultFile = new JButton("结果输出到");
JRadioButton wekaModel = new JRadioButton("WEKA", true);
JRadioButton svmModel = new JRadioButton("SVM", true);
JButton btnOk = new JButton("确定");
JButton btnConcle = new JButton("取消");
// 添加事件监听器
MyActionListener myListener = new MyActionListener();
btnExportTree.addActionListener(myListener);
btnImportTree.addActionListener(myListener);
btnOpinionFile.addActionListener(myListener);
btnResultFile.addActionListener(myListener);
btnOk.addActionListener(myListener);
btnConcle.addActionListener(myListener);
ButtonGroup bGroup = new ButtonGroup();
bGroup.add(wekaModel);
bGroup.add(svmModel);
buttonPane.setLayout(new GridLayout(2, 4));
buttonPane.add(btnExportTree);
buttonPane.add(btnImportTree);
buttonPane.add(btnOpinionFile);
buttonPane.add(btnResultFile);
buttonPane.add(wekaModel);
buttonPane.add(svmModel);
buttonPane.add(btnOk);
buttonPane.add(btnConcle);
}
/**
* 事件监听的内部类
*
* @author Administrator
*
*/
private class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
if (("保存特征树").equals(actionCommand)) {
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("保存特征树到");
int returnVal = fc.showOpenDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
savePath = file.getPath();
boolean isSaveSuccess = treePane.saveTree(savePath);
if (isSaveSuccess) {
JOptionPane.showMessageDialog(frame, "保存特征树成功");
} else {
JOptionPane.showMessageDialog(frame, "保存特征树失败");
}
}
} else if (("导入特征树").equals(actionCommand)) {
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("导入特征树");
int returnVal = fc.showOpenDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
loadPath = file.getPath();
DefaultTreeModel treeModel = treePane.loadTree(loadPath);
if (treeModel!=null) {
initTreeView(treeModel);
JOptionPane.showMessageDialog(frame, "加载特征树成功");
} else {
JOptionPane.showMessageDialog(frame, "加载特征树失败");
}
}
} else if (("选择评论文件").equals(actionCommand)) {
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("选择评论文件");
int returnVal = fc.showOpenDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
opinionPath = file.getPath();
}
} else if (("结果输出到").equals(actionCommand)) {
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("结果输出到");
int returnVal = fc.showOpenDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
resultPath = file.getPath();
}
} else if (("确定").equals(actionCommand)) {
} else if ("取消".equals(actionCommand)) {
frame.dispose();
System.exit(0);
} else if ("添加".equals(actionCommand)) {
// 为树添加一个节点
treePane.addObject((newNodeIndex++) + "name,0.###");
} else if ("删除".equals(actionCommand)) {
// 删除树的一个节点
treePane.removeCurrentNode();
} else if ("清空".equals(actionCommand)) {
// 清空树的节点
treePane.clear();
}
}
}
/**
* 创建并显示窗口
*
*/
public static void createAndShowGUI() {
// 设置窗口样式
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.out.println("设置窗口样式出错");
e.printStackTrace();
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MainFrame contentPane = new MainFrame();
contentPane.setOpaque(true);
frame.setContentPane(contentPane);
// 设置窗口大小,显示窗口
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}