📄 testframe.java
字号:
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.File;
public class TestFrame extends JFrame {
protected TestBase pBase = null;
protected String searchDir = null;
protected int curPlayListLength = 0;
static final String MUSIC_DIR_PROPERTY = "musicdir";
static final String DEFAULT_DIR = "music";
static final String CONFIG_FILE_NAME = "play.ini";
private JPanel refContentPane;
private JMenuBar playerMenu = new JMenuBar();
private JMenu menuFile = new JMenu();
private JMenuItem menuFileExit = new JMenuItem();
private JMenu menuHelp = new JMenu();
private JMenuItem menuHelpAbout = new JMenuItem();
private JPanel listPanel = new JPanel();
private JPanel buttonPanel = new JPanel();
private JButton prevBttn = new JButton();
JButton playBttn = new JButton();
JButton pauseBttn = new JButton();
JButton stopBttn = new JButton();
JList filenamesList = new JList();
JButton nextBttn = new JButton();
public TestFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
guiInit();
logicInit();
pBase = new TestBase();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void guiInit() throws Exception {
refContentPane = (JPanel) this.getContentPane();
refContentPane.setLayout(new BorderLayout());
this.setSize(new Dimension(400, 300));
this.setTitle("Java音乐播放器");
menuFile.setText("文件");
menuFileExit.setText("退出");
menuFileExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
menuFileExitClick(e);
}
});
menuHelp.setText("帮助");
menuHelpAbout.setText("关于");
menuHelpAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
menuHelpAboutClick(e);
}
});
buttonPanel.setPreferredSize(new Dimension(30, 100));
buttonPanel.setLayout(new GridLayout());
addBttnIconText(prevBttn, "后退", "StepBack24.gif");
prevBttn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
prevClick(e);
}
});
addBttnIconText(playBttn, "播放", "Play24.gif");
playBttn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
playClick(e);
}
});
addBttnIconText(pauseBttn, "暂停", "Pause24.gif");
pauseBttn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
pauseClick(e);
}
});
addBttnIconText(stopBttn, "停止", "Stop24.gif");
stopBttn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
stopClick(e);
}
});
addBttnIconText(nextBttn, "前进", "StepForward24.gif");
nextBttn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
nextClick(e);
}
});
filenamesList.setToolTipText("可播放音乐文件的清单");
listPanel.setLayout(new BorderLayout());
menuFile.add(menuFileExit);
menuHelp.add(menuHelpAbout);
playerMenu.add(menuFile);
playerMenu.add(menuHelp);
this.setJMenuBar(playerMenu);
refContentPane.add(listPanel, BorderLayout.CENTER);
listPanel.add(filenamesList, BorderLayout.CENTER);
refContentPane.add(buttonPanel, BorderLayout.SOUTH);
buttonPanel.add(prevBttn, null);
buttonPanel.add(playBttn, null);
buttonPanel.add(pauseBttn, null);
buttonPanel.add(stopBttn, null);
buttonPanel.add(nextBttn, null);
}
private void logicInit() {
Properties myProp = null;
FileInputStream inFileStream = null ;
searchDir = DEFAULT_DIR;
try {
inFileStream = new FileInputStream(new File(CONFIG_FILE_NAME));
myProp = new Properties();
myProp.load(inFileStream);
} catch (Exception ex)
{ System.out.println("不能访问配置文件"); }
if (myProp != null) {
searchDir = myProp.getProperty(MUSIC_DIR_PROPERTY,DEFAULT_DIR);
}
File tpDir = new File(searchDir);
if (tpDir.isDirectory()) {
String [] tpFiles = tpDir.list(new java.io.FilenameFilter() {
public boolean accept(File indir, String inName) {
String lcName = inName.toLowerCase();
if (lcName.endsWith("ogg") || lcName.endsWith("mp3")
|| lcName.endsWith("wav"))
return true;
else
return false;
}
});
DefaultListModel dm = new DefaultListModel();
curPlayListLength = tpFiles.length;
for (int i=0; i<curPlayListLength; i++)
dm.addElement(tpFiles[i]);
filenamesList.setModel(dm);
}
}
public void menuFileExitClick(ActionEvent e) {
pBase.endThread();
System.exit(0);
}
public void menuHelpAboutClick(ActionEvent e) {
AboutBox dlg = new AboutBox(this);
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.show();
}
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
menuFileExitClick(null);
}
}
void playClick(ActionEvent e) {
String fileToPlay = (String) filenamesList.getSelectedValue();
if (fileToPlay != null) {
pBase.playFile(searchDir + System.getProperty("file.separator") + fileToPlay);
}
}
void stopClick(ActionEvent e) {
pBase.stop();
}
void pauseClick(ActionEvent e) {
pBase.pause();
}
void prevClick(ActionEvent e) {
pBase.stop();
filenamesList.setSelectedIndex( filenamesList.getSelectedIndex() - 1);
playClick(e);
}
void nextClick(ActionEvent e) {
pBase.stop();
filenamesList.setSelectedIndex( (filenamesList.getSelectedIndex() + 1) % curPlayListLength);
playClick(e);
}
private void addBttnIconText(JButton inBut, String inTxt, String inIcon) {
ImageIcon tpIcon = new ImageIcon(test.TestFrame.class.getResource(inIcon));
inBut.setIcon(tpIcon);
inBut.setHorizontalTextPosition(SwingConstants.CENTER);
inBut.setVerticalTextPosition(SwingConstants.BOTTOM);
inBut.setText(inTxt);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -