Use Java to obtain native IP and machine names

Source: Internet
Author: User
Iplabel.java


import java.net.*;


import java.awt.*;


import javax.swing.*;


public class Iplabel


{


JFrame frame;


JLabel Label1,label2;


JPanel Panel;


String ip,address;





public void GetIP ()


{


Try


{


inetaddress addr = Inetaddress.getlocalhost ();


ip=addr.gethostaddress (). toString ();//Get native IP


address=addr.gethostname (). toString ();//Get native name








//system.out.println ("addr=:" +string.valueof (addr));


}


catch (Exception e)


{


System.out.println ("Bad IP address!" +E);


}


}





public void Showframe ()


{


frame=new JFrame ("My IP");


label1=new JLabel ("This My IP");


label1.settext (IP);


label2=new JLabel ("This my Address");


Label2.settext (address);


panel=new JPanel ();


Panel.add (Label1);


Panel.add (Label2);


Frame.getcontentpane (). Add (panel);





frame.setsize (400,300);


frame.setvisible (TRUE);


}





public static void Main (String agrs[])


{


Iplabel myip=new Iplabel ();


Myip.getip ();


Myip.showframe ();





}


}














Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.