Java框架代码.docx

上传人:小飞机 文档编号:3159648 上传时间:2023-03-11 格式:DOCX 页数:29 大小:45.06KB
返回 下载 相关 举报
Java框架代码.docx_第1页
第1页 / 共29页
Java框架代码.docx_第2页
第2页 / 共29页
Java框架代码.docx_第3页
第3页 / 共29页
Java框架代码.docx_第4页
第4页 / 共29页
Java框架代码.docx_第5页
第5页 / 共29页
亲,该文档总共29页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《Java框架代码.docx》由会员分享,可在线阅读,更多相关《Java框架代码.docx(29页珍藏版)》请在三一办公上搜索。

1、Java框架代码ParseIML: package rssreader.dao; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import rssreader

2、.model.NewsFeed; public class ParseIML private static String rootNodeName; public static ListreadXMLFile(String inFile, String dirName,String type) throws JDOMException,IOException( Document Doc = null; SAXBuilder sb = new SAXBuilder(false); File fXml = new java.io.File(inFile); Doc = sb.build(fXml)

3、; /加载一个XML文件 /定义newsFeeds返回列表类型 List newsFeeds = new ArrayList; /创建newsFeed实例 NewsFeed newsFeed = null ; /获得根节点名 rootNodeName = rootNodeName.getName; /创建新文件夹命路径 SaveFile.createDir(dirName +/ + rootNodeName.getName + / + / 获得item标签 Element eChannel = rootNodeName.getChild(channel); ListitemList=eChan

4、nel.getChildren(item); for(int i = 0; i itemList.size; i+) org.jdom.Element item = itemList.get(i); / 创建item的Bean实例 type); newsFeed = newsFeed; / 获得节点内容 String title=item.getChildText(title).trim; String link=item.getChildText(link); String author=item.getChildText(author); String guid=item.getChild

5、Text(guid); String pudDateString=item.getChildText(pubDate); String category=item.getChildText(category); iterator.hasNext;) String descriptionString=item.getChildText(description); /设置节点内容 newsFeed.setTitle(title); newsFeed.setLink(link); newsFeed.setAuthor(author); newsFeed.setGuid(guid); newsFeed

6、.setPubDate(pubDate); newsFeed.setCategory(category); newsFeed.setdescription(description); /添加内容对象 newsFeed.add(newsFeeds); /返回内容对象容器 return newsFeeds; /* * 获取的符合新闻结构的缓存对象分类写入文本文件中 * * param List * newsFeed 新闻内容缓存对象 * param String * destFileName 目标文件夹 * param String * type 新闻类型 * throws IOException

7、 */ public static void parseListToFile(List newsFeed, String destFileName, String type) throws IOException / jdk5.0新的for循环方法获得新闻内容对象 for(Iterator iterator = newsFeed.iterator; / 获得新闻内容对象 NewsFeed newsFeed = iterator.next; / 使用已经设置好了的元素 String title = newsFeed.getTitle; String link= newsFeed.getLink;

8、 String author= newsFeed.getAuthor; String guid= newsFeed.getGuid; String pubDate = newsFeed.getPubDate.trim.replaceAll(, ).replace(, ).replace(:, ); String category = newsFeed.getCategory; String description = newsFeed.getDescription; /设置文本文件写入内容 StringBuffer sbf = new StringBuffer; sbf.append(主题:+

9、 title + + t); sbf.append(链接:+ link +t); sbf.append(作者:+ author +t); sbf.append(地址:+ guid +t); sbf.append(发布时间:+ pubDate +t); sbf.append(类型:+ category +t); sbf.append(要文:+ description +t); /把写入的内容转换为char型并生成文本文件 SaveFile.writeFileByChars(destFileName + rootNodeName + / + type + / + pubDate + .text,s

10、bf.toString); package rssreader.dao; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import com.sun.java_cup.internal.internal_error

11、; import rssreader.model.NewsFeed; import javax.swing.SizeSequence; import javax.swing.event.ListSelectionListener; / + import javax.swing.text.html.HTMLDocument.Iterator; public class PaarseXML private static String rootNodelName; public static ListreadXMLFile(String inFile,String dirName,String ty

12、pe) throws JDOMException,IOException Document doc =null; SAXBuilder sb =new SAXBuilder(false); File fXml =new File(inFile); doc =sb.build(fXml); List newsFeeds =new ArrayList; NewsFeed newsFeed=null; Element root =doc.getRootElement; rootNodelName =root.getName; SaveFile.createDir(dirName+/+root.get

13、Name+/+type); Element eChannel =root.getChild(cjannel); ListitemList=eChannel.getChildren(item); for (int i=0;iitemList.size;i+) Element item=itemList.get(i); return newsFeeds; newsFeed=new NewsFeed; String title=item.getChildText(title).trim; String link=item.getChildText(link); String author=item.

14、getChildText(author); String guid=item.getChildText(guid); String category=item.getChildText(category); String pubDateString=item.getChildText(pubDate); String descriptionString=item.getChildText(description); newsFeed.setTitle(title); newsFeed.setlink(link); newsFeed.setAuthor(author); newsFeed.set

15、Guid(guid); newsFeed.setpubDate(pubDateString); newsFeed.setCategory(category); newsFeed.setDescription(descriptionString); newsFeed.add(newsFeed); public static void parseListToFile(ListnewsFeeds,String destFileName,String type) throws IOException for(Iterator=newsFeeds.iterator;iterator.hasNext;)

16、newsFeeds newsFeed=iterator.next; String title=newsFeed.getTitle; String link =newsFeed.getlink; String author =newsFeed.getAuthor; String guid=newsFeed.getlguid; String pubDate =newsFeed.getpubDate.trim.replaceAll(,).replace(,).repalce(:,); String description=newsFeed.getDescription; String categor

17、y=newsFeed.getcategory; StringBuffer sbf=newsFeed StringBuffer; sbf.append(主题: + + title + + t); sbf.append(连接: + link + t); sbf.append(作者: + link + t); sbf.append(地址: + link + t); sbf.append(发布时间: + link + t); sbf.append(类型: + link + t); sbf.append(要文: + link + t); SaveFile.writeFileBYChars(destFil

18、eName+/+rootNodelName+/+type+/+pubDate+.txt,sbf.toString); private static void StringBuffer / TODO Auto-generated method stub FrameMain package ponent; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.Action

19、Listener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.IOException; import .URLDecoder; import java.util.Vector; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; imp

20、ort javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.ListSelectionModel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.text.BadLocationException; import org.jdom.JDOMException; impo

21、rt rssreader.model.URL; import rssreader.service.Service; /* * 表示层面板 */ public class FrameMain extends JFrame /* 版本控制标识 */ private static final long serialVersionUID /* 业务逻辑对象 */ private Service service; /* 网址对象容器 */ private Vector urls; = 1L; /* 目标文件 */ private static String /* 主面板 */ private JPane

22、l /* 上面板 */ panel; topPanel; bottomPanel; tablePanel; newsTitleTable; menuPanel; menuPanel2; menuPanel3; tableSolPanel; messageSolPanel; inFile; private JPanel /* 下面板 */ private JPanel /* 新闻标题表格面板 */ private JPanel /* 表格 */ private TableNewTitleList /* 操作栏面板 */ private JPanel /* 操作栏子面板 */ private JP

23、anel /* 操作栏子面板 */ private JPanel /* 表格滚动条面板 */ private JScrollPane /* 新闻详情滚动面板 */ private JScrollPane /* 新闻内容文本域 */ private TextPaneNewsDescription textPaneNewsDescription; /* 单击事件判断标识 */ private boolean /* 读取按钮 */ private JButton /* 查询按钮 */ flag = false; readButton; queryButton; comboBox; private J

24、Button /* 新闻类型下拉框 */ private JComboBox /* 站点标签 */ private JLabel /* 全文链接标签 */ label; lk; errorInfo newstitleText; = ; private LabelLink /* 显示错误信息 */ private String /* 查询新闻标题文本框 */ private JTextField /* 主面板行数 */ private final static int PANEL_ROW_NUMBER = 2; /* 主面板列数 */ private final static int /* 主面

25、板宽度 */ private final static int /* 主面板高度 */ PANEL_CELL_NUMBER PANEL_WIDTH PANEL_HEIGHT = 1; = 780; private final static int /* 表格面板行数 */ private final static int /* 表格面板列数 */ private final static int /* 表格区域宽度 */ private final static int /* 表格区域高度 */ private final static int /* 主面板标题名 */ private fin

26、al static String /* 读取按钮命名 */ private final static String /* 读取按钮命名 */ private final static String /* 站点标签命名 */ private final static String /* * 面板初始化 */ private void init / 创建业务逻辑对象 = 600; = 1; = 1; TABLE_PANEL_ROW_NUMBER TABLE_PANEL_CELL_NUMBER TABLE_VIEW_WIDTH TABLE_VIEW_HEIGHT = 750; = 220; PANE

27、L_TITLE_NAME READ_BUTTON_NAME = RSS阅读器; = 读取; = 查询; = 站点; QUERY_BUTTON_NAME LABEL_NAME service = new Service; / 创建主面板 panel = new JPanel; / 创建分面板 topPanel = new JPanel; bottomPanel = new JPanel; tablePanel = new JPanel; menuPanel = new JPanel; menuPanel2 = new JPanel; menuPanel3 = new JPanel; / 创建读取

28、按钮 readButton = new JButton(READ_BUTTON_NAME);/ / 创建查询按钮 queryButton = new JButton(QUERY_BUTTON_NAME);/ / 创建站点标签 label = new JLabel(LABEL_NAME);/ (标签名) / 创建查询标题文本框 newstitleText = new JTextField; / 创建下拉框 urls = service.initNewsFeedURLs; comboBox = new JComboBox(urls); / 创建标题表格 newsTitleTable = new T

29、ableNewTitleList; tableSolPanel = new JScrollPane(newsTitleTable); / 创建文本域 textPaneNewsDescription = new TextPaneNewsDescription; messageSolPanel = new JScrollPane(textPaneNewsDescription); /* * 主面板构造器 */ public FrameMain / 面板初始化 init; / 设定主面板和分面板位置 GridLayout(PANEL_ROW_NUMBER, panel.setLayout(new P

30、ANEL_CELL_NUMBER);/(行 / , / 列 / ) / 添加上,下分面板 panel.add(topPanel); panel.add(bottomPanel); / 上面板添加菜单面板,表格面板 topPanel.setLayout(new BorderLayout); / 边界布局:顶层 topPanel.add(North, menuPanel); topPanel.add(Center, tablePanel); / 下面板添加 bottomPanel.setLayout(new BorderLayout); / 边界布局:顶层 bottomPanel.add(Cent

31、er, messageSolPanel); / 菜单面板添加控件 menuPanel.setLayout(new BorderLayout); / 边界布局:顶层 menuPanel.add(West, new JLabel( ); menuPanel.add(Center, menuPanel2); menuPanel.add(East, menuPanel3); / 菜单子面板添加控件 menuPanel2.setLayout(new BorderLayout); / 边界布局:顶层 menuPanel2.add(West, label); menuPanel2.add(Center, c

32、omboBox); menuPanel2.add(East, readButton); / 菜单子面板添加控件 menuPanel3.setLayout(new BorderLayout); / 边界布局:顶层 menuPanel3.add(West, new JLabel( ); menuPanel3.add(Center, newstitleText); menuPanel3.add(East, queryButton); / 设定新闻标题文本框属性 newstitleText.setColumns(30); / 设定表格面板样式 tablePanel.setLayout(new Grid

33、Layout(TABLE_PANEL_ROW_NUMBER, TABLE_PANEL_CELL_NUMBER);/ (行,列) / 设置表格属性 newsTitleTable.setPreferredScrollableViewportSize(new Dimension( TABLE_VIEW_WIDTH, TABLE_VIEW_HEIGHT);/ newsTitleTable.getSelectionModel.setSelectionMode( ListSelectionModel.SINGLE_SELECTION);/ 设置标题选取方式为单行模newsTitleTable.getSel

34、ectionModel.addListSelectionListener( new CreateRow);/ 设置表格监听 newsTitleTable.addMouseListener(new TableRowClick);/ 设置表格单击监听事件 / 读取按钮监听 readButton.addActionListener(new ReadButton); / 查询按钮监听 queryButton.addActionListener(new QueryButton); / 将表格放入滚动面板 tablePanel.add(tableSolPanel); / 主面板属性设定 this.setS

35、ize(PANEL_WIDTH, PANEL_HEIGHT);/ (宽,高) this.setTitle(PANEL_TITLE_NAME);/ (标题名) this.setResizable(true);/ 设置固定窗口大小 this.setContentPane(panel);/ 设置内容面板 式 /* * 标题表格监听器,创建新行事件 */ private class CreateRow implements ListSelectionListener /* * 标题表格监听器,表格单击事件 public void valueChanged(ListSelectionEvent e) i

36、f (flag) / 获得选中行的值 int row = newsTitleTable.getSelectedRow; / 网址路径 String link = ; try catch (BadLocationException ble) finally / 弹窗显示错误信息 showError; / 获得处理BadLocation异常信息 errorInfo = 文档转换操作异常; / 调用逻辑层对象完成新闻内容显示 link = service.showMessage(row, textPaneNewsDescription); / 创建查看全文标签 lk = new LabelLink(

37、link); / 内容面板添加标签 textPaneNewsDescription.insertComponent(lk); / 表格未被单击 flag = false; */ private class TableRowClick extends MouseAdapter public void mousePressed(MouseEvent e) / 表格单击 flag = true; /* * 读取按钮监听器,读取选择网址事件 */ private class ReadButton implements ActionListener public void actionPerformed

38、(ActionEvent e) / 网址字符串 String address = ; / 获得下拉框选中项 Object o = comboBox.getSelectedItem; / 获得工程根目录 String dir = System.getProperty(user.dir); / 获得下拉框选中项的文件名,赋值给网址字符串 address = (URL) o).getAddress; / 获得解析文件完整路径 inFile = dir + NewsfeedFiles + address; try / 转换路径编码为UTF-8 catch (JDOMException jde) / 获

39、得处理JDOM异常信息 inFile = URLDecoder.decode(inFile, utf-8); / 调用逻辑对象对文件进行读写,显示 service.fileDo(inFile, newsTitleTable, textPaneNewsDescription, address.substring(0, address.lastIndexOf(.); catch (IOException ioe) / 获得处理IO异常信息 errorInfo = 系统找不到指定文件; errorInfo = 解析文件异常; finally / 弹窗显示错误信息 showError; /* * 查询

40、按钮监听器,按查询条件过滤新闻标题内容 */ private class QueryButton implements ActionListener /* * 通过弹窗显示错误信息 */ private void showError / 通过弹出对话框显示异常信息 if (!errorInfo.equals() JOptionPane.showMessageDialog(null, errorInfo); public void actionPerformed(ActionEvent e) / 调用逻辑对象实现查询功能 service.queryTitle(newstitleText, tex

41、tPaneNewsDescription, newsTitleTable); Service package rssreader.service; import java.awt.Color; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Vector; import javax.swing.JTextField; import javax.swing.text.BadLocationException; import org.jdom.JDOMException; import rssreader.dao.ParseXML; import rssreade

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号