版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、文件名称功能MainFrame.javaStoneForest应用的主界面框架MusicPanel.java这个类构建VCD的主面板(panel)MusicDetailsDialog.java这个类显示VCD详细信息对话框MusicEntryDialog.java这个类提供VCD详细信息输入框(可选,本项目未使用)TrackEntryDialog这个类提供VCD歌曲信息输入框(可选,本项目未使用)表2-2 数据服务类列表文件名称功能DataAccessor.java这个抽象类定义了如何读取一个数据文件MusicDataAccessor.java音乐VCD数据读取的实现类,继承了DataAcce
2、ssorHandler.java这个类是基于socket连接的多线程处理器MusicDataClient.java这个类连接数据服务器来获得数据MusicDataServer.java这个类提供数据服务StoneForestProtocol.java这个接口包含数据服务的协议信息表2-3 工具类列表文件名称功能StoneForest.java这个类是整个应用的主(main)入口类Duration.java这个类描述时间的长度。 它包括小时,分和秒Track.java这个类代表歌曲VCD数据Recording.java这个抽象类代表音乐VCD数据,比Track含更多信息MusicRecordin
3、g.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 pa
4、ne组件 */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 Musi
5、cPanel(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(
6、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
7、 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(abo
8、utMenuItem);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(
9、9;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,
10、ActionEvent.CTRL_MASK);/* * 设定和选择外观 * */protected void setupLookAndFeelMenu(JMenuBar theMenuBar) UIManager.LookAndFeelInfo lookAndFeelInfo = UIManager.getInstalledLookAndFeels();JMenu lookAndFeelMenu = new JMenu("选项");JMenuItem anItem = null;LookAndFeelListener myListener = new LookAndFeel
11、Listener();try for (int i=0; i < lookAndFeelInfo.length; i+) anItem = new JMenuItem(lookAndFeelInfoi.getName() + " 外观");anItem.setActionCommand(lookAndFeelInfoi.getClassName();anItem.addActionListener(myListener);lookAndFeelMenu.add(anItem);catch (Exception e) e.printStackTrace();theMen
12、uBar.add(lookAndFeelMenu);/* * 退出方法. */public void exit() setVisible(false);dispose();System.exit(0);/* * "退出"事件处理内部类. */class ExitActionListener implements ActionListener public void actionPerformed(ActionEvent event) exit();/* * "关闭窗口"事件处理内部类. */class WindowCloser extends Windo
13、wAdapter /* * let's call our exit() method defined above */public void windowClosing(WindowEvent e) exit();/* * "外观"选择监听类 * */class LookAndFeelListener implements ActionListener public void actionPerformed(ActionEvent event) String className = event.getActionCommand();try UIManager.set
14、LookAndFeel(className);SwingUtilities.updateComponentTreeUI(MainFrame.this);catch (Exception e) e.printStackTrace();/* * "关于"菜单监听类 */ class AboutActionListener implements ActionListener public void actionPerformed(ActionEvent event) String msg = "StoneForest 超值享受!"JOptionPane.sho
15、wMessageDialog(MainFrame.this, msg); MusicPanelimport javax.swing.*;import javax.swing.event.*;import java.awt.*;import java.awt.event.*;import java.util.*;import java.io.*;/* * 这个类构建CD面板 */public class MusicPanel extends JPanel protected JLabel selectionLabel;protected JComboBox categoryComboBox;pr
16、otected JPanel topPanel;protected JList musicListBox;protected JScrollPane musicScrollPane;protected JButton detailsButton;protected JButton clearButton;protected JButton exitButton;protected JPanel bottomPanel;protected MainFrame parentFrame;protected ArrayList musicArrayList;protected MusicDataCli
17、ent myDataClient;public MusicPanel(MainFrame theParentFrame) try parentFrame = theParentFrame;myDataClient = new MusicDataClient();selectionLabel = new JLabel("选择音乐目录");categoryComboBox = new JComboBox();categoryComboBox.addItem("-");ArrayList categoryArrayList = myDataClient.get
18、Categories();Iterator iterator = categoryArrayList.iterator();String aCategory;while (iterator.hasNext() aCategory = (String) iterator.next();categoryComboBox.addItem(aCategory);topPanel = new JPanel();musicListBox = new JList();musicListBox.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);musi
19、cScrollPane = new JScrollPane(musicListBox);detailsButton = new JButton("详细.");clearButton = new JButton("清空");exitButton = new JButton("退出");bottomPanel = new JPanel();this.setLayout(new BorderLayout();topPanel.setLayout(new FlowLayout(FlowLayout.LEFT);topPanel.add(sel
20、ectionLabel);topPanel.add(categoryComboBox);this.add(BorderLayout.NORTH, topPanel);this.add(BorderLayout.CENTER, musicScrollPane);bottomPanel.setLayout(new FlowLayout();bottomPanel.add(detailsButton);bottomPanel.add(clearButton);bottomPanel.add(exitButton);this.add(BorderLayout.SOUTH, bottomPanel);d
21、etailsButton.addActionListener(new DetailsActionListener();clearButton.addActionListener(new ClearActionListener();exitButton.addActionListener(new ExitActionListener();categoryComboBox.addItemListener(new GoItemListener();musicListBox.addListSelectionListener(new MusicListSelectionListener();detail
22、sButton.setEnabled(false);clearButton.setEnabled(false);catch (IOException exc) JOptionPane.showMessageDialog(this, "网络问题 " + exc, "网络问题", JOptionPane.ERROR_MESSAGE);System.exit(1);protected void populateListBox() try String category = (String) categoryComboBox.getSelectedItem();
23、if (! category.startsWith("-") musicArrayList = myDataClient.getRecordings(category);else musicArrayList = new ArrayList(); Object theData = musicArrayList.toArray();musicListBox.setListData(theData);if (musicArrayList.size() > 0) clearButton.setEnabled(true);else clearButton.setEnabled
24、(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 impl
25、ements ActionListener public void actionPerformed(ActionEvent event) int index = musicListBox.getSelectedIndex();MusicRecording myMusicRecording = (MusicRecording) musicArrayList.get(index);MusicDetailsDialog myDetailsDialog = new MusicDetailsDialog(parentFrame, myMusicRecording);myDetailsDialog.set
26、Visible(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);categoryC
27、omboBox.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 even
28、t) 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 JDi
29、alog protected MusicRecording myRecording;protected Frame parentFrame;public MusicDetailsDialog(Frame theParentFrame, MusicRecording theMusicRecording) this(theParentFrame, "光盘详细信息 " + theMusicRecording.toString(), theMusicRecording);public MusicDetailsDialog(Frame theParentFrame, String t
30、heTitle, 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.
31、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.fil
32、l = 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 Inse
33、ts(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(Co
34、lor.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
35、);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.we
36、ightx = 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(get
37、Class().getResource("images/music/" + imageName);recordingLabel = new JLabel(recordingIcon);catch (Exception exc)recordingLabel = new JLabel(" 图片不存在 ");recordingLabel.setBorder(BorderFactory.createRaisedBevelBorder();recordingLabel.setToolTipText(myRecording.getArtist();infoPanel
38、.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 Track
39、s");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);okButto
40、n.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(fa
41、lse);DataAccessorimport java.util.*;/* * 这个抽象类定义了如何读取一个数据文件。 它提供的方法可以用来获得CD的分类和具体的CD信息 * */public abstract class DataAccessor /* * 存放CD信息的HashMap/hashtable . * */protected HashMap dataTable;/* * 最近增加的CD集合 * */ protected ArrayList recentRecordingList; /* * 默认构造方法 */ public DataAccessor() dataTable =
42、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
43、的集合 * * 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 *
44、* 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("完成添加新
45、的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连接的处理器 * 例如: * <pre> *
46、Handler aHandler = new Handler(clientSocket, myMusicDataAccessor); *aHandler.start(); * </pre> * * */public class Handler extends Thread implements StoneForestProtocol protected Socket clientSocket;protected ObjectOutputStream outputToClient;protected ObjectInputStream inputFromClient;protecte
47、d MusicDataAccessor myMusicDataAccessor;protected boolean done;public Handler(Socket theClientSocket, MusicDataAccessor theMusicDataAccessor) throws IOException clientSocket = theClientSocket;outputToClient = new ObjectOutputStream(clientSocket.getOutputStream();inputFromClient = new ObjectInputStre
48、am(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:opGetMusicCat
49、egories();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.writeObjec
50、t(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(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2026年天津市苏教版高三物理选修3-1第一章电场测试卷
- 2026年老年人饮食照料与安全喂食题库(附答案)
- 桁架跨越安装记录
- 云南省昭通市镇雄县三校2025-2026学年高一上学期第一次月考物理试卷(含答案)
- 医院感染暴发控制与手卫生规范知识考试试题及答案
- 2026年青海考研(数学)考试题库及答案
- 2026年山西省吕梁市辅警人员招聘考试试题及答案
- 2026年山西省晋城市公安招聘辅警考试试卷含答案
- 2026年河南考研(数学)考试试卷(真题)答案解析
- 2026年湖北襄阳市中小学教师招聘考试题库含答案
- (2026年)三力测试官方模拟考试题库完整版(可直接刷题)
- 2026秋季开明出版社五年级上册《魅力辽宁》教学工作计划
- 2026年辽宁省员额检察官遴选考试真题及答案
- 2026年秋大象版(新教材)小学科学四年级上册教学计划及进度表
- 2026秋小学科学教科版六年级上册(新教材)教学计划附进度表
- 2026版保密教育线上培训考试题库参考答案
- 浙江巨圣氟化学有限公司4000吨-年TFE下游高端精细品技改项目环境影响报告书
- 项目1:跨境电商数据分析基础
- 麻风病皮肤查菌技术课件
- 教育学 第四章 学生与教师
- 人工智能数学基础高职PPT完整全套教学课件
评论
0/150
提交评论