你的浏览器禁用了JavaScript, 请开启后刷新浏览器获得更好的体验!
首页
热门
推荐
精选
登录
|
注册
JAVA小项目实例源码—学习娱乐小助手
立即下载
用AI写一个
金额:
1
元
支付方式:
友情提醒:源码购买后不支持退换货
立即支付
我要免费下载
发布时间:2017-08-28
125人
|
浏览:13128次
|
收藏
|
分享
技术:java+Jsoup+JFrame+JTable+JTabbedPane+JScrollPane
运行环境:eclipse
概述
该程序支持功能有:笑话、新闻、星座运势、成语、解梦。其中数据来源为访问api接口和爬虫爬取(Jsoup)。
详细
### 一、程序实现 - 项目目录: MyJFrame:实现项目界面样式; AppProcess:实现调用api或爬虫拿数据; NLIProcess:解析用户输入语句意图; lib中是程序开发需要用到的jar包列表。 ![](/contentImages/image/20170828/OFOQpv2hBPs8yY2hMb1.png) - 主要代码说明: 程序不同功能领域由选项卡JTabbedPane展示。用户输入哪个功能领域的问题,将由该领域的选项卡展示答案。 在选项卡中添加选项: ```java tabbedPanel.addTab("笑话",panel_joke); tabbedPanel.addTab("新闻",panel_news); tabbedPanel.addTab("星座",panel_hor); tabbedPanel.addTab("成语",panel_idiom); tabbedPanel.addTab("解梦",panel_oneir); ``` 切换选项卡时给出对应功能领域的语料示例: ```java tabbedPanel.addChangeListener(new ChangeListener(){ @Override public void stateChanged(ChangeEvent arg0) { // TODO Auto-generated method stub int index = tabbedPanel.getSelectedIndex(); areaTxt.setText(introduce + introduces[index]); ((JScrollPane)tabbedPanel.getComponent(index)).setViewportView(areaTxt); } }); ``` 输出答案时切换为对应功能选项卡展示答案: private void jButton_sendActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: Map
retMap = app.process(jTextField1.getText()); jLabel2.setText(""); if (retMap.containsKey("ds")) { jLabel2.setText(retMap.get("ds").toString()); jLabel2.setForeground(Color.red); } else if (retMap.containsKey("joke")) { Object obj = retMap.get("joke"); tabbedPanel.setSelectedIndex(0); JScrollPane jpanel = ((JScrollPane) tabbedPanel.getComponent(0)); if (obj instanceof String) { areaTxt.setText(obj.toString()); jpanel.setViewportView(areaTxt); } else { JLabel jLabel = new JLabel(); if (obj != null) { ImageIcon image = new ImageIcon((URL) obj); jLabel.setIcon(image); } else { jLabel.setText("图片加载失败!"); } jpanel.setViewportView(jLabel); } }else if (retMap.containsKey("horoname")){ Object obj = retMap.get("horoname"); tabbedPanel.setSelectedIndex(2); JScrollPane jpanel = ((JScrollPane) tabbedPanel.getComponent(2)); areaTxt.setText(obj.toString()); jpanel.setViewportView(areaTxt); }else if(retMap.containsKey("news")){ Object obj = retMap.get("news"); tabbedPanel.setSelectedIndex(1); JScrollPane jpanel = ((JScrollPane) tabbedPanel.getComponent(1)); if (obj instanceof String) { areaTxt.setText(obj.toString()); jpanel.setViewportView(areaTxt); }else{ @SuppressWarnings("unchecked") Map
> newsMap = (HashMap
>)obj; List
titleList = newsMap.get("title"); final List
urlList = newsMap.get("url"); table = getSimpleTable(titleList); jpanel.setViewportView(table); table.addMouseListener(new MouseListener(){ @Override public void mouseClicked(MouseEvent arg0) { // TODO Auto-generated method stub // 取得用户所选行的行数 int selectRows=table.getSelectedRows().length; if(selectRows==1){ // 取得用户所选单行 int selectedRowIndex = table.getSelectedRow(); String url = urlList.get(selectedRowIndex); Desktop desktop = Desktop.getDesktop(); try { desktop.browse(new URI(url)); } catch (IOException | URISyntaxException e1) { e1.printStackTrace(); } } } @Override public void mouseEntered(MouseEvent arg0) {} @Override public void mouseExited(MouseEvent arg0) {} @Override public void mousePressed(MouseEvent arg0) {} @Override public void mouseReleased(MouseEvent arg0) {}}); } }else if(retMap.containsKey("oneiromancy")){ Object obj = retMap.get("oneiromancy"); tabbedPanel.setSelectedIndex(4); final JScrollPane jpanel = ((JScrollPane) tabbedPanel.getComponent(4)); if (obj instanceof String) { areaTxt.setText(obj.toString()); jpanel.setViewportView(areaTxt); }else{ @SuppressWarnings("unchecked") Map
> OneirMap = (HashMap
>)obj; List
wordList = OneirMap.get("word"); final List
urlList = OneirMap.get("url"); table = getSimpleTable(wordList); jpanel.setViewportView(table); table.addMouseListener(new MouseListener(){ @Override public void mouseClicked(MouseEvent arg0) { // TODO Auto-generated method stub // 取得用户所选行的行数 int selectRows=table.getSelectedRows().length; if(selectRows==1){ // 取得用户所选单行 int selectedRowIndex = table.getSelectedRow(); String url = urlList.get(selectedRowIndex); String answer = app.getOneirData(url); areaTxt.setText(answer); jpanel.setViewportView(areaTxt); } } @Override public void mouseEntered(MouseEvent arg0) {} @Override public void mouseExited(MouseEvent arg0) {} @Override public void mousePressed(MouseEvent arg0) {} @Override public void mouseReleased(MouseEvent arg0) {}}); } }else if(retMap.containsKey("idioms")){ Object obj = retMap.get("idioms"); tabbedPanel.setSelectedIndex(3); final JScrollPane jpanel = ((JScrollPane) tabbedPanel.getComponent(3)); if (obj instanceof String) { areaTxt.setText(obj.toString()); jpanel.setViewportView(areaTxt); }else{ @SuppressWarnings("unchecked") Map
> OneirMap = (HashMap
>)obj; List
nameList = OneirMap.get("name"); final List
idList = OneirMap.get("id"); table = getSimpleTable(nameList); jpanel.setViewportView(table); table.addMouseListener(new MouseListener(){ @Override public void mouseClicked(MouseEvent arg0) { // TODO Auto-generated method stub // 取得用户所选行的行数 int selectRows=table.getSelectedRows().length; if(selectRows==1){ // 取得用户所选单行 int selectedRowIndex = table.getSelectedRow(); String id = idList.get(selectedRowIndex); String answer = app.getIdiomsP(id); areaTxt.setText(answer); jpanel.setViewportView(areaTxt); } } @Override public void mouseEntered(MouseEvent arg0) {} @Override public void mouseExited(MouseEvent arg0) {} @Override public void mousePressed(MouseEvent arg0) {} @Override public void mouseReleased(MouseEvent arg0) {}}); } } } 使用Jsoup解析XML: ```java private Map
> getNewsMap(String type){ Map
> newsMap = new HashMap<>(); List
titleList = new ArrayList<>(); List
urlList = new ArrayList<>(); String url = MessageFormat.format("http://www.chinanews.com/rss/{0}.xml", type); String httpContent = getHttpContent(url,"gbk"); Document doc = Jsoup.parse(httpContent, "", new Parser(new XmlTreeBuilder())); //item Elements itemElements = doc.select("item"); if(itemElements != null){ for(Element e : itemElements){ String title = e.select("title").first().text(); String link = e.select("link").first().text(); titleList.add(title); urlList.add(link); } newsMap.put("title", titleList); newsMap.put("url", urlList); } return newsMap; } ``` ### 二、运行效果 - 运行截图如下: 笑话(支持看笑话、看趣图): ![](/contentImages/image/20170828/uExKg31G5EbpVedILAr.png) 新闻(显示新闻列表,点选后可进入新闻详情页): ![](/contentImages/image/20170828/tcPuU6OtSRuOQEZ6fwi.png) 星座(支持查今日、明日、本周、本月、今年运势): ![](/contentImages/image/20170828/SM41kJD0kcFtkKxwxuN.png) 成语(支持成语释义、查带某字、词的成语): ![](/contentImages/image/20170828/twTmebuHGJ9pPW2PmrI.png) 解梦: ![](/contentImages/image/20170828/7i00VNR3NMFV7ZUL1UG.png) ### 三、其他补充 - 新闻: 点选新闻标题后会调用浏览器显示新闻详细页(这里本来打算用JEditorPane实现的,实现后发现新闻网页格式变形,查询资料知道JEditorPane 存在缺陷: 不支持CSS样式和JavaScript。所以改为了调用浏览器打开新闻页。) - 星座: 没有找到免费的api接口,星座数据是用爬虫爬下来的,所以第一次问星座问题时出结果会有点慢。
本实例支付的费用只是购买源码的费用,如有疑问欢迎在文末留言交流,如需作者在线代码指导、定制等,在作者开启付费服务后,可以点击“购买服务”进行实时联系,请知悉,谢谢
感谢
2
手机上随时阅读、收藏该文章 ?请扫下方二维码
相似例子推荐
评论
作者
Florence
3
例子数量
267
帮助
6
感谢
评分详细
可运行:
4.5
分
代码质量:
4.5
分
文章描述详细:
4.5
分
代码注释:
4.5
分
综合:
4.5
分
作者例子
微信小程序—智能小蜜(基于智能语义解析olami开放平台)
JAVA小项目实例源码—学习娱乐小助手
SpringMVC+Maven开发项目源码详细介绍