?? multimididemo.java
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.sound.midi.*;
import java.io.File;
public class MultiMidiDemo extends JFrame
{
private String[] midiFile={"betsy.mid","camptown.mid"};
private MultiMidiPlayer midiPanel;
private ImagePanel imagePanel; //繪制圖像的面板
public MultiMidiDemo()
{
super("MultiMidiDemo");
this.setBounds(150,150,400,400);
//獲取內(nèi)容窗格
Container container = getContentPane();
//創(chuàng)建用于繪制圖像的面板
imagePanel = new ImagePanel();
container.add(imagePanel, BorderLayout.CENTER);
midiPanel = new MultiMidiPlayer(midiFile);
container.add(midiPanel, BorderLayout.SOUTH);
setVisible(false);
//setDefaultCloseOperation(EXIT_ON_CLOSE);
}
/* public static void main(String[] args)
{
MultiMidiDemo application = new MultiMidiDemo();
}*/
}
class ImagePanel extends JPanel
{
private ImageIcon images;
public ImagePanel()
{
super();
setBackground(Color.WHITE);
//載入圖像
// images= new ImageIcon("boy01.png");
images= new ImageIcon("music.jpg");
}
public void paintComponent(Graphics g)
{
Graphics2D g2d=(Graphics2D)g;
g2d.setColor(getBackground());
g2d.fillRect(0, 0, getWidth(), getHeight());
//繪制圖像,坐標為屏幕中心
int x = (this.getWidth() - images.getIconWidth())/2;
int y = (this.getHeight() - images.getIconHeight())/2;
images.paintIcon(this, g, x, y);
}
}
class MultiMidiPlayer extends JPanel implements Runnable, ActionListener
{
private Thread runner;
private JPanel buttonPanel; //存放按鈕的面板
private JButton play, stop; //播放, 結束按鈕
private JLabel label; //顯示信息
private Sequence currentSound; //音序
private Sequencer player; //音序器
private String[] songFile; //播放文件
private int songToPlay;
public MultiMidiPlayer(String[] songs) {
super();
songFile = songs;
//創(chuàng)建標簽
label = new JLabel("", JLabel.CENTER);
label.setFont(new Font("Serif", Font.PLAIN, 14));
label.setPreferredSize(new Dimension(400, 30));
//創(chuàng)建按鈕
play = new JButton("Play");
stop = new JButton("Stop");
stop.setEnabled(false);
//注冊監(jiān)聽器
play.addActionListener(this);
stop.addActionListener(this);
setLayout(new BorderLayout());
//將標簽加入內(nèi)容窗格
add(label, BorderLayout.NORTH);
buttonPanel = new JPanel();
//將按鈕加入buttonPanel面板
buttonPanel.add(play);
buttonPanel.add(stop);
//將buttonPanel面板加入內(nèi)容窗口
add(buttonPanel, BorderLayout.CENTER);
if (songFile.length == 0)
{
play.setEnabled(false);
}
}
class ImagePanel extends JPanel
{
private ImageIcon images;
public ImagePanel()
{
super();
setBackground(Color.WHITE);
//載入圖像
// images= new ImageIcon("boy01.png");
images= new ImageIcon("d412505582.jpg");
}
public void paintComponent(Graphics g)
{
Graphics2D g2d=(Graphics2D)g;
g2d.setColor(getBackground());
g2d.fillRect(0, 0, getWidth(), getHeight());
//繪制圖像,坐標為屏幕中心
int x = (this.getWidth() - images.getIconWidth())/2;
int y = (this.getHeight() - images.getIconHeight())/2;
images.paintIcon(this, g, x, y);
}
}
//處理按鈕事件
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == play)
play();
else
stop();
}
//播放音樂
public void play()
{
if (runner == null)
{
runner = new Thread(this);
runner.start();
play.setEnabled(false);
stop.setEnabled(true);
}
}
//停止播放
public void stop()
{
if (runner != null)
{
runner = null;
stop.setEnabled(false);
play.setEnabled(true);
}
}
public void run()
{
try
{
player = MidiSystem.getSequencer(); //獲取音序器
}
catch (Exception exception)
{
label.setText(exception.toString());
}
while (Thread.currentThread() == runner)
{
for (songToPlay = 0; songToPlay < songFile.length; songToPlay++)
{
if (songFile[songToPlay] != null)
{
try
{
File song = new File(songFile[songToPlay]);
//獲取音序
currentSound = MidiSystem.getSequence(song);
player.open();
//設置音序器的音序
player.setSequence(currentSound);
//開始播放
player.start();
label.setText("正在播放的音樂文件為: " + song.getName());
while (player.isRunning() && runner != null)
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException exception){}
}
label.setText("");
player.close();
}
catch (Exception exception)
{
label.setText(exception.toString());
break;
}
}
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -