📄 mediaplayer.java
字号:
if(controlComponent!=null)
{
upPanel.remove(controlComponent);
}
}
private void doPlaySong(int fileIndex)
{
destroyOldPlayer();
// index=fileList.getSelectedIndex();//从一开始
String path=(String)vecPath.elementAt(fileIndex);
String name=(String)vecName.elementAt(fileIndex);
URL locator=null;
try{
locator=new URL("file:"+path);
}catch(MalformedURLException e)
{
System.out.println(e.getMessage());
}
try{
player = Manager.createPlayer(locator);
} catch (NoPlayerException e) {
//System.out.println(e);
//System.out.println(locator);
int tempIndex=fileList.getSelectedIndex();
if(tempIndex!=-1)
{
String tempName=(String)vecName.elementAt(tempIndex);
tempName=tempName.substring(tempName.lastIndexOf(".")+1);
JOptionPane.showMessageDialog(null, "本播放器不支持"+tempName+"格式的文件,请下载相关插件!", "警告!",
JOptionPane.ERROR_MESSAGE);
}
}
catch(IOException e)
{
//System.out.println(e);
//System.out.println(locator);
JOptionPane.showMessageDialog(null, "目标文件可能被删除或者是移走,请重新指定路径", "警告!",
JOptionPane.ERROR_MESSAGE);
}
fileList.setSelectedIndex(fileIndex);
setTitle(name);
player.addControllerListener(this);
if(player!=null)
player.realize();
}
private void fileListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_fileListMouseClicked
// TODO 将在此处添加您的处理代码:
if(evt.getClickCount()==2)
{
index=fileList.getSelectedIndex();
doPlaySong(index);
}
}//GEN-LAST:event_fileListMouseClicked
private void playItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_playItemActionPerformed
// TODO 将在此处添加您的处理代码:
//---------------------------------
destroyOldPlayer();
index=fileList.getSelectedIndex();
doPlaySong(index);
}//GEN-LAST:event_playItemActionPerformed
private void customLookAndFeelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_customLookAndFeelActionPerformed
// TODO 将在此处添加您的处理代码:
try{
UIManager.setLookAndFeel(new SyntheticaStandardLookAndFeel());
SwingUtilities.updateComponentTreeUI(this);
}catch(Exception e){System.out.println(e.getMessage());}
}//GEN-LAST:event_customLookAndFeelActionPerformed
private void windowsClassicLookAndFeelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_windowsClassicLookAndFeelActionPerformed
// TODO 将在此处添加您的处理代码:
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
}catch(Exception e){System.out.println(e.getMessage());}
}//GEN-LAST:event_windowsClassicLookAndFeelActionPerformed
private void windowsLookAndFeelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_windowsLookAndFeelActionPerformed
// TODO 将在此处添加您的处理代码:
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
}catch(Exception e){System.out.println(e.getMessage());}
}//GEN-LAST:event_windowsLookAndFeelActionPerformed
private void MotifLookAndFeelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MotifLookAndFeelActionPerformed
// TODO 将在此处添加您的处理代码:
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
}catch(Exception e){System.out.println(e.getMessage());}
}//GEN-LAST:event_MotifLookAndFeelActionPerformed
private void metalLookAndFeelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_metalLookAndFeelActionPerformed
// TODO 将在此处添加您的处理代码:
try{
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
}catch(Exception e){System.out.println(e.getMessage());}
}//GEN-LAST:event_metalLookAndFeelActionPerformed
private void btnGroupInit()
{
lookAndfeelBtnGroup=new ButtonGroup();
lookAndfeelBtnGroup.add(metalLookAndFeel);
lookAndfeelBtnGroup.add(MotifLookAndFeel);
lookAndfeelBtnGroup.add(windowsLookAndFeel);
lookAndfeelBtnGroup.add(windowsClassicLookAndFeel);
lookAndfeelBtnGroup.add(customLookAndFeel);
}
private void playerInit()
{
player=null;
}
private void getList(String infoName,Vector temp)
{
listFromfile=new ListFromFile(infoName);
temp=listFromfile.getContent();
if(temp!=null)
{
for(Object x:temp)
{
this.indexOfList++;
MusicInfo tempMusic=(MusicInfo)x;
vecName.add(tempMusic.getName());
vecPath.add(tempMusic.getPath());
}
}
else
JOptionPane.showMessageDialog(null, "初始列表失败!", "警告!", JOptionPane.INFORMATION_MESSAGE);
}
private void onTopCheckItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onTopCheckItemActionPerformed
// TODO 将在此处添加您的处理代码:
if(onTopCheckItem.getState())
this.setAlwaysOnTop(true);
else
this.setAlwaysOnTop(false);
}//GEN-LAST:event_onTopCheckItemActionPerformed
private void aboutItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutItemActionPerformed
// TODO 将在此处添加您的处理代码:
dialog.setVisible(true);
}//GEN-LAST:event_aboutItemActionPerformed
private void dirItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dirItemActionPerformed
// TODO 将在此处添加您的处理代码:
openDir();
}//GEN-LAST:event_dirItemActionPerformed
private void openItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openItemActionPerformed
// TODO 将在此处添加您的处理代码:
openFile();
}//GEN-LAST:event_openItemActionPerformed
private void exitItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitItemActionPerformed
// TODO 将在此处添加您的处理代码:
int exi;
exi=JOptionPane.showConfirmDialog(this,"确认退出吗??","退出程序",JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(exi==JOptionPane.YES_OPTION)
{
tofile.save();
System.exit(0);
}
return;
}//GEN-LAST:event_exitItemActionPerformed
public void controllerUpdate(ControllerEvent e)
{
if(e instanceof RealizeCompleteEvent)
{
Component visualComponent=player.getVisualComponent();
if(visualComponent!=null)
upPanel.add(visualComponent,BorderLayout.CENTER);
else
{
upPanel.remove(centerLabel);
upPanel.add(temp,BorderLayout.CENTER);
}
Component controlComponent=player.getControlPanelComponent();
if(controlComponent!=null)
upPanel.add(controlComponent,BorderLayout.SOUTH);
upPanel.updateUI();
// SwingUtilities.updateComponentTreeUI(this);
// this.pack();
player.prefetch();
}
else if(e instanceof PrefetchCompleteEvent)
{
if(player!=null)
player.start();
}
else if(e instanceof EndOfMediaEvent)
{
//player.setMediaTime(new Time(0));
//player.start();
if(!Loop)
index=(index+1)%vecPath.size();
else
{}
doPlaySong(index);
}
else if(e instanceof ControllerClosedEvent)
{
destroyOldPlayer();
// System.exit(0);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
MediaPlayer app= new MediaPlayer();
app.setSize(300,600);
app.setVisible(true);
}
});
}
// 变量声明 - 不进行修改//GEN-BEGIN:variables
private javax.swing.JRadioButtonMenuItem MotifLookAndFeel;
private javax.swing.JMenuItem aboutItem;
private javax.swing.JLabel centerLabel;
private javax.swing.JRadioButtonMenuItem customLookAndFeel;
private javax.swing.JMenuItem delAllItem;
private javax.swing.JMenuItem delItem;
private javax.swing.JMenuItem dirItem;
private javax.swing.JPanel downPanel;
private javax.swing.JMenu editItem;
private javax.swing.JMenuItem exitItem;
private javax.swing.JList fileList;
private javax.swing.JMenu fileMenu;
private javax.swing.JMenuItem getPathItem;
private javax.swing.JMenuItem helpItem;
private javax.swing.JMenu helpMenu;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JMenuItem loopItem;
private javax.swing.JPopupMenu mPop;
private javax.swing.JRadioButtonMenuItem metalLookAndFeel;
private javax.swing.JCheckBoxMenuItem onTopCheckItem;
private javax.swing.JMenuItem openDirItem;
private javax.swing.JMenuItem openFileItem;
private javax.swing.JMenuItem openItem;
private javax.swing.JMenu operationMenu;
private javax.swing.JMenuItem playItem;
private javax.swing.JMenuItem saveItem;
private javax.swing.JLabel showLabel;
private javax.swing.JMenuItem stopItem;
private javax.swing.JPanel upPanel;
private javax.swing.JRadioButtonMenuItem windowsClassicLookAndFeel;
private javax.swing.JRadioButtonMenuItem windowsLookAndFeel;
// 变量声明结束//GEN-END:variables
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -