⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 musicpanel.java

📁 《Java核心技术应用开发》电子工业出版社书籍源代码
💻 JAVA
字号:
import 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;
	

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

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

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

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -