📅  最后修改于: 2020-10-01 03:40:34             🧑  作者: Mango
带有源代码的Java中的IP Finder:我们可以借助联网,带有事件处理的AWT / Swing来开发Java中的IP Finder。让我们看看在Java中创建IP Finder的代码。
String url="www.javatpoint.com";
InetAddress ia=InetAddress.getByName(url);
String ip=ia.getHostAddress();
让我们看一下用于查找IP地址的Swing代码。
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
public class IPFinder extends JFrame implements ActionListener{
JLabel l;
JTextField tf;
JButton b;
IPFinder(){
super("IP Finder Tool - Javatpoint");
l=new JLabel("Enter URL:");
l.setBounds(50,70,150,20);;
tf=new JTextField();
tf.setBounds(50,100,200,20);
b=new JButton("Find IP");
b.setBounds(50,150,80,30);
b.addActionListener(this);
add(l);
add(tf);
add(b);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
String url=tf.getText();
try {
InetAddress ia=InetAddress.getByName(url);
String ip=ia.getHostAddress();
JOptionPane.showMessageDialog(this,ip);
} catch (UnknownHostException e1) {
JOptionPane.showMessageDialog(this,e1.toString());
}
}
public static void main(String[] args) {
new IPFinder();
}
}