最近在学习java,,一步一步往上爬,,感觉自己的头脑不怎么适合编程,但学了这个,就得踏实走下去,不然以后拿什么吃饭。。所以还是乖乖滴从编小程序练手开始,,希望有一天自己不再是菜鸟。。。。。

     以前学过C和C++,接触过JAVA后感觉自己的兴趣来了,,因为java可以编些小小的软件,难度不是很大。。。下面是一个ip获取器,功能胡乱胡乱滴(工具Netbeans)..。

 

 

界面设计如图,下面是主要功能的代码:

 

 
  
  1. import java.awt.event.KeyEvent;  
  2. import java.io.IOException;  
  3. import java.net.*;  
  4. import java.util.logging.Level;  
  5. import java.util.logging.Logger;  
  6. import javax.swing.JOptionPane;  
  7. public class Main extends javax.swing.JFrame {  
  8.    public Main() {  
  9.         initComponents();  
  10.         setTitle("IP获取器---java制作"); }  
  11. InetAddress myIP=null;  
  12. InetAddress address=null;  
  13.  
  14. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  15. try{  
  16.      myIP=InetAddress.getLocalHost();  }  
  17. catch (UnknownHostException err){  
  18.     JOptionPane.showMessageDialog(null,"无法获取本地地址");  
  19. }  
  20. try{  
  21.      address=InetAddress.getByName(jTextField1.getText());}  
  22. catch (UnknownHostException err){  
  23.     JOptionPane.showMessageDialog(null,"网址输入错误","警告",JOptionPane.WARNING_MESSAGE);}  
  24. jLabel5.setText(myIP.getHostAddress());  
  25. jLabel6.setText(address.getHostAddress());  
  26.  
  27. if((myIP.getAddress()).length==4)  
  28. {  jLabel7.setText("本机IP为ipv4");  
  29.  
  30.    byte[] ipByte=myIP.getAddress();  
  31.    int firstByte=ipByte[0];  
  32.    if(firstByte<0) {firstByte=firstByte+256;}  
  33.    if((firstByte&0x80)==0){ jLabel8.setText("本机IP地址为A类");}  
  34.    else if((firstByte&0xC0)==0x80){jLabel8.setText("本机IP地址为B类");}  
  35.    else if((firstByte&0xE0)==0xC0){jLabel8.setText("本机IP地址为C类");}  
  36.    else if((firstByte&0xF0)==0xE0){jLabel8.setText("本机IP地址为D类");}  
  37.    else if((firstByte&0xF8)==0xF0){jLabel8.setText("本机IP地址为E类");}  
  38. }  
  39. else if((myIP.getAddress()).length==16)  jLabel7.setText("本机IP为ipv6");  
  40.  
  41. if((address.getAddress()).length==4)  
  42. {  jLabel9.setText("网络IP为ipv4");  
  43.  
  44.    byte[] ipByte=address.getAddress();  
  45.    int firstByte=ipByte[0];  
  46.    if(firstByte<0) {firstByte=firstByte+256;}  
  47.    if((firstByte&0x80)==0){ jLabel10.setText("网络IP地址为A类");}  
  48.    else if((firstByte&0xC0)==0x80){jLabel10.setText("网络IP地址为B类");}  
  49.    else if((firstByte&0xE0)==0xC0){jLabel10.setText("网络IP地址为C类");}  
  50.    else if((firstByte&0xF0)==0xE0){jLabel10.setText("网络IP地址为D类");}  
  51.    else if((firstByte&0xF8)==0xF0){jLabel10.setText("网络IP地址为E类");}  
  52. }  
  53. else if((address.getAddress()).length==16)  jLabel9.setText("网络IP为ipv6");  
  54.  
  55. }        
  56.      
  57. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                  //打开此网页                         
  58. try{  
  59.    myIP=InetAddress.getLocalHost();}  
  60. catch(UnknownHostException err){  
  61.     JOptionPane.showMessageDialog(null,"无法获取本地地址");}  
  62. try{  
  63.    address=InetAddress.getByName(jTextField1.getText());}  
  64. catch(UnknownHostException err){  
  65.   JOptionPane.showMessageDialog(null,"网址输入错误","警告",JOptionPane.WARNING_MESSAGE); }  
  66. try{  
  67.    String str=jTextField1.getText();  
  68.    String command="C:\\Program  Files\\Internet  Explorer\\iexplore.exe "+str;   
  69.     Runtime.getRuntime().exec( command);  
  70. }  
  71. catch(IOException err){  
  72.    JOptionPane.showMessageDialog(null,"无效的输入","警告",JOptionPane.WARNING_MESSAGE); }  
  73. jLabel5.setText(myIP.getHostAddress());  
  74. jLabel6.setText(address.getHostAddress());  
  75. }    
  76.                      
  77.