您的位置:首页 > 其它

根据获取的主机名显示ip地址

2013-01-01 14:51 363 查看
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.net.*;
public class IPTest extends JFrame implements ActionListener
{
JLabel lb=new JLabel("显示IP地址");
JTextField text=new JTextField(12);
JButton btn=new JButton("单击本按钮,显示IP地址");
IPTest()
{
JFrame frame=new JFrame("IP显示");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout border=new BorderLayout();
frame.setLayout(border);
frame.add(lb,BorderLayout.NORTH);
frame.add(text,BorderLayout.CENTER);
frame.add(btn,BorderLayout.SOUTH);
btn.addActionListener(this);
frame.setLocation(new Point(300, 300));
frame.setSize(300,200);
frame.validate();
frame.setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
try
{
InetAddress local=InetAddress.getByName(text.getText().trim());
lb.setText(local.getHostAddress().toString());

}
catch(Exception ex)
{
JOptionPane.showMessageDialog(this, "无法识别的输入的主机名","警告对话框",JOptionPane.WARNING_MESSAGE);
}

}

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

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: