用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

RSS阅读器

2017-11-28 作者: 一如既暀丶举报

[java]代码库

package rssreader.view;
import rssreader.model.NewsFeed;
import rssreader.model.URL;
import rssreader.service.*;
 
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.Label;
import java.awt.Menu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.nio.Buffer;
import java.util.HashMap;
import java.util.Map;
 
import org.jdom.*;
 
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JToolBar;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;
 
import org.jdom.JDOMException;
 
public class FrameMain extends JFrame{
    public static Font font = new Font("宋体", Font.BOLD, 25);
    public static Font bigfont = new Font("宋体", Font.BOLD, 40);
    /**业务逻辑对象*/
    private Service service;
    /**菜单面板中的控件*/
    private JComboBox comboBox;//下拉框
    private JButton readButton;//读取按钮
    private JButton saveButton;//保存按钮
    private JButton moreButton;//打开网页按钮
    private JTextField newstitleText;//查询新闻标题输入框
    private JButton queryButton;//查询按钮
    private String uriaddress;//网页地址
    private JLabel statusLabel;//状态栏
    /**文件选择的文件项*/
     
    /**新闻标题面板的控件*/
    private JTable newsTitleTable;//新闻标题表格
    private JTextPane textPaneNewsDescripation;//新闻内容文本框
     
     
    //主面板构造器
    public FrameMain() {
        // TODO Auto-generated constructor stub
        //创建业务逻辑对象
        service=new Service(this);
        setTitle("RSS阅读器");
        //设置窗体大小
        setBounds(1000, 300, 1500, 1700);
        //添加主面板
        JPanel contentePanel=getContentPanel();
        setContentPane(contentePanel);
        //添加菜单
        setJMenuBar(getJMenuBar());
     
         
         
    }
    //主面板
    private  JPanel getContentPanel(){
        JPanel panel=new JPanel(new BorderLayout());
     
        //面板上添加工具栏
        panel.add(getJToolBar(),BorderLayout.NORTH);
     
        panel.add(getCenterPanel(),BorderLayout.CENTER);
         
         
        panel.add(getStatusBar(),BorderLayout.SOUTH);
     
        return panel;
         
    }
     
    //面板组件
    private JSplitPane getCenterPanel(){
        JSplitPane splitPane=new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        splitPane.setDividerLocation(650);//设置分割条位置
         
        JPanel panel=new JPanel(new BorderLayout());
        //添加功能
        panel.add(getQueryPanel(),BorderLayout.NORTH);
        //新闻列表
        panel.add(getTabelSollPanel(),BorderLayout.CENTER);
         
        //新闻内容
         
        splitPane.setLeftComponent(panel);
        splitPane.setRightComponent(getTextSolPanel());
        return splitPane;
    }
     
     
     
     
    //创建菜单
    public JMenuBar getJMenuBar(){
        //创建菜单条
        JMenuBar menuBar=new JMenuBar();
         
        //创建文件菜单
        JMenu fileMenu=new JMenu("文件");
        fileMenu.setMnemonic('F');//快捷键
        fileMenu.setFont(bigfont);
        //把菜单添加到菜单栏
        menuBar.add(fileMenu);
        //创建文件菜单项
     
        JMenuItem open=new JMenuItem("打开..");
        open.addActionListener(new ActionListener() {
             
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                statusLabel.setText("读取文件");
                JFrame jfFrame=new JFrame("选择一个RSS文件");
                jfFrame.setVisible(true);
                jfFrame.setBounds(1400, 500, 600, 400);
                jfFrame.setLayout(null);
                jfFrame.add(getComboBox()).setBounds(150, 50, 300, 100);
                JButton read=new JButton("确定");
                read.setFont(bigfont);
                jfFrame.add(read).setBounds(50, 200, 200, 100);
                read.addActionListener(readListener);
                read.addActionListener(new ActionListener() {
                     
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        // TODO Auto-generated method stub
                        jfFrame.dispose();
                    }
                });
                JButton exit=new JButton("取消");
                exit.setFont(bigfont);
                jfFrame.add(exit).setBounds(300, 200, 200, 100);
                exit.addActionListener(new ActionListener() {
                     
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        // TODO Auto-generated method stub
                        jfFrame.dispose();
                    }
                });
            }
        });
        open.setFont(bigfont);
        JMenuItem save=new JMenuItem("保存");
        save.setFont(bigfont);
        save.addActionListener(saveListener);
        JMenuItem exit=new JMenuItem("退出");
        exit.addActionListener(new ActionListener() {
             
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                System.exit(0);
                statusLabel.setText("退出");
            }
        });
        exit.setFont(bigfont);
        //添加菜单项到菜单
        fileMenu.add(open);
        fileMenu.add(new JSeparator());//设置分割线
        fileMenu.add(save);
        fileMenu.add(new JSeparator());//设置分割线
        fileMenu.add(exit);
         
        //创建查询菜单
        JMenu queryMenu=new JMenu("查询");
        queryMenu.setFont(bigfont);
        queryMenu.setMnemonic('Q');//快捷键
        menuBar.add(queryMenu);
        //创建查询菜单项
        JMenuItem queryTitle=new JMenuItem("通过标题查找");
        queryTitle.setFont(bigfont);
        JMenuItem queryAutor=new JMenuItem("通过作者查找");
        queryAutor.setFont(bigfont);
        queryAutor.setEnabled(false);
        queryMenu.add(queryAutor);
        queryMenu.add(queryTitle);
        //帮助菜单
        JMenu helpMenu=new JMenu("帮助");
        helpMenu.setFont(bigfont);
        helpMenu.setMnemonic('h');
        JMenuItem aboutAutor=new JMenuItem("关于作者");
        aboutAutor.setFont(bigfont);
        aboutAutor.addActionListener(new ActionListener() {
             
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                 JFrame jFrame=new JFrame("关于作者");
                 jFrame.setVisible(true);
                 jFrame.setSize(800, 400);
                 jFrame.setLocationRelativeTo(null);
                 JLabel jLabel=new JLabel("<html><body>作者:北京交通大学 段学宇<br>日期:2017年10月29日</body></html>");
                 jLabel.setFont(bigfont);
                 jFrame.add(jLabel).setSize(600,200);
            }
        });
        helpMenu.add(aboutAutor);
        menuBar.add(helpMenu);
        return menuBar;
    }
    //创建工具栏
    private JToolBar getJToolBar(){
         
        JToolBar toolBar=new JToolBar();
        //添加工具栏组件
         
        toolBar.add(getSaveButton());//保存按钮
        toolBar.add(getQueryButton());//查询按钮
        toolBar.add(getmoreButton());//访问按钮
        toolBar.setFloatable(false);
        return toolBar;
         
         
         
         
         
         
         
         
    }
    //创建菜单面板
    private JPanel getQueryPanel(){
        JPanel menuPanel=new JPanel();
         
         
        menuPanel.add(new Label("查询标题")).setFont(bigfont);
        menuPanel.add(getNewstitleText());
         
         
        return menuPanel;
    }
    //创建表格面板
    private JScrollPane getTabelSollPanel(){
        JScrollPane tableSolPanel=new JScrollPane();
        tableSolPanel.setViewportView(getNewsTitleTable());
        tableSolPanel.setPreferredSize(new Dimension(1300, 650));
         
        return tableSolPanel;
         
    }
    //创建文本框面板
    private JScrollPane getTextSolPanel(){
        JScrollPane messageSolePanel=new JScrollPane();
        messageSolePanel.setViewportView(getTextPaneNewsDescription());
        messageSolePanel.setPreferredSize(new Dimension(1300, 650));
        return messageSolePanel;
    }
    /**控件初始化*/
    public JComboBox getComboBox(){
        if(comboBox==null){
            comboBox=new JComboBox();
            comboBox.setFont(bigfont);
            comboBox.addItem(new URL("腾讯-国际新闻","rss_newswj.xml"));
            comboBox.addItem(new URL("腾讯-国内新闻","rss_newsgn.xml"));
            comboBox.addItem(new URL("新浪-体育新闻","sports.xml"));
            comboBox.addItem(new URL("新浪-社会新闻","focus15.xml"));
             
        }
        return comboBox;
    }
    //读取按钮
    public JButton getReadButton(){
        if(readButton==null){
            readButton=new JButton("读取");
            readButton.setFont(bigfont);
            readButton.setToolTipText("读取文件");//按钮提示信息
            readButton.addActionListener(readListener);
             
        }
        return readButton;
    }
    //保存按钮
    public JButton getSaveButton(){
        if(saveButton ==null){
         
            ImageIcon icon=new ImageIcon("image/保存.png");
            //ImageIcon icon=new ImageIcon("timg.jpg");
             
            saveButton =new JButton(icon);
 
            saveButton.setToolTipText("保存文件");//按钮提示信息
            saveButton.addActionListener(saveListener);
             
             
         
             
             
             
        }
        return saveButton ;
    }
    //访问按钮
    public JButton getmoreButton(){
        if(moreButton ==null){
            ImageIcon icon=new ImageIcon("image/访问.png");
            moreButton =new JButton(icon);
            moreButton.setToolTipText("访问该新闻所在页面");//按钮提示信息
            moreButton.addActionListener(new ActionListener() {
                 
                @Override
                public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub
                    java.net.URI uri;
                    try {
                        statusLabel.setText("访问外部新闻");
                        uri = new java.net.URI(uriaddress);
                          java.awt.Desktop.getDesktop().browse(uri);
                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                     
                }
            });
             
        }
        return moreButton ;
    }
    //查询按钮
    public JButton  getQueryButton(){
        if(queryButton ==null){
            ImageIcon icon=new ImageIcon("image/查询.png");
            queryButton =new JButton(icon);
            queryButton.addActionListener(new ActionListener() {
                 
                @Override
                public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub
                    statusLabel.setText("查询文件");
                }
            });
         
             
        }
        return queryButton;
    }
    //详细新闻显示框体
    public JTextField getNewstitleText(){
        if(newstitleText==null){
            newstitleText=new JTextField();
            newstitleText.setColumns(22);
            newstitleText.setFont(bigfont);
             
             
        }
        return newstitleText;
    }
 
    //新闻标题栏
    public JTable  getNewsTitleTable(){
        if(newsTitleTable==null){
            //创建新闻标题表格
            newsTitleTable=new JTable();
            //创建表格中的数据模型对象
            myTableModel defaultModel=new myTableModel(new Object [50][9],new String [] {"主题", "接受时间", "发布时间", "作者"});
         
            newsTitleTable.setModel(defaultModel);
            newsTitleTable.getTableHeader().setFont(bigfont);
            newsTitleTable.setFont(font);
            newsTitleTable.setRowHeight(50);
            //设置表格的行高
            newsTitleTable.setEnabled(true);
             
            newsTitleTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//设置居中
            /**
             * 标题表格监听器,创建新行事件
             * */
            newsTitleTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
                 
                @Override
                public void valueChanged(ListSelectionEvent arg0) {
                    // TODO Auto-generated method stub
                    //获得选中行的值
                    int row=newsTitleTable.getSelectedRow();
                    try {
                     
                        service.showMessage(row);
                        uriaddress=service.geturiaddress(); //获得网址
                        statusLabel.setText(service.getTitle());//状态栏
                    } catch (Exception ex) {
                        // TODO: handle exception
                        //获得处理BadLocation异常信息
                        showError("文档转换操作异常:"+ex.getMessage());
                    }
                }
            } );
        }
        return newsTitleTable;
    }
    protected void showError(String string) {
        // TODO Auto-generated method stub
        System.out.println(string);
    }
 
    public JTextPane getTextPaneNewsDescription(){
        if(textPaneNewsDescripation==null){
            //创建文本域
            textPaneNewsDescripation=new JTextPane();
            //不可编辑
            textPaneNewsDescripation.setEditable(false);
            textPaneNewsDescripation.setFont(bigfont);
             
        }
        return textPaneNewsDescripation;
    }
    private JPanel getStatusBar(){
        JPanel panel=new JPanel();
        panel.setLayout(new FlowLayout(FlowLayout.LEFT));
        panel.add(new Label("   "));
        statusLabel=new JLabel("北京交通大学软件学院");
        statusLabel.setFont(bigfont);
        panel.add(statusLabel);
        return panel;
    }
    ActionListener readListener=new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
             
            /**目标文件*/
            String inFile;
            /**显示错误信息*/
            String errorInfo="";
            //网址字符串
            String address="";
            //获得下拉框选中项
            Object o=comboBox.getSelectedItem();
            //获得工程目录
            String dir=System.getProperty("user.dir");
            //获得下拉框选中项的文件名,赋值给网址字符串
            address=((URL)o).getAddress();
            //获得解析文件完整路径
            inFile=dir+"\\NewfeedFiles\\"+address;
                           
            try{
                //转编路径为UTF-8
                inFile=URLDecoder.decode(inFile, "utf-8");
             
                service.fileDo(inFile);
             
            }catch(IOException ioe){
                //获得处理IO异常信息
                errorInfo="系统找不到指定文件";
            }catch(JDOMException jde){
                errorInfo="解析文件异常";
                jde.printStackTrace();
            }finally{
                showError(errorInfo);
            }
             
        }
             
             
    };
 
    ActionListener saveListener=new ActionListener() {
         
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            statusLabel.setText("保存文件");
            JFileChooser chooser = new JFileChooser();
            chooser.setFileFilter(new FileNameExtensionFilter("文本文件(.txt)", "txt"));
            String path=null;
            JPanel parent = new JPanel();
            int returnVal = chooser.showOpenDialog(parent);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
            path=chooser.getSelectedFile().getPath();
            }
            String filename=chooser.getSelectedFile().toString();
             
             
             
            try {
                 
                String text=null;
                text=service.getPassage();
                 if(text!=null){
                     File file=new File(filename+".txt");
                    FileWriter out = new FileWriter(file,true);
                     out.write(service.getPassage());
                     System.out.println("保存成功,路径为"+filename+".txt");
                     out.close();
                      
                     JFrame jFrame=new JFrame("保存成功");
                     jFrame.setVisible(true);
                     jFrame.setSize(600, 400);
                     jFrame.setLocationRelativeTo(null);
                     JLabel jLabel=new JLabel("成功保存该内容!");
                     jLabel.setFont(bigfont);
                     jFrame.add(jLabel).setSize(400,200);
                 }
                 else{
                     System.out.println("保存失败");
                     JFrame jFrame=new JFrame("保存失败");
                     jFrame.setVisible(true);
                     jFrame.setSize(800, 400);
                     jFrame.setLocationRelativeTo(null);
                     JLabel jLabel=new JLabel("请在列表中选择一个要保存的内容!");
                     jLabel.setFont(bigfont);
                     jFrame.add(jLabel).setSize(600,200);
                      
                      
                 }
                  
                  
                 
                  
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
         
     
                 
                 
                 
                 
                 
                 
             
        }
    };
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
}

[源代码打包下载]




网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...