版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、文件名稱功能MainFrame.javaStoneForest應(yīng)用的主界面框架MusicPanel.java這個(gè)類構(gòu)建VCD的主面板(panel)MusicDetailsDialog.java這個(gè)類顯示VCD詳細(xì)信息對(duì)話框MusicEntryDialog.java這個(gè)類提供VCD詳細(xì)信息輸入框(可選,本項(xiàng)目未使用)TrackEntryDialog這個(gè)類提供VCD歌曲信息輸入框(可選,本項(xiàng)目未使用)表2-2 數(shù)據(jù)服務(wù)類列表文件名稱功能DataAccessor.java這個(gè)抽象類定義了如何讀取一個(gè)數(shù)據(jù)文件MusicDataAccessor.java音樂(lè)VCD數(shù)據(jù)讀取的實(shí)現(xiàn)類,繼承了DataAcce
2、ssorHandler.java這個(gè)類是基于socket連接的多線程處理器MusicDataClient.java這個(gè)類連接數(shù)據(jù)服務(wù)器來(lái)獲得數(shù)據(jù)MusicDataServer.java這個(gè)類提供數(shù)據(jù)服務(wù)StoneForestProtocol.java這個(gè)接口包含數(shù)據(jù)服務(wù)的協(xié)議信息表2-3 工具類列表文件名稱功能StoneForest.java這個(gè)類是整個(gè)應(yīng)用的主(main)入口類Duration.java這個(gè)類描述時(shí)間的長(zhǎng)度。 它包括小時(shí),分和秒Track.java這個(gè)類代表歌曲VCD數(shù)據(jù)Recording.java這個(gè)抽象類代表音樂(lè)VCD數(shù)據(jù),比Track含更多信息MusicRecordin
3、g.java這個(gè)類代表音樂(lè)VCD數(shù)據(jù), 繼承了Recording表2-4 其他文件名稱功能music.db這個(gè)文件以特定格式提供數(shù)據(jù)runjavadoc.bat這個(gè)批處理命令可以用來(lái)生成java doc文檔docs這個(gè)文件夾用來(lái)放置生成的java doc文檔images這個(gè)文件夾用來(lái)放置項(xiàng)目所需的圖片MainFrameimport javax.swing.*;import java.awt.*;import java.awt.event.*;/* * StoneForest應(yīng)用的主框架 */public class MainFrame extends JFrame /* * tabbed pa
4、ne組件 */protected JTabbedPane tabbedPane;/* * 音樂(lè)CD panel */protected MusicPanel musicPanel;/* * 默認(rèn)構(gòu)造方法 */public MainFrame() setTitle("歡迎使用StoneForest應(yīng)用! ");Container container = this.getContentPane();container.setLayout(new BorderLayout();tabbedPane = new JTabbedPane();musicPanel = new Musi
5、cPanel(this);tabbedPane.addTab("音樂(lè)", 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("網(wǎng)絡(luò).");openMenu.add(networkMenuItem);JMenuItem webMenuItem = new JMenuItem("互聯(lián)網(wǎng).");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("關(guān)于");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');/設(shè)定快捷鍵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);/* * 設(shè)定和選擇外觀 * */protected void setupLookAndFeelMenu(JMenuBar theMenuBar) UIManager.LookAndFeelInfo lookAndFeelInfo = UIManager.getInstalledLookAndFeels();JMenu lookAndFeelMenu = new JMenu("選項(xiàng)");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);/* * "退出"事件處理內(nèi)部類. */class ExitActionListener implements ActionListener public void actionPerformed(ActionEvent event) exit();/* * "關(guān)閉窗口"事件處理內(nèi)部類. */class WindowCloser extends Windo
13、wAdapter /* * let's call our exit() method defined above */public void windowClosing(WindowEvent e) exit();/* * "外觀"選擇監(jiān)聽類 * */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();/* * "關(guān)于"菜單監(jiān)聽類 */ 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.*;/* * 這個(gè)類構(gòu)建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("選擇音樂(lè)目錄");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("詳細(xì).");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, "網(wǎng)絡(luò)問(wèn)題 " + exc, "網(wǎng)絡(luò)問(wèn)題", 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, "網(wǎng)絡(luò)問(wèn)題: " + exc, "網(wǎng)絡(luò)問(wèn)題", 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.*;/* * 這個(gè)類顯示CD詳細(xì)信息對(duì)話框 */public class MusicDetailsDialog extends JDi
29、alog protected MusicRecording myRecording;protected Frame parentFrame;public MusicDetailsDialog(Frame theParentFrame, MusicRecording theMusicRecording) this(theParentFrame, "光盤詳細(xì)信息 " + 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("長(zhǎng)度: " + runningTime + " 分.");durationLabel.setForeground(Color.black);infoPanel.add(durationLabel, c
35、);JLabel priceLabel = new JLabel("價(jià)格: " + 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);/* 處理按鈕的內(nèi)部類*/class OkButtonActionListener implements ActionListener public void actionPerformed(ActionEvent event)setVisible(fa
41、lse);DataAccessorimport java.util.*;/* * 這個(gè)抽象類定義了如何讀取一個(gè)數(shù)據(jù)文件。 它提供的方法可以用來(lái)獲得CD的分類和具體的CD信息 * */public abstract class DataAccessor /* * 存放CD信息的HashMap/hashtable . * */protected HashMap dataTable;/* * 最近增加的CD集合 * */ protected ArrayList recentRecordingList; /* * 默認(rèn)構(gòu)造方法 */ 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;/* * 在內(nèi)存中增加新的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");/* * 從文件中讀取數(shù)據(jù) */public abstract void load();/* * 向文件中保存數(shù)據(jù) */public abstract void save();/* * 日志方法. */protected void log(Object msg) System.out.println("數(shù)據(jù)存取類Data Accessor: " + msg);Handlerimport java.io.*;import .*;import java.util.*;/* * 這個(gè)類是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("錯(cuò)誤代碼");catch (IOException exc) log(exc);protected void opGetMusicCategories() try ArrayList categoryList = myMusicDataAccessor.getCategories();outputToClient.writeObjec
50、t(categoryList);outputToClient.flush();log("發(fā)出 " + categoryList.size() + " 類別信息到客戶端到客戶端.");catch (IOException exc) log("發(fā)生異常: " + exc);protected void opGetMusicRecordings() try log("讀取份類信息");String category = (String) inputFromClient.readObject();log("類別是 " + category);ArrayList recordingList = myMusicDataAccessor.getRecordings(category);outputToClient.writeObject(recordingList);outputToClient.flush();log("發(fā)出 " + recordingList.size(
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年春季自考法學(xué)專業(yè)本科刑法學(xué)考題含解析
- 影響保安工作效率的管理因素計(jì)劃
- 2024年藥物控釋材料項(xiàng)目建議書
- 旅游行業(yè)的成本控制計(jì)劃培訓(xùn)
- 滅火器種類及使用方法培訓(xùn)
- 2022年下半年刑法學(xué)自考試題含解析
- 生物植物細(xì)胞第1課時(shí)(二)課件2024-2025學(xué)年人教版生物七年級(jí)上冊(cè)
- 2025屆湖南省衡陽(yáng)市五校5月高考模練習(xí)(一)化學(xué)試題含解析
- 2025屆鶴崗市重點(diǎn)中學(xué)廣東高考全真化學(xué)試題模擬試卷含解析
- 2025屆河南省漯河市第五高級(jí)中學(xué)高三下學(xué)期期初聯(lián)考化學(xué)試題試卷含解析
- 漿砌石勞務(wù)分包協(xié)議
- 體育免修申請(qǐng)表(模板)
- 幼兒園小班科學(xué):《有趣的溶解》 課件
- 一類醫(yī)療器械備案 8.符合性聲明
- 扎根理論-課件
- 產(chǎn)后抑郁癥知識(shí)講座共36張課件
- GB-T 14321-2022 剛玉磨料中α-Al2O3相X射線定量測(cè)定方法
- 安全生產(chǎn)事故隱患排查治理實(shí)施辦法
- pcs-9882ad說(shuō)明書-國(guó)內(nèi)中文版
- 小學(xué)綜合實(shí)踐一年級(jí)上冊(cè)第4單元《主題活動(dòng)二:智力大比拼》教案
- 小學(xué)語(yǔ)文人教五年級(jí)上冊(cè)(統(tǒng)編)第五單元-群文閱讀(教案)
評(píng)論
0/150
提交評(píng)論