44 java获取ip地址 - 雪炭网

java获取ip地址2014-10-20 16:28:05

( 还没有投票,继续加油! )
分享:
31.3K

java获取ip地址代码如下:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
                   
public class ip extends JFrame
 implements ActionListener
{
 private static final long serialVersionUID = 3339481369781127417L;
 JButton jb1;
 JButton jb2;
 JButton jb3;
 JPanel jp;
 JLabel jl;
 JLabel jl1;
 JTextField jt;
                   
 public ip()
 {
  this.jp = new JPanel();
  this.jl = new JLabel();
  this.jl1 = new JLabel("您的域名:");
  this.jb1 = new JButton("提交");
  this.jb2 = new JButton("重置");
  this.jb3 = new JButton("退出");
  this.jt = new JTextField(20);
  this.jb1.addActionListener(this);
  this.jb2.addActionListener(this);
  this.jb3.addActionListener(this);
  this.jp.setLayout(new GridLayout(3, 2));
  this.jp.add(this.jl1);
  this.jp.add(this.jt);
  this.jp.add(this.jb1);
  this.jp.add(this.jl);
  this.jp.add(this.jb2);
  this.jp.add(this.jb3);
                   
  setBounds(200, 200, 500, 240);
  add(this.jp);
  setVisible(true);
  setDefaultCloseOperation(3);
 }
                   
 public static void main(String[] args)
 {
  new ip();
 }
                   
 public void actionPerformed(ActionEvent e) {
  if (e.getSource() == this.jb1) {
   String url = this.jt.getText();
   InetAddress ip = null;
   try {
    ip = InetAddress.getByName(url);
   }
   catch (UnknownHostException e1) {
    e1.printStackTrace();
   }
   this.jl.setText(ip.toString());
  }
  else if (e.getSource() == this.jb2) {
   this.jl.setText("");
   this.jt.setText("");
  } else {
   System.exit(0);
  }
 }
}






头像

snowcoal
  • java
  • 获取ip地址

本文标签:

java获取ip地址

收藏到我的私密空间

标题:java获取ip地址

作者:柳岸花明

你暂未登录,请登录后才可收藏至您的私密空间 确认取消
雪炭网

键盘操作 更便捷 -雪炭网雪中送炭-乐趣无限

如果本站的内容有幸帮助到了您,建议您了解一下当页的广告内容哦,我们的进步离不开您的支持,Thank you~