




已阅读5页,还剩40页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
文件名称功能MainFrame.javaStoneForest应用的主界面框架MusicPanel.java这个类构建VCD的主面板(panel)MusicDetailsDialog.java这个类显示VCD详细信息对话框MusicEntryDialog.java这个类提供VCD详细信息输入框(可选,本项目未使用)TrackEntryDialog这个类提供VCD歌曲信息输入框(可选,本项目未使用)表2-2 数据服务类列表文件名称功能DataAccessor.java这个抽象类定义了如何读取一个数据文件MusicDataAccessor.java音乐VCD数据读取的实现类,继承了DataAccessorHandler.java这个类是基于socket连接的多线程处理器MusicDataClient.java这个类连接数据服务器来获得数据MusicDataServer.java这个类提供数据服务StoneForestProtocol.java这个接口包含数据服务的协议信息表2-3 工具类列表文件名称功能StoneForest.java这个类是整个应用的主(main)入口类Duration.java这个类描述时间的长度。 它包括小时,分和秒Track.java这个类代表歌曲VCD数据Recording.java这个抽象类代表音乐VCD数据,比Track含更多信息MusicRecording.java这个类代表音乐VCD数据, 继承了Recording表2-4 其他文件名称功能music.db这个文件以特定格式提供数据runjavadoc.bat这个批处理命令可以用来生成java doc文档docs这个文件夹用来放置生成的java doc文档images这个文件夹用来放置项目所需的图片MainFrameimport javax.swing.*;import java.awt.*;import java.awt.event.*;/* * StoneForest应用的主框架 */public class MainFrame extends JFrame /* * tabbed pane组件 */protected JTabbedPane tabbedPane;/* * 音乐CD panel */protected MusicPanel musicPanel;/* * 默认构造方法 */public MainFrame() setTitle(欢迎使用StoneForest应用! );Container container = this.getContentPane();container.setLayout(new BorderLayout();tabbedPane = new JTabbedPane();musicPanel = new MusicPanel(this);tabbedPane.addTab(音乐, musicPanel);container.add(BorderLayout.CENTER, tabbedPane);JMenuBar myMenuBar = new JMenuBar();JMenu fileMenu = new JMenu(文件);JMenu openMenu = new JMenu(打开);JMenuItem localMenuItem = new JMenuItem(本地硬盘.);openMenu.add(localMenuItem);JMenuItem networkMenuItem = new JMenuItem(网络.);openMenu.add(networkMenuItem);JMenuItem webMenuItem = new JMenuItem(互联网.);openMenu.add(webMenuItem);fileMenu.add(openMenu);JMenuItem saveMenuItem = new JMenuItem(保存);fileMenu.add(saveMenuItem);JMenuItem exitMenuItem = new JMenuItem(退出);fileMenu.add(exitMenuItem);myMenuBar.add(fileMenu);exitMenuItem.addActionListener(new ExitActionListener();setupLookAndFeelMenu(myMenuBar);JMenu helpMenu = new JMenu(帮助);JMenuItem aboutMenuItem = new JMenuItem(关于);helpMenu.add(aboutMenuItem);myMenuBar.add(helpMenu);aboutMenuItem.addActionListener(new AboutActionListener();this.setJMenuBar(myMenuBar);setSize(500, 400);setLocation(100, 100);this.addWindowListener(new WindowCloser();fileMenu.setMnemonic(f);exitMenuItem.setMnemonic(x);helpMenu.setMnemonic(h);aboutMenuItem.setMnemonic(a);/设定快捷键exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK);saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK);aboutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK);/* * 设定和选择外观 * */protected void setupLookAndFeelMenu(JMenuBar theMenuBar) UIManager.LookAndFeelInfo lookAndFeelInfo = UIManager.getInstalledLookAndFeels();JMenu lookAndFeelMenu = new JMenu(选项);JMenuItem anItem = null;LookAndFeelListener myListener = new LookAndFeelListener();try for (int i=0; i 0) clearButton.setEnabled(true);else clearButton.setEnabled(false);catch (IOException exc) JOptionPane.showMessageDialog(this, 网络问题: + exc, 网络问题, JOptionPane.ERROR_MESSAGE);System.exit(1);class GoActionListener implements ActionListener public void actionPerformed(ActionEvent event) populateListBox();class DetailsActionListener implements ActionListener public void actionPerformed(ActionEvent event) int index = musicListBox.getSelectedIndex();MusicRecording myMusicRecording = (MusicRecording) musicArrayList.get(index);MusicDetailsDialog myDetailsDialog = new MusicDetailsDialog(parentFrame, myMusicRecording);myDetailsDialog.setVisible(true);class ExitActionListener implements ActionListener public void actionPerformed(ActionEvent event) parentFrame.exit();class ClearActionListener implements ActionListener public void actionPerformed(ActionEvent event) Object noData = new Object1;musicListBox.setListData(noData);categoryComboBox.setSelectedIndex(0);class GoItemListener implements ItemListener public void itemStateChanged(ItemEvent event) if (event.getStateChange() = ItemEvent.SELECTED) populateListBox();class MusicListSelectionListener implements ListSelectionListener public void valueChanged(ListSelectionEvent event) if (musicListBox.isSelectionEmpty() detailsButton.setEnabled(false);else detailsButton.setEnabled(true);MusicDetailsDialogimport java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;import java.util.*;/* * 这个类显示CD详细信息对话框 */public class MusicDetailsDialog extends JDialog protected MusicRecording myRecording;protected Frame parentFrame;public MusicDetailsDialog(Frame theParentFrame, MusicRecording theMusicRecording) this(theParentFrame, 光盘详细信息 + theMusicRecording.toString(), theMusicRecording);public MusicDetailsDialog(Frame theParentFrame, String theTitle, MusicRecording theMusicRecording) super(theParentFrame, theTitle, true);myRecording = theMusicRecording;parentFrame = theParentFrame;buildGui();private void buildGui() Container container = this.getContentPane();container.setLayout(new BorderLayout();JPanel topPanel = new JPanel();topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS);JPanel infoPanel = new JPanel();infoPanel.setBorder(new EmptyBorder(10, 10, 0, 10);infoPanel.setLayout(new GridBagLayout();GridBagConstraints c = new GridBagConstraints();c.gridx = 0;c.gridy = 1;c.gridwidth = 3;c.weightx = 0.0;c.weighty = 0.0;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.WEST;c.insets = new Insets(10, 0, 2, 10);JLabel artistLabel = new JLabel(歌手: + myRecording.getArtist();artistLabel.setForeground(Color.black);infoPanel.add(artistLabel, c);c.gridy = GridBagConstraints.RELATIVE;c.insets = new Insets(2, 0, 10, 10);JLabel titleLabel = new JLabel(歌名: + myRecording.getTitle();titleLabel.setForeground(Color.black);infoPanel.add(titleLabel, c);JLabel categoryLabel = new JLabel(类别: + myRecording.getCategory();c.insets = new Insets(2, 0, 2, 0);categoryLabel.setForeground(Color.black);infoPanel.add(categoryLabel, c);Duration theDuration = myRecording.getDuration();int runningTime = theDuration.getTotalSeconds() / 60;JLabel durationLabel = new JLabel(长度: + runningTime + 分.);durationLabel.setForeground(Color.black);infoPanel.add(durationLabel, c);JLabel priceLabel = new JLabel(价格: + myRecording.getPrice() );c.insets = new Insets(10, 0, 2, 0);priceLabel.setForeground(Color.black);infoPanel.add(priceLabel, c);c.gridx = 3;c.gridy = 1;c.gridwidth = GridBagConstraints.REMAINDER;c.gridheight = 5;c.fill = GridBagConstraints.NONE;c.weightx = 1.0;c.weighty = 1.0;c.insets = new Insets(5, 5, 20, 0);String imageName = myRecording.getImageName();ImageIcon recordingIcon = null;JLabel recordingLabel = null;/ 读取图片tryif (imageName.trim().length() = 0) recordingLabel = new JLabel( 图片不存在 );else recordingIcon = new ImageIcon(getClass().getResource(images/music/ + imageName);recordingLabel = new JLabel(recordingIcon);catch (Exception exc)recordingLabel = new JLabel( 图片不存在 );recordingLabel.setBorder(BorderFactory.createRaisedBevelBorder();recordingLabel.setToolTipText(myRecording.getArtist();infoPanel.add(recordingLabel, c);container.add(BorderLayout.NORTH, infoPanel);Track tracksArray = myRecording.getTrackList();JList tracksListBox= new JList(tracksArray);JScrollPane tracksScrollPane = new JScrollPane(tracksListBox);TitledBorder listBorder = BorderFactory.createTitledBorder(List of Tracks);listBorder.setTitleColor(Color.black);tracksScrollPane.setBorder(listBorder);container.add(BorderLayout.CENTER, tracksScrollPane);JPanel bottomPanel = new JPanel();JButton okButton = new JButton(OK);bottomPanel.add(okButton);container.add(BorderLayout.SOUTH, bottomPanel);okButton.addActionListener(new OkButtonActionListener();this.pack();Point parentLocation = parentFrame.getLocation();this.setLocation(parentLocation.x + 50, parentLocation.y + 50);/* 处理按钮的内部类*/class OkButtonActionListener implements ActionListener public void actionPerformed(ActionEvent event)setVisible(false);DataAccessorimport java.util.*;/* * 这个抽象类定义了如何读取一个数据文件。 它提供的方法可以用来获得CD的分类和具体的CD信息 * */public abstract class DataAccessor /* * 存放CD信息的HashMap/hashtable . * */protected HashMap dataTable;/* * 最近增加的CD集合 * */ protected ArrayList recentRecordingList; /* * 默认构造方法 */ public DataAccessor() dataTable = new HashMap();recentRecordingList = new ArrayList(); /* * 获得CD分类集合 */public ArrayList getCategories() Set categorySet = dataTable.keySet();log(获得分类.);ArrayList categories = new ArrayList(categorySet);/ 排序Collections.sort(categories);log(完成获得分类!n);return categories;/* * 获得某类CD的集合 * * param 类别 */public ArrayList getRecordings(String category) ArrayList recordingList = null;log(获得CD集合信息, 它们属于: + category);recordingList = (ArrayList) dataTable.get(category);/ 排序Collections.sort(recordingList);log(完成获得CD集合信息!n);return recordingList;/* * 在内存中增加新的CD * * param被增加的CD * */public void addRecording(Recording theRecording) String category = theRecording.getCategory();log(添加新的CD: + theRecording);ArrayList recordingList = (ArrayList) dataTable.get(category);recordingList.add(theRecording);recentRecordingList.add(theRecording);log(完成添加新的CD!n);/* * 从文件中读取数据 */public abstract void load();/* * 向文件中保存数据 */public abstract void save();/* * 日志方法. */protected void log(Object msg) System.out.println(数据存取类Data Accessor: + msg);Handlerimport java.io.*;import .*;import java.util.*;/* * 这个类是socket连接的处理器 * 例如: * *Handler aHandler = new Handler(clientSocket, myMusicDataAccessor); *aHandler.start(); * * * */public class Handler extends Thread implements StoneForestProtocol protected Socket clientSocket;protected ObjectOutputStream outputToClient;protected ObjectInputStream inputFromClient;protected MusicDataAccessor myMusicDataAccessor;protected boolean done;public Handler(Socket theClientSocket, MusicDataAccessor theMusicDataAccessor) throws IOException clientSocket = theClientSocket;outputToClient = new ObjectOutputStream(clientSocket.getOutputStream();inputFromClient = new ObjectInputStream(clientSocket.getInputStream();myMusicDataAccessor = theMusicDataAccessor;done = false;public void run() try while (!done) log(等待命令.);int opCode = inputFromClient.readInt();log(opCode = + opCode);switch(opCode) case StoneForestProtocol.OP_GET_MUSIC_CATEGORIES:opGetMusicCategories();break;case StoneForestProtocol.OP_GET_MUSIC_RECORDINGS:opGetMusicRecordings();break;default:System.out.println(错误代码);catch (IOException exc) log(exc);protected void opGetMusicCategories() try ArrayList categoryList = myMusicDataAccessor.getCategories();outputToClient.writeObject(categoryList);outputToClient.flush();log(发出 + categoryList.size() + 类别信息到客户端到客户端.);catch (IOException exc) log(发生异常: + exc);protected void opGetMusicRecordings() try log(读取份类信息);String category = (String) inputFromClient.readObject();log(类别是 + category);ArrayList recordingList = myMusicDataAccessor.getRecordings(category);outputToClient.writeObject(recordingList);outputToClient.flush();log(发出 + recordingList.size() + CD信息到客户端.);catch (IOException exc) log(发生异常: + exc);exc.printStackTrace();catch (ClassNotFoundException exc) log(发生异常: + exc);exc.printStackTrace();public void setDone(boolean flag) done = flag;protected void log(Object msg) System.out.println(处理器: + msg);MusicDataAccessorimport java.util.*;import java.io.*;/* * 音乐CD数据读取的实现
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 地下车库土地租赁及车位销售合同
- 2025公务员妆容面试题及答案
- 电子商务平台与高校人才输送合作协议范本
- 企业可持续发展合理化建议合作合同
- 军官专业面试题目及答案
- 专业心态测试题及答案
- 测序成本下降策略-洞察及研究
- 2025至2030医药级甘氨酸行业发展趋势分析与未来投资战略咨询研究报告
- 消防安全核查培训内容课件
- 消防安全月培训简讯课件
- 厂房租赁合同书格式
- 标识牌的制作与安装方案
- GB/T 15934-2024电器附件电线组件和互连电线组件
- 《计算机网络技术》课程教案(完整版)
- 育肥猪购销协议书
- 《建筑工程设计文件编制深度规定》(2022年版)
- 西安交通大学出版小学信息技术五年级上册教案
- 水库清淤项目可行性研究报告
- 工程项目计价结算付款情况统计表
- DL∕T 797-2012 风力发电场检修规程
- JGJ181-2009T 房屋建筑与市政基础设施工程检测
评论
0/150
提交评论