📄 ipstatisticframe.java
字号:
//package ipstatistic;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
/**
* @author Liangwen 2006/8/1
*/
/**
*界面类
*/
public class IPStatisticFrame extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JMenuBar menuBar1 = new JMenuBar();
JMenu file = new JMenu();
JMenuItem fileOpen=new JMenuItem();
JMenuItem fileAnalysis=new JMenuItem();
JMenuItem fileSave = new JMenuItem();
JMenuItem fileExit=new JMenuItem();
JLabel inforOutJL = new JLabel();
JLabel statusBar = new JLabel();
//添加输出框
JScrollPane jScrollPane1 = new JScrollPane();
public static JTextArea inforOutJTA = new JTextArea();
static File[] choosedFile;
static boolean openedFile=false;
static boolean analysedFile=false;
String fileList="nothing";
FileHandler fHandler;
public IPStatisticFrame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
IPStatisticFrame_actionAdapter frameAct=
new IPStatisticFrame_actionAdapter(this);
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(600,500));
this.setTitle("IP数据统计分析工具");
//设置菜单命令
file.setText("文件");
fileOpen.setText("打开");
fileOpen.setActionCommand("Open");
fileOpen.addActionListener(frameAct);
fileAnalysis.setText("分析");
fileAnalysis.setActionCommand("Analysis");
fileAnalysis.addActionListener(frameAct);
fileSave.setText("保存");
fileSave.setActionCommand("Save");
fileSave.addActionListener(frameAct);
fileExit.setText("退出");
fileExit.setActionCommand("Close");
fileExit.addActionListener(frameAct);
//添加菜单项
file.add(fileOpen);
file.add(fileAnalysis);
file.add(fileSave);
file.addSeparator();
file.add(fileExit);
//添加菜单命令
menuBar1.add(file);
this.setJMenuBar(menuBar1);
//设置提示标签
inforOutJL.setText("执行步骤:1、打开输入文件;2、分析文件;3、保存数据\n");
contentPane.add(inforOutJL, BorderLayout.SOUTH);
//添加输出框
inforOutJTA.setFont(new java.awt.Font("Dialog", 0, 14));
inforOutJTA.setEnabled(false);
contentPane.add(jScrollPane1, BorderLayout.CENTER);
jScrollPane1.getViewport().add(this.inforOutJTA, null);
//初始情况下“分析”、“保存”项呈现灰色,无法点击
fileAnalysis.setEnabled(false);
fileSave.setEnabled(false);
}
/*打开数据文件,允许同时打开多个文件
*/
void fileOpen_actionPerformed(ActionEvent e) {
JFileChooser fileChooser=new JFileChooser();
fileChooser.setMultiSelectionEnabled(true);
int option=fileChooser.showOpenDialog(this);
if(option==JFileChooser.APPROVE_OPTION)
{
choosedFile=fileChooser.getSelectedFiles();
if(choosedFile.length > 0)
fileList="已选择输入文件:\n"+
choosedFile[0].getAbsolutePath();
for(int i=1;i<choosedFile.length;i++)
fileList+="\n"+choosedFile[i].getName();
openedFile=true;
//选择文件后,允许分析
fileAnalysis.setEnabled(true);
}
//输出已选文件提示
this.inforOutJTA.append(fileList+"\n");
}
/*分析数据
*/
void fileAnalysis_actionPerformed(ActionEvent e) {
if(!openedFile) return;
fHandler=new FileHandler(this.choosedFile);
fHandler.run();
fHandler.stop();
analysedFile=true;
//分析文件后,允许保存,同时将"分析"按钮变灰
fileSave.setEnabled(true);
fileAnalysis.setEnabled(false);
}
/*数据保存入库
*/
void fileSave_actionPerformed(ActionEvent e) {
if(!analysedFile) return;
DBOperation dboper=new DBOperation();
dboper.OpenConnection();
//更新IP表
dboper.UpdateIPTable(fHandler.GetIPTable());
//释放Hash表资源
fHandler.ReleaseHash();
dboper.DBClose();
//数据保存后,将"保存"按钮变灰
fileSave.setEnabled(false);
}
/*关闭窗口
*/
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void fileExit_actionPerformed(ActionEvent e) {
System.exit(0);
}
}
/**
*事件处理程序
*/
class IPStatisticFrame_actionAdapter implements java.awt.event.ActionListener {
IPStatisticFrame adaptee;
IPStatisticFrame_actionAdapter(IPStatisticFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
//打开Path文件
if(e.getActionCommand()=="Open")
adaptee.fileOpen_actionPerformed(e);
else
//关闭窗口
if(e.getActionCommand()=="Close")
adaptee.fileExit_actionPerformed(e);
else
//分析数据
if(e.getActionCommand()=="Analysis")
adaptee.fileAnalysis_actionPerformed(e);
else
//保存h数据
if(e.getActionCommand()=="Save")
adaptee.fileSave_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -