首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

展示本机IP(GUI版)

2012-12-24 
显示本机IP(GUI版)IPUtil.java?package com.gary.netimport java.net.Inet4Addressimport java.net.Inet

显示本机IP(GUI版)

IPUtil.java

?

package com.gary.net;import java.net.Inet4Address;import java.net.InetAddress;import java.net.NetworkInterface;import java.net.SocketException;import java.util.ArrayList;import java.util.Enumeration;import java.util.HashMap;public class IPUtil {public static void main(String[] args) throws SocketException {System.out.println(getLocalIPStr());}public static ArrayList<HashMap<String, String>> getLocalIP() throws SocketException {ArrayList<HashMap<String, String>> ips = new ArrayList<HashMap<String, String>>();Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();InetAddress ip = null;while (allNetInterfaces.hasMoreElements()) {NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();Enumeration<InetAddress> addresses = netInterface.getInetAddresses();while (addresses.hasMoreElements()) {ip = (InetAddress) addresses.nextElement();if (ip != null && ip instanceof Inet4Address && !ip.getHostAddress().equals("127.0.0.1")) {HashMap<String, String> nameAndIP = new HashMap<String, String>();nameAndIP.put("InterfaceName", netInterface.getName());nameAndIP.put("IP", ip.getHostAddress());ips.add(nameAndIP);}}}return ips;}public static String print(ArrayList<HashMap<String, String>> ips){StringBuffer sb = new StringBuffer();for (HashMap<String, String> ip : ips) {sb.append("InterfaceName:" + ip.get("InterfaceName") + "\n");sb.append("IP:" + ip.get("IP") + "\n");}return sb.toString();}public static String getLocalIPStr(){try {return print(getLocalIP());} catch (SocketException e) {return "127.0.0.1";}}}

?

ViewIPFrame.java

?

package com.gary.net;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JTextArea;public class ViewIPFrame extends JFrame implements ActionListener{private static final long serialVersionUID = 6389756886534379680L;JMenuItem jmiExit,jmiAbout;public ViewIPFrame(){jmiExit = new JMenuItem("Exit");jmiAbout = new JMenuItem("About");JMenuBar jmb = new JMenuBar();setJMenuBar(jmb);JMenu fileMenu = new JMenu("File",false);JMenu helpMenu = new JMenu("Help",false);jmb.add(fileMenu);jmb.add(helpMenu);fileMenu.add(jmiExit);helpMenu.add(jmiAbout);jmiExit.addActionListener(this);jmiAbout.addActionListener(this);}public void actionPerformed(ActionEvent e) {if(e.getSource() == jmiExit){System.exit(0);}else if(e.getSource() == jmiAbout){JOptionPane.showMessageDialog(this,"View IP Address \n author: gary \n bug report: qq 408036296");}}public static void main(String [] args){JTextArea jtextArea = new JTextArea(IPUtil.getLocalIPStr());jtextArea.setEditable(false);ViewIPFrame frame=new ViewIPFrame();frame.setTitle("viewIP");frame.getContentPane().add(jtextArea);frame.setSize(200,100);frame.pack();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);}}

?

界面预览


展示本机IP(GUI版)

热点排行