主界面框架:
package KnowAll;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class KnowAllPanel extends JFrame{
public KnowAllPanel() {
showPanel();
}
JButton findIP = new JButton(" 查 询 I P ");
JButton findMyIP = new JButton("查询自己的IP地址");
JButton findID = new JButton("查询身份证号");
JButton findNUMBER = new JButton("查询手机号码");
JTextArea myIP = new JTextArea(2, 25);
public void showPanel() {
this.setTitle("百事通");
this.setSize(410, 450);// 左长右高
this.setLocation(500, 70);// 左水平又竖直
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setLayout(new FlowLayout(FlowLayout.CENTER));//流式布局
//添加图标并设置尺寸
int width = 100;
int height = 100;
ImageIcon findIdNumber_img = new ImageIcon("img\\idcard.jpg");
Image findIdNumber = findIdNumber_img.getImage();
findIdNumber = findIdNumber.getScaledInstance(width, height, Image.SCALE_DEFAULT);
findIdNumber_img.setImage(findIdNumber);
JLabel id = new JLabel(findIdNumber_img,JLabel.CENTER);
id.setSize(width, height);
ImageIcon findIp_img = new ImageIcon("img\\ip.jpg");
Image findIp = findIp_img.getImage();
findIp = findIp.getScaledInstance(width, height, Image.SCALE_DEFAULT);
findIp_img.setImage(findIp);
JLabel ip = new JLabel(findIp_img,JLabel.CENTER);
ip.setSize(width, height);
ImageIcon findPhoneNumber_img = new ImageIcon("img\\phonenumber.jpg");
Image findNumber = findPhoneNumber_img.getImage();
findNumber = findNumber.getScaledInstance(width, height, Image.SCALE_DEFAULT);
findPhoneNumber_img.setImage(findNumber);
JLabel phoneNumber = new JLabel(findPhoneNumber_img,JLabel.CENTER);
phoneNumber.setSize(width, height);
//将图标、按钮添加到面板
JPanel jp1 = new JPanel();
jp1.add(ip);
this.add(jp1);
JPanel jp2 = new JPanel();
jp2.add(id);
this.add(jp2);
JPanel jp3 = new JPanel();
jp3.add(phoneNumber);
this.add(jp3);
JPanel jp4 = new JPanel();
jp4.add(findIP);
jp4.add(findID);
jp4.add(findNUMBER);
JPanel jp5 = new JPanel();
jp5.add(findMyIP);
this.add(jp4);
this.add(jp5);
JPanel jp6 = new JPanel();
myIP.setEditable(false);
jp6.add(myIP);
this.add(jp6);
//按钮添加监听
findIP.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
setVisible(false);
FindipPanel panel = new FindipPanel();
dispose();
}
});
findID.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
setVisible(false);
FindidPanel panel = new FindidPanel();
dispose();
}
});
findNUMBER.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
setVisible(false);
FindPhoneNumberPanel panel = new FindPhoneNumberPanel();
dispose();
}
});
findMyIP.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
myIP.setText("");
myIP.setText(InquireIpFunction.getMyIpFromContent());
}
});
}
public static void main(String[] args) {
KnowAllPanel panel = new KnowAllPanel();
}
}
查询身份证号信息框架:
package KnowAll;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class FindidPanel extends JFrame{
public FindidPanel() {
// TODO Auto-generated constructor stub
showPanel();
}
JTextField inputId = new JTextField(20);
JButton inquire = new JButton("查询");
JButton back = new JButton("返回");
JTextArea resultText = new JTextArea(20, 30);
public void showPanel() {
this.setTitle("查询身份证号");
this.setSize(410, 450);// 左长右高
this.setLocation(500, 70);// 左水平又竖直
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setLayout(new FlowLayout(FlowLayout.CENTER));//流式布局
this.add(inputId);
this.add(inquire);
this.add(back);
resultText.setEditable(false);
this.add(resultText);
back.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
setVisible(false);
KnowAllPanel panel = new KnowAllPanel();
dispose();
}
});
inquire.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String idNumber = inputId.getText();
String result = InquireIpFunction.getIdCardInfoFromURL(idNumber);
resultText.setText(result);
}
});
}
public static void main(String[] args) {
FindidPanel panel = new FindidPanel();
}
}
查询IP地址信息框架:
package KnowAll;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class FindipPanel extends JFrame {
public FindipPanel() {
// TODO Auto-generated constructor stub
showPanel();
}
JTextField inputIp = new JTextField(10);
JButton inquire = new JButton("查询");
JButton back = new JButton("返回");
JTextArea resultText = new JTextArea(20, 30);
public void showPanel() {
this.setTitle("查询IP");
this.setSize(410, 450);// 左长右高
this.setLocation(500, 70);// 左水平又竖直
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setLayout(new FlowLayout(FlowLayout.CENTER));// 流式布局
this.add(inputIp);
this.add(inquire);
this.add(back);
resultText.setEditable(false);
this.add(resultText);
back.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
setVisible(false);
KnowAllPanel panel = new KnowAllPanel();
dispose();
}
});
inquire.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String ip = inputIp.getText();
String result = InquireIpFunction.getIpInfoFromURL(ip);
resultText.setText(result);
}
});
}
public static void main(String[] args) {
FindipPanel panel = new FindipPanel();
}
}
查找手机号码信息框架:
package KnowAll;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class FindPhoneNumberPanel extends JFrame {
public FindPhoneNumberPanel() {
// TODO Auto-generated constructor stub
showPanel();
}
JTextField inputPhoneNumber = new JTextField(10);
JButton inquire = new JButton("查询");
JButton back = new JButton("返回");
JTextArea resultText = new JTextArea(20, 30);
public void showPanel() {
this.setTitle("查询手机号码");
this.setSize(410, 450);// 左长右高
this.setLocation(500, 70);// 左水平又竖直
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setLayout(new FlowLayout(FlowLayout.CENTER));// 流式布局
this.add(inputPhoneNumber);
this.add(inquire);
this.add(back);
resultText.setEditable(false);
this.add(resultText);
back.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
setVisible(false);
KnowAllPanel panel = new KnowAllPanel();
dispose();
}
});
inquire.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String phoneNumber = inputPhoneNumber.getText();
String result = InquireIpFunction.getPhoneNumberInfoFromURL(phoneNumber);
resultText.setText(result);
}
});
}
public static void main(String[] args) {
FindPhoneNumberPanel panel = new FindPhoneNumberPanel();
}
}
查询功能实现类:
package KnowAll;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class InquireIpFunction {
//查询自己的IP
public static String getContentFromIp138() {
try {
URL url = new URL("http://2019.ip138.com/ic.asp");
URLConnection conn = url.openConnection();
conn.addRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36");
Scanner scan = new Scanner(conn.getInputStream());
StringBuffer sb = new StringBuffer();
while(scan.hasNextLine()) {
sb.append(scan.nextLine()).append("\r\n");
}
return sb.toString();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
return null;
}
public static String getMyIpFromContent() {
int Index = getContentFromIp138().indexOf("<center>");
int endIndex = getContentFromIp138().indexOf("</center>");
String result = getContentFromIp138().substring(Index+8, endIndex);
return result;
}
//查询输入的IP地址的地理位置
public static String getIpInfoFromURL(String ip) {
StringBuffer content = new StringBuffer();
try {
Elements es = Jsoup.connect("http://ip138.com/ips138.asp?ip="+ip+"&action=2").get().getElementsByTag("li");
if(es.size()>0) {
for (int i = 0; i < es.size(); i++) {
Element e = es.get(i);
content.append(e.text()).append("\r\n");
}
}else {
content.append("IP地址有误");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String result = content.toString();
return result;
}
//查询身份证号码信息
public static String getIdCardInfoFromURL(String idNumber) {
StringBuffer content = new StringBuffer();
try {
Elements es = Jsoup.connect("http://qq.ip138.com/idsearch/index.asp?userid="+idNumber+"&action=idcard").get().getElementsByTag("td");
if(es.size()>0) {
for (int i = 0; i < es.size(); i++) {
Element e = es.get(i);
content.append(e.text()).append("\r\n");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String result = content.toString();
return result;
}
//查询手机号码信息
public static String getPhoneNumberInfoFromURL(String phoneNumber) {
StringBuffer content = new StringBuffer();
try {
Elements es = Jsoup.connect("http://www.ip138.com:8080/search.asp?mobile="+phoneNumber+"&action=mobile").get().select(".TDC");
if(es.size()>0) {
for (int i = 3; i < es.size(); i++) {
Element e = es.get(i);
content.append(e.text()).append("\r\n");
}
}
} catch (IOException e) {
e.printStackTrace();
}
String result = content.toString().replace("测吉凶(新)", "").replace("更详细的..","");
return result;
}
}