JAVA笔记 关于IP的小程序

JAVA笔记 这个我的写的一个自己获得本机IP和获得其它网站的IP的一个小程序希望大家来看看 还有什么要补的。
请在输入框里输入网址: 比如:www.baidu.com ;

import java.net.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Ipserver11 extends JFrame implements ActionListener{
      private InetAddress ServerIP ;
      private InetAddress LocalIP ;
    
JTextField t_tr = new JTextField() ;
JTextField t_hd = new JTextField() ;

InetAddress uip ;
InetAddress lip ;
Ipserver11() {
    //设置窗体的属性
    this.setSize(300 ,180 ) ;
    this.setResizable(false) ;
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
  
    //设置窗体剧中
    int width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth() ;
    int height = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth() ;
    this.setLocation((width-300)/2 , (height-180)/2 ) ;
  
    //新建一大堆组件
    JLabel l_from = new JLabel("请输入网址:") ;
  

  
    JButton b_hd = new JButton("获得IP");
    JButton b_local = new JButton("获得本机IP") ;
  
    //注册监听器
    b_hd.addActionListener(this) ;
    b_local.addActionListener(this) ;  
  
    //设置输入面板
    JPanel p_input = new JPanel() ;
    p_input.setLayout(new GridLayout()) ;
  
    p_input.add(l_from) ;
    p_input.add(t_tr) ;
  
    //设置获得面板
    JPanel p_button = new JPanel() ;
    p_button.setLayout(new FlowLayout() ) ;
  
    p_button.add(b_hd) ;
    p_button.add(b_local) ;
  
    //布置面板
    this.setLayout(new BorderLayout()) ;
  
    this.add(p_input , BorderLayout.NORTH) ;
    this.add(t_hd , BorderLayout.CENTER) ;
    this.add(p_button , BorderLayout.SOUTH) ;
  
}
public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true) ;
    Ipserver11 is = new Ipserver11() ;
    is.setVisible(true) ;
  
    //System.out.println("ServerIP ip is :" +is.CatchServerIP()) ;
}

public InetAddress CatchServerIP(){
    try{
       ServerIP = InetAddress.getByName(t_tr.getText()) ;
      //获得服务器IP
    }catch(Exception ex){
     JOptionPane.showMessageDialog(this," 请输入正确地址: !!!!") ;
    }
    return ServerIP ;
}

//获得本机IP
public InetAddress CatchLocalIP(){
    try{
     LocalIP = InetAddress.getLocalHost() ;
    }catch(Exception ex){
     ex.printStackTrace() ;
    }
    return LocalIP ;
  
}

public void actionPerformed(ActionEvent e) {
    if(e.getActionCommand().equals("获得IP")){
   
     try{
      String st = t_tr.getText() ;
      if(st.equals(" localhost")){
       JOptionPane.showConfirmDialog(this,"dd") ;
      }
      uip = this.CatchServerIP() ;
    
       t_hd.setText(uip.toString()) ;
     }catch(Exception ex){
      ex.printStackTrace() ;
     }
    }
  
    if(e.getActionCommand().equals("获得本机IP")){
     try{
//System.out.print("dd") ;
      lip = this.CatchLocalIP() ;
      t_hd.setText(lip.toString()) ;
    
     }catch(Exception ex){
      ex.printStackTrace() ;
     }
    }
   
}
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值