/*
* @author 盧偉
* @version 1.0 2008-03-06
*
*/
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
public class IPMask {
??? public static void main(String args[]) {
??????????? IPMaskFrame frame = new IPMaskFrame();
??????????? frame.init();
??? }
}
??? /**
??? * 主界面
??? */
class IPMaskFrame extends JFrame implements ActionListener, KeyListener {
??? private static final long serialVersionUID = 1L;
??? //創建窗口對象
??? private JLabel addrLabel = new JLabel("IP地址:");
??? private JTextField addrField = new JTextField(9);
??? private JLabel maskLabel = new JLabel("子網掩碼:");
??? private JTextField maskField = new JTextField(9);
??? private JPanel outputPanel = new JPanel();
??? private JTextArea outputArea = new JTextArea(7, 31);
??? private JButton calculateButton = new JButton("計算");
??? private JButton copyButton = new JButton("復制");
??? private JButton resetButton = new JButton("重置");
??? private JButton helpButton = new JButton("幫助");
??? private Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
??? //關于對話框
??? private JLabel about = new JLabel("<html>      子網掩碼計算器ver1.2.5 (2008-03-9)<br><br>該工具采用的計算公式是:<br>    網絡地址 = IP地址 & 子網掩碼<br>    廣播地址 = ~廣播地址+256 | IP地址<br>    可用主機數 = pow(2, 主機位數)-2<br>如果口算,可用如下快速計算公式:<br>     A = 256 -異常掩碼<br>     B = 異常掩碼對應的IP<br>    網絡地址 = A*n(取最接近于B但小于B的值)<br>    廣播地址 = 網絡地址+A-1<br><br>???????     作者:地圖<br>???????     郵件:godmap@sohu.com</html>");
??? private JLabel url = new JLabel(("<html>???????     博客:<a href=\"http://hi.baidu.com/godmap\">http://hi.baidu.com/godmap</a><br></html>"));
??? private JPanel aboutPanel = new JPanel();
??? private String os = System.getProperty ("os.name");
??? //初始化窗口
??? public void init() {
??????? //設置窗口組件基本屬性
??????? this.setTitle("子網掩碼計算器V1.2.5");
??????? addrField.setToolTipText("格式:172.168.1.2 ……");
??????? addrField.setActionCommand("addr");
??????? addrField.addActionListener(this);
??????? addrField.addKeyListener(this);
??????? maskField.setToolTipText("格式:1~31或255.255.255.0 ……");
??????? maskField.setActionCommand("mask");
??????? maskField.addActionListener(this);
??????? maskField.addKeyListener(this);
??????? calculateButton.setToolTipText("計算結果。");
??????? calculateButton.setActionCommand("calculate");
??????? calculateButton.addActionListener(this);
??????? copyButton.setToolTipText("將計算結果復制到系統剪貼板。");
??????? copyButton.setActionCommand("copy");
??????? copyButton.addActionListener(this);
??????? resetButton.setToolTipText("將輸入框清零。");
??????? resetButton.setActionCommand("reset");
??????? resetButton.addActionListener(this);
??????? helpButton.setToolTipText("更多幫助。");
??????? helpButton.setActionCommand("help");
??????? helpButton.addActionListener(this);
??????? outputArea.setToolTipText("Ctrl+A/Ctrl+C復制計算結果。");
??????? outputArea.setEditable(false);
??????? outputPanel.setBorder(new TitledBorder("計算結果"));
??????? outputPanel.add(outputArea);
??????? JScrollPane scrollBar = new JScrollPane(outputArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
??????? outputPanel.add(scrollBar);
??????? //設置關于窗口屬性
??????? url.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
??????? url.addMouseListener(new MouseListener() {
??????????? public void mouseClicked(MouseEvent e) {
??????????????? if (os.startsWith ("Windows")) {
??????????????????? try {
??????????????????????? Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler http://hi.baidu.com/godmap");
??????????????????? } catch (Exception urlException) {
??????????????????????? System.out.println("啟動瀏覽器錯誤:".concat(urlException.toString().concat("\n請手動啟動瀏覽器訪問:http://hi.baidu.com/godmap")));
??????????????????? }
??????????????? }
??????????????? else
??????????????????? try {
??????????????????????? Runtime.getRuntime().exec("mozila http://hi.baidu.com/godmap");
??????????????????? } catch (Exception urlException) {
??????????????????????? System.out.println("啟動瀏覽器錯誤:".concat(urlException.toString().concat("\n請手動啟動瀏覽器訪問:http://hi.baidu.com/godmap")));
??????????????????? }
??????????? }
??????????? public void mouseEntered(MouseEvent e) {
??????????? }
??????????? public void mouseExited(MouseEvent e) {
??????????? }
??????????? public void mousePressed(MouseEvent e) {
??????????? }
??????????? public void mouseReleased(MouseEvent e) {
??????????? }
??????? });
??????? GroupLayout aboutLayout = new GroupLayout(aboutPanel);
??????? aboutLayout.setHorizontalGroup(aboutLayout.createParallelGroup(GroupLayout.Alignment.CENTER)
??????????????? .addComponent(about)
??????????????? .addComponent(url));
??????? aboutLayout.setVerticalGroup(aboutLayout.createSequentialGroup()
??????????????? .addComponent(about)
??????????????? .addComponent(url));
??????? aboutPanel.setLayout(aboutLayout);
??????? aboutPanel.add(about);
??????? aboutPanel.add(url);
??????? //添加布局管理器
??????? GroupLayout groupLayout = new GroupLayout(getContentPane());
??????? groupLayout.setAutoCreateContainerGaps(true);
??????? groupLayout.setAutoCreateGaps(true);
??????? groupLayout.setHorizontalGroup(
??????????? groupLayout.createParallelGroup(GroupLayout.Alignment.CENTER)
??????????????? .addGroup(groupLayout.createParallelGroup()
??????????????????? .addGroup(groupLayout.createSequentialGroup()
??????????????????????? .addComponent(addrLabel)
??????????????????????? .addComponent(addrField)
??????????????????????? .addComponent(maskLabel)
??????????????????????? .addComponent(maskField))
??????????????????? .addComponent(outputPanel))
??????????????? .addGroup(groupLayout.createSequentialGroup()
??????????????????? .addComponent(calculateButton)
??????????????????? .addComponent(copyButton)
??????????????????? .addComponent(resetButton)
??????????????????? .addComponent(helpButton))
??????? );
??????? groupLayout.setVerticalGroup(
??????????? groupLayout.createSequentialGroup()
??????????????????? .addGroup(groupLayout.createParallelGroup()
??????????????????????? .addComponent(addrLabel)
??????????????????????? .addComponent(addrField)
??????????????????????? .addComponent(maskLabel)
??????????????????????? .addComponent(maskField))
??????????????????? .addComponent(outputPanel)
??????????????????? .addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.CENTER)
??????????????????????? .addComponent(calculateButton)
??????????????????????? .addComponent(copyButton)
??????????????????????? .addComponent(resetButton)
??????????????????????? .addComponent(helpButton))
??????? );
??????? getContentPane().setLayout(groupLayout);
??????? //輸出窗口
??????? pack();
??????? setResizable(false);
??????? setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
??????? setLocation((screenSize.width-getWidth())/2, (screenSize.height-getHeight())/2);
??????? setVisible(true);
??? }
??? //事件監聽
??? public void actionPerformed(ActionEvent e) {
??????? if(e.getActionCommand().equals("calculate")) {
??????????? outputArea.setText(this.getIPMask(deSpace(addrField), deSpace(maskField)));
??????????? outputArea.setCaretPosition(0);
??????? }
??????? else if(e.getActionCommand().equals("copy")) {
??????????? this.outputArea.selectAll();
??????????? this.outputArea.copy();
??????? }
??????? else if(e.getActionCommand().equals("reset")) {
??????????? this.addrField.setText("");
??????????? this.maskField.setText("");
??????? }
??????? else if(e.getActionCommand().equals("help")) {
??????????????? JOptionPane.showMessageDialog(null, aboutPanel, "關于", JOptionPane.INFORMATION_MESSAGE );
??????? }
??????? else ;
??? }
??? //鍵盤監聽
??? public void keyPressed(KeyEvent e) {
??? }
??? public void keyReleased(KeyEvent e) {
??????? if(e.getKeyCode()==10) {
??????????? this.calculateButton.doClick();
??????? }
??? }
??? public void keyTyped(KeyEvent e) {
??? }
???
??? //主方法
??? public String getIPMask(String ipAddr, String maskAddr) {
??????? String outputMaskInfo = "非法IP或子網掩碼地址。";
??????? //非法IP或子網掩碼地址,不進行計算。
??????? if(!isValidIP(ipAddr) || !isValidMask(maskAddr))
??????????? return outputMaskInfo;
??????? //合法IP、子網掩碼,開始計算。
??????? if(maskAddr.indexOf(".") == -1)
??????????? maskAddr = getMask(Byte.parseByte(maskAddr));
??????? String[] ipSplit = {"0","0","0","0"};
??????? String[] maskSplit = {"0","0","0","0"};
??????? ipSplit = ipAddr.split("\\.");
??????? maskSplit = maskAddr.split("\\.");
??????? String ip = "";
??????? String mask = "";
??????? String netIP = "";
??????? String broadcastIP = "";
??????? String startIP = "";
??????? String endIP = "";
??????? String binaryMask = "";
??????? String addrType = "";
??????? //String hostIP = "";
??????? int hostNum = 0;
??????? //int subNetNum = 0;
??????? int subNetMaxNum = 0;
???????
??????? for(int i = 0; i < 4; i++) {
??????????? int ipTemp = Integer.parseInt(ipSplit[i]);
??????????? int maskTemp = Integer.parseInt(maskSplit[i]);
??????????? //地址類型
??????????? if(i == 0) {
??????????????? if(ipTemp == 127)
??????????????????? addrType = "回環地址:";
??????????????? else if(ipTemp < 127)
??????????????????? addrType = "A類地址:";
??????????????? else if(ipTemp < 192)
??????????????????? addrType = "B類地址:";
??????????????? else if(ipTemp < 224)
??????????????????? addrType = "C類地址:";
??????????????? else if(ipTemp < 240)
??????????????????? addrType = "D類(組播)地址:";
??????????????? else if(ipTemp < 255)
??????????????????? addrType = "E類(保留)地址:";
??????????? }
??????????? //用戶輸入的IP
??????????? ip = ip.concat(Integer.toString(ipTemp)).concat(".");
??????????? //用戶輸入的子網掩碼
??????????? mask = mask.concat(Integer.toString(maskTemp)).concat(".");
??????????? //網絡地址
??????????? netIP = netIP.concat(Integer.toString(ipTemp & maskTemp)).concat(".");
??????????? //廣播地址
??????????? broadcastIP = broadcastIP.concat(Integer.toString(~maskTemp+256 | ipTemp)).concat(".");
??????????? //主機地址
??????????? //hostIP = hostIP.concat(Integer.toString(~maskTemp & ipTemp)).concat(".");
??????????? //可分配主機地址
??????????? if(i < 3) {
??????????????? startIP = startIP.concat(Integer.toString(ipTemp & maskTemp)).concat(".");
??????????????? endIP = endIP.concat(Integer.toString(~maskTemp+256 | ipTemp)).concat(".");
??????????? }
??????????? else if(i == 3) {
??????????????? if(maskTemp != 254) {
??????????????????? startIP = startIP.concat(Integer.toString((ipTemp & maskTemp) + 1)).concat(".");
??????????????????? endIP = endIP.concat(Integer.toString((~maskTemp+256 | ipTemp) - 1)).concat(".");
??????????????? }
??????????????? else {
??????????????????? startIP = "無.";
??????????????????? endIP = "無.";
??????????????? }
??????????? }
??????????? //生成連續的二進制子網掩碼,以計算可用主機數
??????????? binaryMask = binaryMask.concat(Integer.toBinaryString(maskTemp));
??????? }
??????? //可用主機數
??????? hostNum = (int)Math.pow(2, 32 - this.getMaskBit(binaryMask)) - 2;
??????? //可劃分子網數
??????? subNetMaxNum = (int)(32-getMaskBit(binaryMask)-2 > 0 ? Math.pow(2, 32-getMaskBit(binaryMask)-2):0);
??????? //計算子網
??????? outputMaskInfo = addrType.concat(ip.substring(0, ip.length() - 1)).concat("/").concat(mask.substring(0, mask.length() - 1))
??????????? .concat("的\n網絡地址是:").concat(netIP.substring(0, netIP.length() - 1))
??????????? .concat("\n廣播地址是:".concat(broadcastIP.substring(0, broadcastIP.length() - 1))
??????????? .concat("\n可分配主機地址包括:").concat(startIP.substring(0, startIP.length() - 1)).concat("~").concat(endIP.substring(0, endIP.length() - 1))
??????????? .concat("\n可用主機數共:").concat(Integer.toString(hostNum)).concat("臺")
??????????? .concat("\n最多可劃分:")??? .concat(Integer.toString(subNetMaxNum).concat("個子網\n")));
??????????? //.concat(this.getIPMask(false)));
??????? return outputMaskInfo;
??? }
???
??? //計算子網分配方案
??? public String getSubNet() {
??????? String subNetInfo = "子網分配方案如下:\n";
??????? for(int i=0; i<this.getIPMask("","").indexOf("a"); i++) {
???????????
??????? }
??????? return subNetInfo;
??? }
???
??? //轉換十進制掩碼為IP地址格式掩碼
??? public String getMask(byte maskBit) {
??????? if(maskBit == 1)
??????????? return "128.0.0.0";
??????? else if(maskBit == 2)
??????????? return "192.0.0.0";
??????? else if(maskBit == 3)
??????????? return "224.0.0.0";
??????? else if(maskBit == 4)
??????????? return "240.0.0.0";
??????? else if(maskBit == 5)
??????????? return "248.0.0.0";
??????? else if(maskBit == 6)
??????????? return "252.0.0.0";
??????? else if(maskBit == 7)
??????????? return "254.0.0.0";
??????? else if(maskBit ==
??????????? return "255.0.0.0";
??????? else if(maskBit ==9)
??????????? return "255.128.0.0";
??????? else if(maskBit == 10)
??????????? return "255.192.0.0";
??????? else if(maskBit == 11)
??????????? return "255.224.0.0";
??????? else if(maskBit == 12)
??????????? return "255.240.0.0";
??????? else if(maskBit == 13)
??????????? return "255.248.0.0";
??????? else if(maskBit == 14)
??????????? return "255.252.0.0";
??????? else if(maskBit == 15)
??????????? return "255.254.0.0";
??????? else if(maskBit == 16)
??????????? return "255.255.0.0";
??????? else if(maskBit == 17)
??????????? return "255.255.128.0";
??????? else if(maskBit == 18)
??????????? return "255.255.192.0";
??????? else if(maskBit == 19)
??????????? return "255.255.224.0";
??????? else if(maskBit == 20)
??????????? return "255.255.240.0";
??????? else if(maskBit == 21)
??????????? return "255.255.248.0";
??????? else if(maskBit == 22)
??????????? return "255.255.252.0";
??????? else if(maskBit == 23)
??????????? return "255.255.254.0";
??????? else if(maskBit == 24)
??????????? return "255.255.255.0";
??????? else if(maskBit == 25)
??????????? return "255.255.255.128";
??????? else if(maskBit == 26)
??????????? return "255.255.255.192";
??????? else if(maskBit == 27)
??????????? return "255.255.255.224";
??????? else if(maskBit == 28)
??????????? return "255.255.255.240";
??????? else if(maskBit == 29)
??????????? return "255.255.255.248";
??????? else if(maskBit == 30)
??????????? return "255.255.255.252";
??????? else if(maskBit == 31)
??????????? return "255.255.255.254";
??????? else if(maskBit == 32)
??????????? return "255.255.255.255";
??????? return "";
??? }
???
??? //判斷IP是否合法
??? public boolean isValidIP(String ip) {
??????? if(ip.indexOf(".") == -1)
??????????? return false;
??????? String[] ipSplit = ip.split("\\.");
??????? int ipNum = 0;
??????? if (ipSplit.length != 4)
??????????? return false;
??????? for (int i = 0; i < ipSplit.length; i++) {
??????????? try {
??????????????? ipNum = Integer.parseInt(ipSplit[i]);
??????????? }catch(Exception e) {
??????????????? return false;
??????????? }
??????????? if(ipNum < 0 || ipNum > 255)
??????????????? return false;
??????????? if(i == 0)
??????????????? if(ipNum == 0 || ipNum == 255)
??????????????? return false;
??????? }
??????? return true;
??? }
???
??? //判斷子網掩碼是否合法
??? public boolean isValidMask(String mask) {
??????? int maskNum = 0;
??????? int maskBit = 0;
??????? //十進制掩碼
??????? if(mask.indexOf(".") == -1) {
??????????? try {
??????????????? maskBit = Byte.parseByte(mask);
??????????? }catch(Exception e) {
??????????????? return false;
??????????? }
??????????? if(maskBit > 31 || maskBit < 1) {
??????????????? return false;
??????????? }
??????????? return true;
??????? }
??????? //IP格式掩碼
??????? String[] maskSplit = mask.split("\\.");
??????? String maskBinString = "";
??????? if(maskSplit.length != 4)
??????????? return false;
??????? //將大于128的4個掩碼段連成2進制字符串
??????? for(int i=0; i<maskSplit.length; i++) {
??????????? try {
??????????????? maskNum = Integer.parseInt(maskSplit[i]);
??????????? }catch(Exception e) {
??????????????? return false;
??????????? }
??????????? //首位為0,非法掩碼
??????????? if(i == 0 && Integer.numberOfLeadingZeros(maskNum) == 32)
??????????????? return false;
??????????? //非0或128~255之間,非法掩碼
??????????? if(Integer.numberOfLeadingZeros(maskNum) != 24)
??????????????? if(Integer.numberOfLeadingZeros(maskNum) != 32)
??????????????????? return false;
??????????? //將大于128的掩碼段連接成完整的二進制字符串
??????????? maskBinString = maskBinString.concat(Integer.toBinaryString(maskNum));
??????? }
??????? //二進制掩碼字符串,包含非連續1時,非法掩碼
??????? if(maskBinString.indexOf("0") < maskBinString.lastIndexOf("1"))
??????????????? return false;
??????? //剩下的就是合法掩碼
??????? return true;
??? }
???
??? //識別掩碼位數
??? public int getMaskBit(String binaryMask) {
??????? return binaryMask.lastIndexOf("1") + 1;
??? }
???
??? //過濾空格
??? public String deSpace(JTextField textField) {
??????? String curStr = null;
??????? String outStr = "";
??????????? for (int i = 0; i < textField.getText().length(); i++) {
??????????????? curStr = textField.getText().substring(i, i + 1);
??????????????? if (!curStr.equals(" ")) {
??????????????????? outStr += curStr;
??????????????? }
??????????? }
??????????? return outStr;
??????? }
???
}
* @author 盧偉
* @version 1.0 2008-03-06
*
*/
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
public class IPMask {
??? public static void main(String args[]) {
??????????? IPMaskFrame frame = new IPMaskFrame();
??????????? frame.init();
??? }
}
??? /**
??? * 主界面
??? */
class IPMaskFrame extends JFrame implements ActionListener, KeyListener {
??? private static final long serialVersionUID = 1L;
??? //創建窗口對象
??? private JLabel addrLabel = new JLabel("IP地址:");
??? private JTextField addrField = new JTextField(9);
??? private JLabel maskLabel = new JLabel("子網掩碼:");
??? private JTextField maskField = new JTextField(9);
??? private JPanel outputPanel = new JPanel();
??? private JTextArea outputArea = new JTextArea(7, 31);
??? private JButton calculateButton = new JButton("計算");
??? private JButton copyButton = new JButton("復制");
??? private JButton resetButton = new JButton("重置");
??? private JButton helpButton = new JButton("幫助");
??? private Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
??? //關于對話框
??? private JLabel about = new JLabel("<html>      子網掩碼計算器ver1.2.5 (2008-03-9)<br><br>該工具采用的計算公式是:<br>    網絡地址 = IP地址 & 子網掩碼<br>    廣播地址 = ~廣播地址+256 | IP地址<br>    可用主機數 = pow(2, 主機位數)-2<br>如果口算,可用如下快速計算公式:<br>     A = 256 -異常掩碼<br>     B = 異常掩碼對應的IP<br>    網絡地址 = A*n(取最接近于B但小于B的值)<br>    廣播地址 = 網絡地址+A-1<br><br>???????     作者:地圖<br>???????     郵件:godmap@sohu.com</html>");
??? private JLabel url = new JLabel(("<html>???????     博客:<a href=\"http://hi.baidu.com/godmap\">http://hi.baidu.com/godmap</a><br></html>"));
??? private JPanel aboutPanel = new JPanel();
??? private String os = System.getProperty ("os.name");
??? //初始化窗口
??? public void init() {
??????? //設置窗口組件基本屬性
??????? this.setTitle("子網掩碼計算器V1.2.5");
??????? addrField.setToolTipText("格式:172.168.1.2 ……");
??????? addrField.setActionCommand("addr");
??????? addrField.addActionListener(this);
??????? addrField.addKeyListener(this);
??????? maskField.setToolTipText("格式:1~31或255.255.255.0 ……");
??????? maskField.setActionCommand("mask");
??????? maskField.addActionListener(this);
??????? maskField.addKeyListener(this);
??????? calculateButton.setToolTipText("計算結果。");
??????? calculateButton.setActionCommand("calculate");
??????? calculateButton.addActionListener(this);
??????? copyButton.setToolTipText("將計算結果復制到系統剪貼板。");
??????? copyButton.setActionCommand("copy");
??????? copyButton.addActionListener(this);
??????? resetButton.setToolTipText("將輸入框清零。");
??????? resetButton.setActionCommand("reset");
??????? resetButton.addActionListener(this);
??????? helpButton.setToolTipText("更多幫助。");
??????? helpButton.setActionCommand("help");
??????? helpButton.addActionListener(this);
??????? outputArea.setToolTipText("Ctrl+A/Ctrl+C復制計算結果。");
??????? outputArea.setEditable(false);
??????? outputPanel.setBorder(new TitledBorder("計算結果"));
??????? outputPanel.add(outputArea);
??????? JScrollPane scrollBar = new JScrollPane(outputArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
??????? outputPanel.add(scrollBar);
??????? //設置關于窗口屬性
??????? url.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
??????? url.addMouseListener(new MouseListener() {
??????????? public void mouseClicked(MouseEvent e) {
??????????????? if (os.startsWith ("Windows")) {
??????????????????? try {
??????????????????????? Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler http://hi.baidu.com/godmap");
??????????????????? } catch (Exception urlException) {
??????????????????????? System.out.println("啟動瀏覽器錯誤:".concat(urlException.toString().concat("\n請手動啟動瀏覽器訪問:http://hi.baidu.com/godmap")));
??????????????????? }
??????????????? }
??????????????? else
??????????????????? try {
??????????????????????? Runtime.getRuntime().exec("mozila http://hi.baidu.com/godmap");
??????????????????? } catch (Exception urlException) {
??????????????????????? System.out.println("啟動瀏覽器錯誤:".concat(urlException.toString().concat("\n請手動啟動瀏覽器訪問:http://hi.baidu.com/godmap")));
??????????????????? }
??????????? }
??????????? public void mouseEntered(MouseEvent e) {
??????????? }
??????????? public void mouseExited(MouseEvent e) {
??????????? }
??????????? public void mousePressed(MouseEvent e) {
??????????? }
??????????? public void mouseReleased(MouseEvent e) {
??????????? }
??????? });
??????? GroupLayout aboutLayout = new GroupLayout(aboutPanel);
??????? aboutLayout.setHorizontalGroup(aboutLayout.createParallelGroup(GroupLayout.Alignment.CENTER)
??????????????? .addComponent(about)
??????????????? .addComponent(url));
??????? aboutLayout.setVerticalGroup(aboutLayout.createSequentialGroup()
??????????????? .addComponent(about)
??????????????? .addComponent(url));
??????? aboutPanel.setLayout(aboutLayout);
??????? aboutPanel.add(about);
??????? aboutPanel.add(url);
??????? //添加布局管理器
??????? GroupLayout groupLayout = new GroupLayout(getContentPane());
??????? groupLayout.setAutoCreateContainerGaps(true);
??????? groupLayout.setAutoCreateGaps(true);
??????? groupLayout.setHorizontalGroup(
??????????? groupLayout.createParallelGroup(GroupLayout.Alignment.CENTER)
??????????????? .addGroup(groupLayout.createParallelGroup()
??????????????????? .addGroup(groupLayout.createSequentialGroup()
??????????????????????? .addComponent(addrLabel)
??????????????????????? .addComponent(addrField)
??????????????????????? .addComponent(maskLabel)
??????????????????????? .addComponent(maskField))
??????????????????? .addComponent(outputPanel))
??????????????? .addGroup(groupLayout.createSequentialGroup()
??????????????????? .addComponent(calculateButton)
??????????????????? .addComponent(copyButton)
??????????????????? .addComponent(resetButton)
??????????????????? .addComponent(helpButton))
??????? );
??????? groupLayout.setVerticalGroup(
??????????? groupLayout.createSequentialGroup()
??????????????????? .addGroup(groupLayout.createParallelGroup()
??????????????????????? .addComponent(addrLabel)
??????????????????????? .addComponent(addrField)
??????????????????????? .addComponent(maskLabel)
??????????????????????? .addComponent(maskField))
??????????????????? .addComponent(outputPanel)
??????????????????? .addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.CENTER)
??????????????????????? .addComponent(calculateButton)
??????????????????????? .addComponent(copyButton)
??????????????????????? .addComponent(resetButton)
??????????????????????? .addComponent(helpButton))
??????? );
??????? getContentPane().setLayout(groupLayout);
??????? //輸出窗口
??????? pack();
??????? setResizable(false);
??????? setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
??????? setLocation((screenSize.width-getWidth())/2, (screenSize.height-getHeight())/2);
??????? setVisible(true);
??? }
??? //事件監聽
??? public void actionPerformed(ActionEvent e) {
??????? if(e.getActionCommand().equals("calculate")) {
??????????? outputArea.setText(this.getIPMask(deSpace(addrField), deSpace(maskField)));
??????????? outputArea.setCaretPosition(0);
??????? }
??????? else if(e.getActionCommand().equals("copy")) {
??????????? this.outputArea.selectAll();
??????????? this.outputArea.copy();
??????? }
??????? else if(e.getActionCommand().equals("reset")) {
??????????? this.addrField.setText("");
??????????? this.maskField.setText("");
??????? }
??????? else if(e.getActionCommand().equals("help")) {
??????????????? JOptionPane.showMessageDialog(null, aboutPanel, "關于", JOptionPane.INFORMATION_MESSAGE );
??????? }
??????? else ;
??? }
??? //鍵盤監聽
??? public void keyPressed(KeyEvent e) {
??? }
??? public void keyReleased(KeyEvent e) {
??????? if(e.getKeyCode()==10) {
??????????? this.calculateButton.doClick();
??????? }
??? }
??? public void keyTyped(KeyEvent e) {
??? }
???
??? //主方法
??? public String getIPMask(String ipAddr, String maskAddr) {
??????? String outputMaskInfo = "非法IP或子網掩碼地址。";
??????? //非法IP或子網掩碼地址,不進行計算。
??????? if(!isValidIP(ipAddr) || !isValidMask(maskAddr))
??????????? return outputMaskInfo;
??????? //合法IP、子網掩碼,開始計算。
??????? if(maskAddr.indexOf(".") == -1)
??????????? maskAddr = getMask(Byte.parseByte(maskAddr));
??????? String[] ipSplit = {"0","0","0","0"};
??????? String[] maskSplit = {"0","0","0","0"};
??????? ipSplit = ipAddr.split("\\.");
??????? maskSplit = maskAddr.split("\\.");
??????? String ip = "";
??????? String mask = "";
??????? String netIP = "";
??????? String broadcastIP = "";
??????? String startIP = "";
??????? String endIP = "";
??????? String binaryMask = "";
??????? String addrType = "";
??????? //String hostIP = "";
??????? int hostNum = 0;
??????? //int subNetNum = 0;
??????? int subNetMaxNum = 0;
???????
??????? for(int i = 0; i < 4; i++) {
??????????? int ipTemp = Integer.parseInt(ipSplit[i]);
??????????? int maskTemp = Integer.parseInt(maskSplit[i]);
??????????? //地址類型
??????????? if(i == 0) {
??????????????? if(ipTemp == 127)
??????????????????? addrType = "回環地址:";
??????????????? else if(ipTemp < 127)
??????????????????? addrType = "A類地址:";
??????????????? else if(ipTemp < 192)
??????????????????? addrType = "B類地址:";
??????????????? else if(ipTemp < 224)
??????????????????? addrType = "C類地址:";
??????????????? else if(ipTemp < 240)
??????????????????? addrType = "D類(組播)地址:";
??????????????? else if(ipTemp < 255)
??????????????????? addrType = "E類(保留)地址:";
??????????? }
??????????? //用戶輸入的IP
??????????? ip = ip.concat(Integer.toString(ipTemp)).concat(".");
??????????? //用戶輸入的子網掩碼
??????????? mask = mask.concat(Integer.toString(maskTemp)).concat(".");
??????????? //網絡地址
??????????? netIP = netIP.concat(Integer.toString(ipTemp & maskTemp)).concat(".");
??????????? //廣播地址
??????????? broadcastIP = broadcastIP.concat(Integer.toString(~maskTemp+256 | ipTemp)).concat(".");
??????????? //主機地址
??????????? //hostIP = hostIP.concat(Integer.toString(~maskTemp & ipTemp)).concat(".");
??????????? //可分配主機地址
??????????? if(i < 3) {
??????????????? startIP = startIP.concat(Integer.toString(ipTemp & maskTemp)).concat(".");
??????????????? endIP = endIP.concat(Integer.toString(~maskTemp+256 | ipTemp)).concat(".");
??????????? }
??????????? else if(i == 3) {
??????????????? if(maskTemp != 254) {
??????????????????? startIP = startIP.concat(Integer.toString((ipTemp & maskTemp) + 1)).concat(".");
??????????????????? endIP = endIP.concat(Integer.toString((~maskTemp+256 | ipTemp) - 1)).concat(".");
??????????????? }
??????????????? else {
??????????????????? startIP = "無.";
??????????????????? endIP = "無.";
??????????????? }
??????????? }
??????????? //生成連續的二進制子網掩碼,以計算可用主機數
??????????? binaryMask = binaryMask.concat(Integer.toBinaryString(maskTemp));
??????? }
??????? //可用主機數
??????? hostNum = (int)Math.pow(2, 32 - this.getMaskBit(binaryMask)) - 2;
??????? //可劃分子網數
??????? subNetMaxNum = (int)(32-getMaskBit(binaryMask)-2 > 0 ? Math.pow(2, 32-getMaskBit(binaryMask)-2):0);
??????? //計算子網
??????? outputMaskInfo = addrType.concat(ip.substring(0, ip.length() - 1)).concat("/").concat(mask.substring(0, mask.length() - 1))
??????????? .concat("的\n網絡地址是:").concat(netIP.substring(0, netIP.length() - 1))
??????????? .concat("\n廣播地址是:".concat(broadcastIP.substring(0, broadcastIP.length() - 1))
??????????? .concat("\n可分配主機地址包括:").concat(startIP.substring(0, startIP.length() - 1)).concat("~").concat(endIP.substring(0, endIP.length() - 1))
??????????? .concat("\n可用主機數共:").concat(Integer.toString(hostNum)).concat("臺")
??????????? .concat("\n最多可劃分:")??? .concat(Integer.toString(subNetMaxNum).concat("個子網\n")));
??????????? //.concat(this.getIPMask(false)));
??????? return outputMaskInfo;
??? }
???
??? //計算子網分配方案
??? public String getSubNet() {
??????? String subNetInfo = "子網分配方案如下:\n";
??????? for(int i=0; i<this.getIPMask("","").indexOf("a"); i++) {
???????????
??????? }
??????? return subNetInfo;
??? }
???
??? //轉換十進制掩碼為IP地址格式掩碼
??? public String getMask(byte maskBit) {
??????? if(maskBit == 1)
??????????? return "128.0.0.0";
??????? else if(maskBit == 2)
??????????? return "192.0.0.0";
??????? else if(maskBit == 3)
??????????? return "224.0.0.0";
??????? else if(maskBit == 4)
??????????? return "240.0.0.0";
??????? else if(maskBit == 5)
??????????? return "248.0.0.0";
??????? else if(maskBit == 6)
??????????? return "252.0.0.0";
??????? else if(maskBit == 7)
??????????? return "254.0.0.0";
??????? else if(maskBit ==

??????????? return "255.0.0.0";
??????? else if(maskBit ==9)
??????????? return "255.128.0.0";
??????? else if(maskBit == 10)
??????????? return "255.192.0.0";
??????? else if(maskBit == 11)
??????????? return "255.224.0.0";
??????? else if(maskBit == 12)
??????????? return "255.240.0.0";
??????? else if(maskBit == 13)
??????????? return "255.248.0.0";
??????? else if(maskBit == 14)
??????????? return "255.252.0.0";
??????? else if(maskBit == 15)
??????????? return "255.254.0.0";
??????? else if(maskBit == 16)
??????????? return "255.255.0.0";
??????? else if(maskBit == 17)
??????????? return "255.255.128.0";
??????? else if(maskBit == 18)
??????????? return "255.255.192.0";
??????? else if(maskBit == 19)
??????????? return "255.255.224.0";
??????? else if(maskBit == 20)
??????????? return "255.255.240.0";
??????? else if(maskBit == 21)
??????????? return "255.255.248.0";
??????? else if(maskBit == 22)
??????????? return "255.255.252.0";
??????? else if(maskBit == 23)
??????????? return "255.255.254.0";
??????? else if(maskBit == 24)
??????????? return "255.255.255.0";
??????? else if(maskBit == 25)
??????????? return "255.255.255.128";
??????? else if(maskBit == 26)
??????????? return "255.255.255.192";
??????? else if(maskBit == 27)
??????????? return "255.255.255.224";
??????? else if(maskBit == 28)
??????????? return "255.255.255.240";
??????? else if(maskBit == 29)
??????????? return "255.255.255.248";
??????? else if(maskBit == 30)
??????????? return "255.255.255.252";
??????? else if(maskBit == 31)
??????????? return "255.255.255.254";
??????? else if(maskBit == 32)
??????????? return "255.255.255.255";
??????? return "";
??? }
???
??? //判斷IP是否合法
??? public boolean isValidIP(String ip) {
??????? if(ip.indexOf(".") == -1)
??????????? return false;
??????? String[] ipSplit = ip.split("\\.");
??????? int ipNum = 0;
??????? if (ipSplit.length != 4)
??????????? return false;
??????? for (int i = 0; i < ipSplit.length; i++) {
??????????? try {
??????????????? ipNum = Integer.parseInt(ipSplit[i]);
??????????? }catch(Exception e) {
??????????????? return false;
??????????? }
??????????? if(ipNum < 0 || ipNum > 255)
??????????????? return false;
??????????? if(i == 0)
??????????????? if(ipNum == 0 || ipNum == 255)
??????????????? return false;
??????? }
??????? return true;
??? }
???
??? //判斷子網掩碼是否合法
??? public boolean isValidMask(String mask) {
??????? int maskNum = 0;
??????? int maskBit = 0;
??????? //十進制掩碼
??????? if(mask.indexOf(".") == -1) {
??????????? try {
??????????????? maskBit = Byte.parseByte(mask);
??????????? }catch(Exception e) {
??????????????? return false;
??????????? }
??????????? if(maskBit > 31 || maskBit < 1) {
??????????????? return false;
??????????? }
??????????? return true;
??????? }
??????? //IP格式掩碼
??????? String[] maskSplit = mask.split("\\.");
??????? String maskBinString = "";
??????? if(maskSplit.length != 4)
??????????? return false;
??????? //將大于128的4個掩碼段連成2進制字符串
??????? for(int i=0; i<maskSplit.length; i++) {
??????????? try {
??????????????? maskNum = Integer.parseInt(maskSplit[i]);
??????????? }catch(Exception e) {
??????????????? return false;
??????????? }
??????????? //首位為0,非法掩碼
??????????? if(i == 0 && Integer.numberOfLeadingZeros(maskNum) == 32)
??????????????? return false;
??????????? //非0或128~255之間,非法掩碼
??????????? if(Integer.numberOfLeadingZeros(maskNum) != 24)
??????????????? if(Integer.numberOfLeadingZeros(maskNum) != 32)
??????????????????? return false;
??????????? //將大于128的掩碼段連接成完整的二進制字符串
??????????? maskBinString = maskBinString.concat(Integer.toBinaryString(maskNum));
??????? }
??????? //二進制掩碼字符串,包含非連續1時,非法掩碼
??????? if(maskBinString.indexOf("0") < maskBinString.lastIndexOf("1"))
??????????????? return false;
??????? //剩下的就是合法掩碼
??????? return true;
??? }
???
??? //識別掩碼位數
??? public int getMaskBit(String binaryMask) {
??????? return binaryMask.lastIndexOf("1") + 1;
??? }
???
??? //過濾空格
??? public String deSpace(JTextField textField) {
??????? String curStr = null;
??????? String outStr = "";
??????????? for (int i = 0; i < textField.getText().length(); i++) {
??????????????? curStr = textField.getText().substring(i, i + 1);
??????????????? if (!curStr.equals(" ")) {
??????????????????? outStr += curStr;
??????????????? }
??????????? }
??????????? return outStr;
??????? }
???
}
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
