JAVA小项目实例源码—学习娱乐小助手
一、程序实现
- 项目目录:
MyJFrame:实现项目界面样式;
AppProcess:实现调用api或爬虫拿数据;
NLIProcess:解析用户输入语句意图;
lib中是程序开发需要用到的jar包列表。
- 主要代码说明:
程序不同功能领域由选项卡JTabbedPane展示。用户输入哪个功能领域的问题,将由该领域的选项卡展示答案。
在选项卡中添加选项:
tabbedPanel.addTab("笑话",panel_joke);
tabbedPanel.addTab("新闻",panel_news);
tabbedPanel.addTab("星座",panel_hor);
tabbedPanel.addTab("成语",panel_idiom);
tabbedPanel.addTab("解梦",panel_oneir);
切换选项卡时给出对应功能领域的语料示例:
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<String, Object> 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<String, List<String>> newsMap = (HashMap<String, List<String>>)obj;
List<String> titleList = newsMap.get("title");
final List<String> 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<String, List<String>> OneirMap = (HashMap<String, List<String>>)obj;
List<String> wordList = OneirMap.get("word");
final List<String> 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<String, List<String>> OneirMap = (HashMap<String, List<String>>)obj;
List<String> nameList = OneirMap.get("name");
final List<String> 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:
private Map<String, List<String>> getNewsMap(String type){
Map<String, List<String>> newsMap = new HashMap<>();
List<String> titleList = new ArrayList<>();
List<String> 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;
}
二、运行效果
- 运行截图如下:
笑话(支持看笑话、看趣图):
新闻(显示新闻列表,点选后可进入新闻详情页):
星座(支持查今日、明日、本周、本月、今年运势):
成语(支持成语释义、查带某字、词的成语):
解梦:
三、其他补充
- 新闻:
点选新闻标题后会调用浏览器显示新闻详细页(这里本来打算用JEditorPane实现的,实现后发现新闻网页格式变形,查询资料知道JEditorPane 存在缺陷: 不支持CSS样式和JavaScript。所以改为了调用浏览器打开新闻页。) - 星座:
没有找到免费的api接口,星座数据是用爬虫爬下来的,所以第一次问星座问题时出结果会有点慢。
JAVA小项目实例源码—学习娱乐小助手
注:本文著作权归作者,由demo大师代发,拒绝转载,转载需要作者授权