SuperVCD管理系统 java版完整代码 带各种注释....

更新时间:2023-06-01 18:26:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

SuperVCD管理系统 完整代码,带各种注释,可以拿这个项目入门。

表2-2 数据服务类列表

表2-3 工具类列表

表2-4 其他

SuperVCD管理系统 完整代码,带各种注释,可以拿这个项目入门。

MainFrame

import 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);

SuperVCD管理系统 完整代码,带各种注释,可以拿这个项目入门。

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);

SuperVCD管理系统 完整代码,带各种注释,可以拿这个项目入门。

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 < lookAndFeelInfo.length; i++) {

anItem = new JMenuItem(lookAndFeelInfo[i].getName() + " 外观"); anItem.setActionCommand(lookAndFeelInfo[i].getClassName());

anItem.addActionListener(myListener);

lookAndFeelMenu.add(anItem);

}

}

catch (Exception e) {

e.printStackTrace();

}

theMenuBar.add(lookAndFeelMenu); =

SuperVCD管理系统 完整代码,带各种注释,可以拿这个项目入门。

} /** * 退出方法. */ public void exit() { } setVisible(false); dispose(); System.exit(0); /** * "退出"事件处理内部类. */ class ExitActionListener implements ActionListener { public void actionPerformed(ActionEvent event) { exit(); } } /** * "关闭窗口"事件处理内部类. */ class WindowCloser extends WindowAdapter { } /** * let's call our exit() method defined above */ public void windowClosing(WindowEvent e) { } exit(); /** * "外观"选择监听类 * */ class LookAndFeelListener implements ActionListener {

SuperVCD管理系统 完整代码,带各种注释,可以拿这个项目入门。

public void actionPerformed(ActionEvent event) {

String className = event.getActionCommand();

try {

UIManager.setLookAndFeel(className);

SwingUtilities.updateComponentTreeUI(MainFrame.this);

}

catch (Exception e) {

e.printStackTrace();

}

}

}

/**

* "关于"菜单监听类

*/

class AboutActionListener implements ActionListener {

public void actionPerformed(ActionEvent event) {

String msg = "StoneForest 超值享受!";

JOptionPane.showMessageDialog(MainFrame.this, msg);

}

}

}

MusicPanel

import javax.swing.*;

import javax.swing.event.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.io.*;

/**

SuperVCD管理系统 完整代码,带各种注释,可以拿这个项目入门。

* 这个类构建CD面板

*/

public class MusicPanel extends JPanel {

protected JLabel selectionLabel;

protected JComboBox categoryComboBox;

protected 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 MusicDataClient myDataClient;

public MusicPanel(MainFrame theParentFrame) {

try {

parentFrame = theParentFrame;

myDataClient = new MusicDataClient();

selectionLabel = new JLabel("选择音乐目录");

categoryComboBox = new JComboBox();

categoryComboBox.addItem("-------");

SuperVCD管理系统 完整代码,带各种注释,可以拿这个项目入门。

ArrayList categoryArrayList = myDataClient.getCategories(); 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); musicScrollPane = 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(selectionLabel); 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); detailsButton.addActionListener(new DetailsActionListener()); clearButton.addActionListener(new ClearActionListener());

SuperVCD管理系统 完整代码,带各种注释,可以拿这个项目入门。

exitButton.addActionListener(new ExitActionListener());

categoryComboBox.addItemListener(new GoItemListener());

musicListBox.addListSelectionListener(new MusicListSelectionListener());

detailsButton.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();

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(false);

}

}

catch (IOException exc) {

JOptionPane.showMessageDialog(this, "网络问题: " + exc, "网络问题", JOptionPane.ERROR_MESSAGE);

System.exit(1);

}

}

SuperVCD管理系统 完整代码,带各种注释,可以拿这个项目入门。

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 Object[1];

musicListBox.setListData(noData);

categoryComboBox.setSelectedIndex(0);

SuperVCD管理系统 完整代码,带各种注释,可以拿这个项目入门。

}

}

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);

}

}

}

}

MusicDetailsDialog

import 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;

SuperVCD管理系统 完整代码,带各种注释,可以拿这个项目入门。

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;

SuperVCD管理系统 完整代码,带各种注释,可以拿这个项目入门。

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; // 读取图片 try { if (imageName.trim().length() == 0) { recordingLabel = new JLabel(" 图片不存在 ");

SuperVCD管理系统 完整代码,带各种注释,可以拿这个项目入门。

}

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);

}

SuperVCD管理系统 完整代码,带各种注释,可以拿这个项目入门。

/**

* 处理按钮的内部类

*/

class OkButtonActionListener implements ActionListener {

public void actionPerformed(ActionEvent event)

{

setVisible(false);

}

}

}

DataAccessor

import java.util.*;

/**

* 这个抽象类定义了如何读取一个数据文件。

具体的CD信息

*

*/

public abstract class DataAccessor {

/**

* 存放CD信息的HashMap/hashtable .

*

*/

protected HashMap dataTable;

/**

* 最近增加的CD集合

*

*/

protected ArrayList recentRecordingList;

/**

* 默认构造方法

*/

public DataAccessor() {

dataTable = new HashMap();

recentRecordingList = new ArrayList();

}

它提供的方法可以用来获得CD的分类和

SuperVCD管理系统 完整代码,带各种注释,可以拿这个项目入门。

/** * 获得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;

SuperVCD管理系统 完整代码,带各种注释,可以拿这个项目入门。

* 在内存中增加新的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);

}

}

Handler

SuperVCD管理系统 完整代码,带各种注释,可以拿这个项目入门。

import java.io.*;

import .*;

import java.util.*;

/**

* 这个类是socket连接的处理器

* 例如:

* <pre>

* 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;

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);

SuperVCD管理系统 完整代码,带各种注释,可以拿这个项目入门。

} 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信息到客户端.");

SuperVCD管理系统 完整代码,带各种注释,可以拿这个项目入门。

}

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);

}

}

MusicDataAccessor

import java.util.*;

import java.io.*;

/**

* 音乐CD数据读取的实现类

*

*/

public class MusicDataAccessor extends DataAccessor {

//////////////////////////////////////////////////////

//

// 文件格式如下

// 歌手姓名, 唱片名, 类别, 图片名, 歌曲数目

// 歌曲 #1, 总秒数

// 歌曲 #2, 总秒数

// ----------------------------

//

SuperVCD管理系统 完整代码,带各种注释,可以拿这个项目入门。

/////////////////////////////////////////////////////// /** * 数据文件名 */ protected static final String FILE_NAME = "music.db"; /** * 纪录的分割符 */ protected static final String RECORD_SEPARATOR = "----------"; /** * 默认构造方法 * */ public MusicDataAccessor() { load(); } /** * 读取数据的方法 */ public void load() { dataTable = new HashMap(); ArrayList musicArrayList = null; StringTokenizer st = null; MusicRecording myRecording; String line = ""; String artist, title; String category, imageName; int numberOfTracks; int basePrice; double price; Track[] trackList; try { log("读取文件: " + FILE_NAME + "...");

本文来源:https://www.bwwdw.com/article/wl91.html

Top