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

📄 musicdetailsdialog.java

📁 《Java核心技术应用开发》电子工业出版社书籍源代码
💻 JAVA
字号:
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;
	
	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;
		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("  图片不存在  ");
			}
			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);
	}
	
	/**
	*  处理按钮的内部类
	*/
	class OkButtonActionListener implements ActionListener {
		
		public void actionPerformed(ActionEvent event)
		{
			setVisible(false);
		}
	}
}

⌨️ 快捷键说明

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