显示本机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版)

你可能感兴趣的:(java,swing,IP)