自己照着书写的,用到的新东西就是InetAddress,自己API看一下就会了。

package day17;
 
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.net.*;
 
public class testIP implements ActionListener {
 
	/**
	 * @author Laycher {@link http://laycher.cn}
	 */
	JFrame jf;
	JTextArea jta;
	JTextField jtf;
 
	public testIP() {
		jf = new JFrame("查询IP测试版");
		jta = new JTextArea(13, 30);
		JScrollPane jsp = new JScrollPane(jta);
		jta.setEditable(false);
		JPanel jp1 = new JPanel();
		jp1.add(jsp);
		jf.add(jp1, BorderLayout.NORTH);
 
		JButton jb1 = new JButton("本机IP地址");
		jb1.addActionListener(this);
		JButton jb2 = new JButton("清屏");
		jb2.addActionListener(this);
		JButton jb3 = new JButton("关于");
		jb3.addActionListener(this);
		JButton jb4 = new JButton("退出");
		jb4.addActionListener(this);
		JPanel jp2 = new JPanel();
		jp2.add(jb1);
		jp2.add(jb2);
		jp2.add(jb3);
		jp2.add(jb4);
		jf.add(jp2, BorderLayout.CENTER);
 
		JLabel jl = new JLabel("域名测 IP:");
		jtf = new JTextField(15);
		jtf.setText("www.laycher.cn");
		JButton jb5 = new JButton("测试");
		jb5.addActionListener(this);
		jtf.addActionListener(this);
		JPanel jp3 = new JPanel();
		jp3.add(jl);
		jp3.add(jtf);
		jp3.add(jb5);
		jf.add(jp3, BorderLayout.SOUTH);
 
		jf.setLocation(300, 200);
		jf.setSize(360, 360);
		jf.setVisible(true);
		jf.setResizable(false);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
 
	public static void main(String[] args) {
		new testIP();
	}
 
	@Override
	public void actionPerformed(ActionEvent e) {
		String comm = e.getActionCommand();
		if ("本机IP地址".equals(comm)) {
			try {
				InetAddress addr1 = InetAddress.getLocalHost();
				// System.out.println(addr1);
				jta.append("主机名:" + addr1.getHostName() + "\n");
				jta.append("IP地址:" + addr1.getHostAddress() + "\n");
			} catch (UnknownHostException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} else if ("清屏".equals(comm)) {
			jta.setText(null);
		} else if ("退出".equals(comm)) {
			System.exit(0);
		} else if ("测试".equals(comm) || jtf.getText().equals(comm)) {
			try {
				InetAddress addr2 = InetAddress.getByName(jtf.getText());
				jta.append("主机名:" + addr2.getHostName() + "\n");
				jta.append("IP地址:" + addr2.getHostAddress() + "\n");
				jtf.selectAll();
				jtf.requestFocus();
			} catch (UnknownHostException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				JOptionPane.showConfirmDialog(
						jf, // 如果为null,此框架显示在中央,为jf则显示为jf的中央
						"您输入的既不是 域名 也不是 IP!\n或者您的输入有误!\n请重新输入!", "错误",
						JOptionPane.CLOSED_OPTION);
				jtf.setText(null);
				jtf.requestFocus();// 光标回来
			}
		} else if ("关于".equals(comm)) {
			final JFrame jf = new JFrame("关于");
			JLabel jl1 = new JLabel("<html><b>查询IP</b></html>");
			JPanel jp1 = new JPanel();
			jp1.add(jl1);
			jf.add(jp1);
			JLabel jl2 = new JLabel("第一次用net编程");
			JPanel jp2 = new JPanel();
			jp2.add(jl2);
			jf.add(jp2);
			JLabel jl3 = new JLabel(
					"<html>作者博客:<a href='http://laycher.cn'>laycher.cn</a></html>");
			jl3.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent e) {
					try {
						Runtime.getRuntime().exec(
								"explorer " + "http://laycher.cn");
					} catch (Exception ex) {
						ex.printStackTrace();
					}
				}
			});
			JPanel jp3 = new JPanel();
			jp3.add(jl3);
			jf.add(jp3);
			JLabel jl4 = new JLabel(
					"<html>作者微博:<a href='http://t.sina.com.cn/laycher'>t.sina.com.cn/laycher</a></html>");
			jl4.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent e) {
					try {
						Runtime.getRuntime().exec(
								"explorer " + "http://t.sina.com.cn/laycher");
					} catch (Exception ex) {
						ex.printStackTrace();
					}
				}
			});
			JPanel jp4 = new JPanel();
			jp4.add(jl4);
			jf.add(jp4);
			JLabel jl5 = new JLabel("制作日期:2010.8.31");
			JPanel jp5 = new JPanel();
			jp5.add(jl5);
			jf.add(jp5);
			JPanel jp6 = new JPanel();
			JButton jb = new JButton("确定");
			jb.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					jf.dispose();
				}
			});
			jp6.add(jb);
			jf.add(jp6);
			jf.setLayout(new GridLayout(6, 1));
			jf.setResizable(false);
			jf.setVisible(true);
			jf.setSize(300, 280);
			jf.setLocation(450, 200);
		}
	}
}

>> 若为原创,转载请注明: 转载自Laycher's Blog

>> 本文链接地址: java 查询IP测试版

>> 订阅本站: http://feed.feedsky.com/laycher



无觅相关文章插件,快速提升流量