package com.zzk;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class ByIpGainDomainFrame extends JFrame {
private JTextField tf_ip;
private JTextField tf_canonical;
private JTextField tf_host;
/**
* Launch the application
*
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ByIpGainDomainFrame frame = new ByIpGainDomainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public ByIpGainDomainFrame() {
super();
getContentPane().setLayout(null);
setTitle("通过IP地址获得域名和主机名");
setBounds(100, 100, 333, 218);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try {
String ip = tf_ip.getText();// IP地址
String[] ipStr = ip.split("[.]");// IP转换为字符串数组
byte[] ipBytes = new byte[4];// 声明存储转换后IP的字节数组
for (int i = 0; i < 4; i++) {
int m = Integer.parseInt(ipStr[i]);// 转换为整数
byte b = (byte) (m & 0xff);// 转换为字节
ipBytes[i] = b;// 赋值给字节数组
}
InetAddress inetAddr = InetAddress.getByAddress(ipBytes);// 创建InetAddress对象
String canonical = inetAddr.getCanonicalHostName();// 获取域名
String host = inetAddr.getHostName();// 获取主机名
tf_canonical.setText(canonical);// 在文本框中显示域名
tf_host.setText(host);// 在文本框中显示主机名
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
}
});
button.setText("获取域名和主机名");
button.setBounds(28, 136, 150, 28);
getContentPane().add(button);
final JLabel label = new JLabel();
label.setForeground(new Color(0, 0, 255));
label.setFont(new Font("", Font.BOLD, 16));
label.setText("通过IP地址获得域名和主机名");
label.setBounds(51, 10, 223, 35);
getContentPane().add(label);
final JLabel label_1 = new JLabel();
label_1.setText("主 机 名:");
label_1.setBounds(28, 110, 66, 18);
getContentPane().add(label_1);
tf_host = new JTextField();
tf_host.setBounds(87, 108, 199, 22);
getContentPane().add(tf_host);
final JButton button_1 = new JButton();
button_1.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
System.exit(0);
}
});
button_1.setText("退出系统");
button_1.setBounds(191, 136, 95, 28);
getContentPane().add(button_1);
final JLabel label_2 = new JLabel();
label_2.setText("域 名:");
label_2.setBounds(28, 82, 66, 18);
getContentPane().add(label_2);
tf_canonical = new JTextField();
tf_canonical.setBounds(87, 80, 199, 22);
getContentPane().add(tf_canonical);
final JLabel label_3 = new JLabel();
label_3.setText("输入IP地址:");
label_3.setBounds(10, 51, 84, 18);
getContentPane().add(label_3);
tf_ip = new JTextField();
tf_ip.setBounds(87, 52, 199, 22);
getContentPane().add(tf_ip);
//
}
}
通过IP地址获得域名和主机名
需积分: 11 149 浏览量
2012-03-28
14:42:31
上传
评论
收藏 6KB RAR 举报
chinafjfzlj
- 粉丝: 5
- 资源: 165
最新资源
- 基于yolov5识别算法实现的DNF自动脚本源码.zip
- 基于Python实现的自动化办公项目.zip
- 基于python实现的基于PyQt5和爬虫的小说阅读系统.zip
- 机械设计整经机上纱自动化sw20非常好的设计图纸100%好用.zip
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈