?? musicpanel.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 + -