IPAssignFrm.java~187~
资源名称:Java.rar [点击查看]
上传用户:liming9091
上传日期:2014-10-27
资源大小:3376k
文件大小:11k
源码类别:
Java编程
开发平台:
Java
- package tsinghuaip;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.table.*;
- import javax.swing.event.*;
- import com.borland.jbcl.layout.*;
- import javax.swing.border.*;
- import java.util.*;
- import java.math.*;
- public class IPAssignFrm extends JFrame {
- JPanel contentPane;
- XYLayout xYLayout1 = new XYLayout();
- DefaultTableColumnModel columnModel = new DefaultTableColumnModel();
- TableColumn column = new TableColumn();
- JTableHeader tableHeader = new JTableHeader(columnModel);
- IPInfoDataModel ipInfoDataModal = new IPInfoDataModel();
- JTable jTable1 = new JTable(ipInfoDataModal);
- JLabel jLabel1 = new JLabel();
- JButton queryButton = new JButton();
- JButton exitButton = new JButton();
- JFormattedTextField IPAddrTextField = new JFormattedTextField();
- TitledBorder titledBorder1;
- TitledBorder titledBorder2;
- TitledBorder titledBorder3;
- JLabel jLabel2 = new JLabel();
- //Construct the frame
- public IPAssignFrm() {
- enableEvents(AWTEvent.WINDOW_EVENT_MASK);
- try {
- IPInfoInit(); // some initialization about IP info
- jbInit();
- addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- dispose();
- }
- });
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- private void IPInfoInit() throws Exception {
- IPInfoToDB iPInfoToDB = new IPInfoToDB();
- iPInfoToDB.SetTxtFileName("TsinghuaIPAssign.txt");
- iPInfoToDB.SaveIPToDB();
- //GenXML genXML = new GenXML();
- //genXML.SetXmlFileName("TsinghuaIPAssign.xml");
- //genXML.SaveIPFromDB();
- }
- //Component initialization
- private void jbInit() throws Exception {
- contentPane = (JPanel)this.getContentPane();
- titledBorder1 = new TitledBorder("");
- titledBorder2 = new TitledBorder("");
- titledBorder3 = new TitledBorder("");
- contentPane.setMaximumSize(new Dimension(480, 400));
- contentPane.setMinimumSize(new Dimension(480, 400));
- contentPane.setOpaque(true);
- contentPane.setPreferredSize(new Dimension(480, 400));
- contentPane.setLayout(xYLayout1);
- this.setContentPane(contentPane);
- this.setDefaultCloseOperation(HIDE_ON_CLOSE);
- this.setSize(new Dimension(550, 400));
- this.setTitle("清华大学IP地址分布查询器(野人制作)");
- jTable1.setBackground(new Color(149, 165, 182));
- jTable1.setToolTipText("清华大学IP地址分配表");
- jTable1.setRowHeight(25);
- jTable1.setRowMargin(3);
- jLabel1.setFont(new java.awt.Font("Dialog", 0, 15));
- jLabel1.setRequestFocusEnabled(false);
- jLabel1.setText("请出入待查询的IP地址:");
- queryButton.setFont(new java.awt.Font("DialogInput", 1, 16));
- queryButton.setText("开始查询");
- queryButton.addActionListener(new IPAssignFrm_queryButton_actionAdapter(this));
- IPAddrTextField.setBackground(Color.white);
- IPAddrTextField.setBorder(BorderFactory.createEtchedBorder());
- IPAddrTextField.setHorizontalAlignment(SwingConstants.CENTER);
- IPAddrTextField.setText("");
- jLabel2.setFont(new java.awt.Font("Monospaced", 0, 13));
- jLabel2.setForeground(Color.darkGray);
- jLabel2.setToolTipText("");
- jLabel2.setText("Savage制作@CopyRight 2003-2010...");
- JScrollPane scrollpane = JTable.createScrollPaneForTable(jTable1);
- exitButton.addActionListener(new IPAssignFrm_exitButton_actionAdapter(this));
- exitButton.setText("退出系统");
- exitButton.addActionListener(new IPAssignFrm_exitButton_actionAdapter(this));
- exitButton.setFont(new java.awt.Font("DialogInput", 1, 16));
- contentPane.add(scrollpane, new XYConstraints(16, 22, 510, 202));
- contentPane.add(jLabel1, new XYConstraints(30, 249, 167, 37));
- contentPane.add(IPAddrTextField, new XYConstraints(214, 250, 152, 34));
- contentPane.add(jLabel2, new XYConstraints(114, 357, 288, -1));
- contentPane.add(exitButton, new XYConstraints(312, 301, 136, 31));
- contentPane.add(queryButton, new XYConstraints(66, 299, 136, 31));
- }
- //Overridden so we can exit when window is closed
- protected void processWindowEvent(WindowEvent e) {
- super.processWindowEvent(e);
- if (e.getID() == WindowEvent.WINDOW_CLOSING) {
- System.exit(0);
- }
- }
- void queryButton_actionPerformed(ActionEvent e) {
- String strIP = IPAddrTextField.getText();
- if (strIP.trim().length() == 0) {
- JOptionPane anOptionPane = new JOptionPane(); //弹出对话框
- anOptionPane.showMessageDialog(this, "请输入待查询得IP地址!", "输入提示",
- JOptionPane.ERROR_MESSAGE);
- return;
- }
- QueryIP(strIP);
- }
- /**********************************************
- 这个函数的作用就是查询输入的IP地址
- **********************************************/
- private void QueryIP(String strIP) {
- strIP = strIP.trim();
- String strResult;
- //检查IP地址格式
- if (CheckIPFormat(strIP) == false) {
- strResult = "对不起,IP地址输入错误!";
- JOptionPane anOptionPane = new JOptionPane(); //弹出对话框
- anOptionPane.showMessageDialog(this, strResult, "IP地址输入格式错误",
- JOptionPane.ERROR_MESSAGE);
- return;
- }
- //将IP地址字符串中的'.'去掉
- strIP = IngnoreIPDot(strIP);
- //将去掉'.'的IP地址字符串转换为长整数
- BigInteger bigIntIP = StringToBigInt(strIP);
- //查找IP地址所属单位
- strResult = CheckIPInList(bigIntIP);
- JOptionPane anOptionPane = new JOptionPane(); //弹出对话框
- anOptionPane.showMessageDialog(this, strResult, "查询结果",
- JOptionPane.PLAIN_MESSAGE);
- }
- /**********************************************
- 这个函数的作用就是检查输入的IP地址的格式是否正确
- **********************************************/
- private boolean CheckIPFormat(String strIP) {
- int intLen = strIP.length();
- int index = 0;
- if (strIP.length() > 15)
- return false;
- while (index < intLen) {
- if ( ( (strIP.charAt(index) >= '0') && (strIP.charAt(index) <= '9'))
- || (strIP.charAt(index) == '.')) {
- index++;
- }
- else
- break;
- }
- if (index < intLen)
- return false;
- return true;
- }
- /********************************************************
- 这个函数的作用就是将IP地址中间的‘.’去掉将其连成一纯数字字符串
- *********************************************************/
- private String IngnoreIPDot(String strIP) {
- String strTmp = new String("");
- strIP = strIP.trim();
- StringTokenizer token = new StringTokenizer(strIP, ".");
- while (token.hasMoreElements()) {
- strTmp = strTmp.concat( (String) token.nextElement());
- }
- return strTmp;
- }
- /**********************************************
- 这个函数的作用就是将数字字符串转换为长整数
- **********************************************/
- private BigInteger StringToBigInt(String strIP) {
- return new BigInteger(strIP);
- }
- /**********************************************
- 这个函数的作用就是在已有IP地址列表中查找此IP地址,如果
- 找到就返回该IP地址的所属单位,如果找不到就报错
- **********************************************/
- public String CheckIPInList(BigInteger bigIntIP) {
- SAXParseIPInfo parseIPInfo = new SAXParseIPInfo();
- try {
- parseIPInfo.ParseIPInfo();
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- Vector startIPVector = parseIPInfo.GetStartIPVector();
- Vector endIPVector = parseIPInfo.GetEndIPVector();
- Vector localVector = parseIPInfo.GetLocalVector();
- int intLen = endIPVector.size();
- for (int i = 1; i < intLen; i++) {
- BigInteger bigIntStartIP = StringToBigInt(
- IngnoreIPDot( (String) startIPVector.elementAt(i)));
- BigInteger bigIntEndIP = StringToBigInt(
- IngnoreIPDot( (String) endIPVector.elementAt(i)));
- if ( ( (bigIntIP.compareTo(bigIntStartIP) == 1) &&
- (bigIntIP.compareTo(bigIntEndIP) == -1))
- || (bigIntIP.compareTo(bigIntStartIP) == 0)
- || (bigIntIP.compareTo(bigIntEndIP) == 0)) {
- String strResult = "此IP属于";
- strResult = strResult.concat( (String) localVector.elementAt(i));
- return strResult;
- }
- }
- return "对不起,此IP不属于清华大学!";
- }
- void exitButton_actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- }
- class IPAssignFrm_queryButton_actionAdapter implements java.awt.event.
- ActionListener {
- IPAssignFrm adaptee;
- IPAssignFrm_queryButton_actionAdapter(IPAssignFrm adaptee) {
- this.adaptee = adaptee;
- }
- public void actionPerformed(ActionEvent e) {
- adaptee.queryButton_actionPerformed(e);
- }
- }
- class IPInfoDataModel extends AbstractTableModel {
- private Object[][] arrIPInfo;
- private Vector startIPVector;
- private Vector endIPVector;
- private Vector localVector;
- public IPInfoDataModel() {
- SAXParseIPInfo parseIPInfo = new SAXParseIPInfo();
- try {
- parseIPInfo.ParseIPInfo();
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- startIPVector = parseIPInfo.GetStartIPVector();
- endIPVector = parseIPInfo.GetEndIPVector();
- localVector = parseIPInfo.GetLocalVector();
- int intLen = startIPVector.size();
- arrIPInfo = new Object[intLen][3];
- for (int i = 0; i < intLen; i++) {
- arrIPInfo[i] = new Object[3];
- }
- for (int m = 0; m < intLen-1; m++) {
- //System.out.print(m);
- arrIPInfo[m][0] = startIPVector.elementAt(m);
- arrIPInfo[m][1] = endIPVector.elementAt(m);
- arrIPInfo[m][2] = localVector.elementAt(m);
- }
- }
- public int getColumnCount() {
- return arrIPInfo[0].length;
- }
- public int getRowCount() {
- return arrIPInfo.length - 1;
- }
- public Object getValueAt(int row, int col) {
- return arrIPInfo[row][col];
- }
- public boolean isCellEditable(int row, int col) {
- return false;
- }
- }
- class IPAssignFrm_exitButton_actionAdapter implements java.awt.event.
- ActionListener {
- IPAssignFrm adaptee;
- IPAssignFrm_exitButton_actionAdapter(IPAssignFrm adaptee) {
- this.adaptee = adaptee;
- }
- public void actionPerformed(ActionEvent e) {
- adaptee.exitButton_actionPerformed(e);
- }
- }