import javax.swing.; import java.awt.; import java.awt.event.; import java.io.;

public class SubnetCalculatorGUI extends JFrame implements ActionListener { private JTextField ipField, subnetNumField; private JLabel ipLabel, subnetNumLabel, resultLabel; private JButton calculateButton;

public SubnetCalculatorGUI() {
    // 设置窗口标题
    setTitle('子网划分计算器');
    // 设置窗口大小
    setSize(500, 300);
    // 设置窗口位置
    setLocationRelativeTo(null);
    // 设置窗口布局
    setLayout(new GridLayout(3, 2, 10, 10));
    // 设置窗口关闭方式
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // 添加IP地址输入框和标签
    ipLabel = new JLabel('请输入要划分的IP地址(格式为:x1.x2.x3.x4):');
    ipField = new JTextField();
    add(ipLabel);
    add(ipField);

    // 添加子网数输入框和标签
    subnetNumLabel = new JLabel('请输入要划分的子网数:');
    subnetNumField = new JTextField();
    add(subnetNumLabel);
    add(subnetNumField);

    // 添加计算按钮和结果标签
    calculateButton = new JButton('计算');
    calculateButton.addActionListener(this);
    resultLabel = new JLabel();
    add(calculateButton);
    add(resultLabel);

    // 显示窗口
    setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
    // 获取输入的IP地址和子网数
    String ip = ipField.getText();
    String subnetNumStr = subnetNumField.getText();

    // 根据输入的IP地址和子网数计算超网信息
    try {
        String result = calculate(ip, subnetNumStr);
        resultLabel.setText(result);
    } catch (IOException ex) {
        JOptionPane.showMessageDialog(this, '计算出错:' + ex.getMessage(), '错误', JOptionPane.ERROR_MESSAGE);
    }
}

private String calculate(String ip, String subnetNumStr) throws IOException {
    int s; // 用于接收以要划分的子网数
    int n; // 输入子网数对应的二进制位数

    // 截取x1
    int index1 = ip.indexOf('.');
    String st1 = ip.substring(0, index1);
    int x1 = Integer.parseInt(st1); // 把String类型的st转化成int型
    // 截取x2
    int index2 = ip.indexOf('.', index1 + 1);
    String st2 = ip.substring(index1 + 1, index2);
    int x2 = Integer.parseInt(st2); // 把String类型的st转化成int型
    // 截取x3
    int index3 = ip.indexOf('.', index2 + 1);
    String st3 = ip.substring(index2 + 1, index3);
    int x3 = Integer.parseInt(st3); // 把String类型的st转化成int型

    StringBuilder sb = new StringBuilder();

    // A类
    if (x1 > 0 && x1 <= 126) {
        sb.append('本IP地址能划分划分的最大子网数为: ').append((int) Math.pow(2, 24 - 2) - 2).append('

'); } // B类 if (x1 >= 128 && x1 <= 191) { sb.append('本IP地址能划分划分的最大子网数为: ').append((int) Math.pow(2, 16 - 2) - 2).append(' '); } // C类 if (x1 >= 192 && x1 <= 223) { sb.append('本IP地址能划分划分的最大子网数为: ').append((int) Math.pow(2, 8 - 2) - 2).append(' '); }

    // 求所输入子网数对应的二进制位数:n
    s = Integer.parseInt(subnetNumStr);
    for (n = 1; (Math.pow(2, n) - 2) < s; n++) ;
    sb.append('子网掩码为:

'); // A类 if (x1 > 0 && x1 <= 126) { if (n <= 8) { sb.append('255.').append(getSum(n)).append('.0.0 '); } else if (n > 8 && n <= 16) { n = n - 8; sb.append('255.255.').append(getSum(n)).append('.0 '); } else { n = n - 16; sb.append('255.255.255.').append(getSum(n)).append(' '); } } // B类 if (x1 >= 128 && x1 <= 191) { if (n <= 8) { sb.append('255.255.').append(getSum(n)).append('.0 '); } else { n = n - 8; sb.append('255.255.255.').append(getSum(n)).append(' '); } } // C类 if (x1 >= 192 && x1 <= 223) { sb.append('255.255.255.').append(getSum(n)).append(' '); }

    // 求每网段主机数
    sb.append('每网段主机数:

'); // A类 if (x1 > 0 && x1 <= 126) { sb.append((int) Math.pow(2, (32 - n)) - 2).append(' '); } // B类 if (x1 >= 128 && x1 <= 191) { sb.append((int) Math.pow(2, (16 - n)) - 2).append(' '); } // C类 if (x1 >= 192 && x1 <= 223) { sb.append((int) Math.pow(2, (8 - n)) - 2).append(' '); }

    // 输出各子网IP范围
    sb.append('各子网IP范围如下:

'); // A类 if (x1 > 0 && x1 <= 126) { if (n <= 8) { for (int i = 1, sub_id = 0; i < Math.pow(2, n) - 1; i++) { sub_id = (int) Math.pow(2, (8 - n)) * i; sb.append('<').append(i).append('>').append(' '); sb.append(x1).append('.').append(sub_id).append('.0.').append(1).append('---').append(x1).append('.').append((int) (sub_id + Math.pow(2, (8 - n)) - 1)).append('.').append(255).append('.').append(254).append(' '); } } if (n > 8 && n <= 16) { int subnet_id = 0; // 定义子网号 for (int i = 0; i < Math.pow(2, 8); i++) { if (i == 0) { for (int k = 1, sub_id = 0; k <= Math.pow(2, n - 8) - 1; k++) { sub_id = (int) Math.pow(2, (16 - n)) * k; sb.append('<').append(++subnet_id).append('>').append(' '); sb.append(x1).append('.').append(i).append('.').append(sub_id).append('.').append(1).append('------').append(x1).append('.').append(i).append('.').append((sub_id + (int) Math.pow(2, (16 - n)))).append('.').append(254).append(' '); } } else { for (int k = 0, sub_id = 0; k <= Math.pow(2, n - 8) - 1; k++) { if ((i == 255) && k == (Math.pow(2, n - 8) - 1)) { break; } sub_id = (int) Math.pow(2, (16 - n)) * k; sb.append('<').append(++subnet_id).append('>').append(' '); sb.append(x1).append('.').append(i).append('.').append((sub_id)).append('.').append(1).append('------').append(x1).append(i).append('.').append((sub_id + (int) Math.pow(2, (16 - n)))).append('.').append(254).append(' '); } } } } if (n > 16 && n < 24) { int subnet_id = 0; // 定义子网号 for (int m = 0; m < Math.pow(2, 8); m++) { if (m == 0) { for (int i = 0; i < Math.pow(2, 8); i++) { if (i == 0) { for (int k = 1, sub_id = 0; k <= Math.pow(2, n - 16) - 1; k++) { sub_id = (int) Math.pow(2, (24 - n)) * k; sb.append('<').append(++subnet_id).append('>').append(' '); sb.append(x1).append('.').append(m).append('.').append(i).append('.').append((sub_id + 1)).append('------').append(x1).append('.').append(m).append('.').append(i).append('.').append((sub_id + (int) Math.pow(2, (24 - n)) - 2)).append(' '); } } else { for (int k = 0, sub_id = 0; k <= Math.pow(2, n - 16) - 1; k++) { sub_id = (int) Math.pow(2, (24 - n)) * k; sb.append('<').append(++subnet_id).append('>').append(' '); sb.append(x1).append('.').append(m).append('.').append(i).append('.').append((sub_id + 1)).append('------').append(x1).append('.').append(m).append('.').append(i).append('.').append((sub_id + (int) Math.pow(2, (24 - n)) - 2)).append(' '); } } } } else { for (int i = 0; i < Math.pow(2, 8); i++) { for (int k = 0, sub_id = 0; k <= Math.pow(2, n - 16) - 1; k++) { if (m == 255 && (i == 255) && k == (Math.pow(2, n - 16) - 1)) { break; } sub_id = (int) Math.pow(2, (24 - n)) * k; sb.append('<').append(++subnet_id).append('>').append(' '); sb.append(x1).append('.').append(m).append('.').append(i).append('.').append((sub_id + 1)).append('------').append(x1).append('.').append(m).append('.').append(i).append('.').append((sub_id + (int) Math.pow(2, (24 - n)) - 2)).append(' '); } } } } } } // B类 if (x1 >= 128 && x1 <= 191) { if (n <= 8) { for (int i = 1, sub_id = 0; i < Math.pow(2, n) - 1; i++) { sub_id = (int) Math.pow(2, (8 - n)) * i; sb.append('<').append(i).append('>').append(' '); sb.append(x1).append('.').append(x2).append('.').append(sub_id).append('.').append(1).append('---').append(x1).append('.').append(x2).append('.').append((int) (sub_id + Math.pow(2, (8 - n)) - 1)).append('.').append(254).append(' '); } } if (n > 8 && n < 16) { int subnet_id = 0; // 定义子网号 for (int i = 0; i < Math.pow(2, 8); i++) { if (i == 0) { for (int k = 1, sub_id = 0; k <= Math.pow(2, n - 8) - 1; k++) { sub_id = (int) Math.pow(2, (16 - n)) * k; sb.append('<').append(++subnet_id).append('>').append(' '); sb.append(x1).append('.').append(x2).append('.').append(i).append('.').append((sub_id + 1)).append('------').append(x1).append('.').append(x2).append('.').append(i).append('.').append((sub_id + (int) Math.pow(2, (16 - n)) - 2)).append(' '); } } else { for (int k = 0, sub_id = 0; k <= Math.pow(2, n - 8) - 1; k++) { if ((i == 255) && k == (Math.pow(2, n - 8) - 1)) { break; } sub_id = (int) Math.pow(2, (16 - n)) * k; sb.append('<').append(++subnet_id).append('>').append(' '); sb.append(x1).append('.').append(x2).append('.').append(i).append('.').append((sub_id + 1)).append('------').append(x1).append('.').append(x2).append('.').append(i).append('.').append((sub_id + (int) Math.pow(2, (16 - n)) - 2)).append(' '); } } } } } // C类 if (x1 >= 192 && x1 <= 223) { for (int i = 1, sub_id = 0; i < Math.pow(2, n) - 1; i++) { sub_id = (int) Math.pow(2, (8 - n)) * i; sb.append('<').append(i).append('>').append(' '); sb.append(x1).append('.').append(x2).append('.').append(x3).append('.').append((sub_id + 1)).append('---').append(x1).append('.').append(x2).append('.').append(x3).append('.').append(((int) sub_id + Math.pow(2, (8 - n)) - 2)).append(' '); } } sb.append('子网划分完毕 '); return sb.toString(); }

private int getSum(int n) {
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum = sum + (int) Math.pow(2, (7 - i));
    }
    return sum;
}

public static void main(String[] args) {
    new SubnetCalculatorGUI();
}

}

子网划分计算器:超网计算和图形界面

原文地址: https://www.cveoy.top/t/topic/ocvY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录