[java]代码库
package com.ruanko.view;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.KeyStroke;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import com.ruanko.model.Channel;
import com.ruanko.model.News;
import com.ruanko.service.RSSService;
import com.ruanko.service.UpdateThread;
import com.runko.dao.NewsDao;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import java.awt.Color;
@SuppressWarnings({ "serial", "unused" })
public class JMainFrame extends JFrame
{
/**
* Description: 主界面窗体
* @param WIDTH 静态全局变量,设置窗体的宽度
* @param HEIGHT 全局静态变量,设置窗体的高度
* @param TITLE 全局静态变量,设置窗体标题
*/
RSSService rssService = null;
private NewsDao rssDao;
private final static int WIDTH = 800;
private final static int HEIGHT = 650;
private final static String TITLE = "RSS阅读器";
private JButton jbExport; //导出按钮
private JButton jbUpdate; //更新按钮
private JButton jbExit; //退出按钮
private JTextArea jtaContent; //新闻内容文本区
private DefaultTableModel dtmTableModel; //表格数据模型
private JTable jtTable; //表格
private List<News> newsList; //新闻内容列表
private static List<Channel> channelList;
private ShowJdialog showJdialogMessage = new ShowJdialog(); //提示窗口
private JLabel label;
private ReadActionListener readactionlistener;
private int select = 0; //保存选中的新闻选项
public JMainFrame()
{
rssService = new RSSService();
this.setTitle(TITLE);
this.setSize(WIDTH,HEIGHT);
this.setCenter();
this.setJMenuBar(getJMBMy());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(getJPMain());
readactionlistener = new ReadActionListener();
}
private void setCenter() //窗口居中显示
{
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
this.setLocation((screenSize.width - WIDTH)/2,(screenSize.height - HEIGHT)/2);
}
private JMenuBar getJMBMy() //菜单栏
{
channelList = rssService.getChannelList();
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("文件(F)");
menu.setMnemonic(KeyEvent.VK_F);
JMenu help = new JMenu("帮助(H)");
help.setMnemonic(KeyEvent.VK_H);
JMenu view = new JMenu("新闻列表(O)");
view.setMnemonic(KeyEvent.VK_O);
/**
* 添加新闻列表
*/
JMenuItem[] view_item = new JMenuItem[channelList.size()];
for(int i = 0; i < channelList.size(); i++)
{
Channel channel = channelList.get(i);
view_item[i] = new JMenuItem(channel.getName());
view.add(view_item[i]);
view_item[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
readactionlistener.actionPerformed(e);
}
});
}
JMenuItem item_dao = new JMenuItem("导出(D)",KeyEvent.VK_D);
item_dao.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,ActionEvent.CTRL_MASK));
item_dao.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
ExportActionListener export = new ExportActionListener();
export.actionPerformed(e);
}
});
JMenuItem item_ESC = new JMenuItem("退出(X)",KeyEvent.VK_X);
item_ESC.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.CTRL_MASK));
item_ESC.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
menuBar.add(menu);
menuBar.add(help);
menu.add(view);
menu.add(item_dao);
menu.add(item_ESC);
return menuBar;
}
class ReadActionListener implements ActionListener
{
/**
* 新闻内容读取功能
*
*/
public void actionPerformed(ActionEvent e)
{
showNews(e.getActionCommand());
}
}
private void showNews(String name)
{
/**
* 把新闻显示功能独立成一个方法
*/
Channel selectedChannel = new Channel();
for(int i = 0; i < channelList.size(); i++)
{
selectedChannel = channelList.get(i);
if(selectedChannel.getName() == name)
{
select = i;
break;
}
}
String filePath = selectedChannel.getFilePath();
newsList = rssService.getNewsList(filePath);
jtaContent.setText("");
showTable(newsList);
}
class ExportActionListener implements ActionListener
{
/**
* 导出按钮的动作事件
*/
public void actionPerformed(ActionEvent e)
{
if(newsList == null)
{
Channel channel = channelList.get(select);
String filePath = channel.getFilePath();
newsList = rssService.getNewsList(filePath);
}
if(rssService.save())
showJdialogMessage.showJdialog("新闻信息保存成功!");
else
showJdialogMessage.showJdialog("新闻信息保存失败!");
}
}
private JPanel getJPMain() //窗口面板
{
JPanel jpMain = new JPanel(); //主面板
jpMain.setLayout(new BorderLayout());
jpMain.add(getJTBMy(), BorderLayout.NORTH);
jpMain.add(getJSPLeft() ,BorderLayout.CENTER);
jpMain.add(getJSBMy(),BorderLayout.SOUTH);
return jpMain;
}
private JSplitPane getJSPLeft() //左右面板分割
{
JSplitPane jsLeft = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
jsLeft.setDividerLocation(180);
jsLeft.setLeftComponent(getJTree());
jsLeft.setRightComponent(getJSPClientArea());
return jsLeft;
}
private JScrollPane getJTree() //获取树形控件
{
String[] name = new String[10];
for(int i = 0; i < channelList.size(); i++)
{
Channel channel = channelList.get(i);
name[i] = channel.getName();
}
DefaultMutableTreeNode root = new DefaultMutableTreeNode("新闻列表");
DefaultMutableTreeNode t1 = new DefaultMutableTreeNode("腾讯新闻");
DefaultMutableTreeNode t2 = new DefaultMutableTreeNode("新浪新闻");
DefaultMutableTreeNode t1_1 = new DefaultMutableTreeNode(name[0]);
DefaultMutableTreeNode t1_2 = new DefaultMutableTreeNode(name[1]);
DefaultMutableTreeNode t2_1 = new DefaultMutableTreeNode(name[2]);
DefaultMutableTreeNode t2_2 = new DefaultMutableTreeNode(name[3]);
root.add(t1);
root.add(t2);
t1.add(t1_1);
t1.add(t1_2);
t2.add(t2_1);
t2.add(t2_2);
final JTree jtree = new JTree(root);
jtree.addTreeSelectionListener(new TreeSelectionListener()
{
public void valueChanged(TreeSelectionEvent e)
{
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)jtree.getLastSelectedPathComponent();
if(selectedNode == null)
return ;
if(selectedNode.isLeaf())
{
showNews(selectedNode.toString());
}
}
});
JScrollPane jtTreePanel = new JScrollPane(jtree);// 将树放在滚动面板中
return jtTreePanel;
}
public JToolBar getJTBMy() //工具栏
{
JToolBar jtTool = new JToolBar();
jtTool.setLayout(new FlowLayout(FlowLayout.LEFT));
jtTool.add(getJBExport());
jtTool.add(getJBUpdate());
jtTool.add(getJBExit());
return jtTool;
}
private JSplitPane getJSPClientArea() //分割面板
{
JSplitPane jspVertical = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
jspVertical.setDividerLocation(240);
jspVertical.setLeftComponent(getJSPTable());
jspVertical.setRightComponent(getJSPContent());
return jspVertical;
}
private JPanel getJSBMy() //状态栏
{
JPanel status = new JPanel();
status.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel jlStatus = new JLabel("www.ruanko.com");
status.add(jlStatus);
return status;
}
private JScrollPane getJSPContent() //新闻文本区
{
JScrollPane jspContent = null;
if(jtaContent == null)
{
jtaContent = new JTextArea();
jtaContent.setLineWrap(true); //文本区的换行策略
jspContent = new JScrollPane(jtaContent);
jspContent.setPreferredSize(new Dimension(780, 260));
}
return jspContent;
}
private void showTable(List<News> newsList) //新闻内容以表格显示
{
//清空表格的内容
int rowCount = dtmTableModel.getRowCount();
while(rowCount > 0)
{
dtmTableModel.removeRow(0);
rowCount--;
}
//遍历新闻内容列表,将相应的新闻内容显示到表格中
for(int i = 0; i < newsList.size(); i++ )
{
News news = newsList.get(i);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd- HH:mm:ss");
Date date = new Date();
String currentDate = dateFormat.format(date);
String[] data = {news.getTitle(),currentDate,news.getPubDate(),news.getAuthor()};
dtmTableModel.addRow(data);
}
}
private JScrollPane getJSPTable() //表格内容单击事件处理
{
JScrollPane jspTable = null;
if(jtTable == null)
{
dtmTableModel = new DefaultTableModel();
dtmTableModel.addColumn("主题");
dtmTableModel.addColumn("接收时间");
dtmTableModel.addColumn("发布时间");
dtmTableModel.addColumn("作者");
jtTable = new JTable(dtmTableModel);
jtTable.addMouseListener(new MouseAdapter()
{
/**
* 重写mouseClicked() 方法处理鼠标点击事件
*/
public void mouseClicked(MouseEvent e)
{
if(e.getButton() == MouseEvent.BUTTON1)
{
int selectedRow = jtTable.getSelectedRow();
News selectedNews = newsList.get(selectedRow);
jtaContent.setText(rssService.newsToString(selectedNews));
}
}
});
jspTable = new JScrollPane(jtTable);
}
return jspTable;
}
private JButton getJBExport() //导出按钮
{
if(jbExport == null)
{
ImageIcon icon = new ImageIcon("images/export.png");
jbExport = new JButton(icon);
jbExport.setToolTipText("导出");
jbExport.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
ExportActionListener export = new ExportActionListener();
export.actionPerformed(e);
}
});
}
return jbExport;
}
private JButton getJBUpdate() //更新按钮
{
if(jbUpdate == null)
{
ImageIcon icon = new ImageIcon("images/update.png");
jbUpdate = new JButton(icon);
jbUpdate.setToolTipText("更新新闻内容");
jbUpdate.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
UpdateThread up = new UpdateThread();
up.start();
}
});
}
return jbUpdate;
}
private JButton getJBExit() //退出按钮
{
if(jbExit == null)
{
ImageIcon icon = new ImageIcon("images/exit.png");
jbExit = new JButton(icon);
jbExit.setToolTipText("退出RSS阅读器");
jbExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
return jbExit;
}
}
[源代码打包下载]
初级程序员
by: 姽婳 发表于:2016-09-05 09:26:19 顶(0) | 踩(0) 回复
怎么没有源码呀!
回复评论