import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.sound.midi.*;
import java.io.File;
class Meplay implements Runnable
{
private Thread runner;
private Sequence currentSound;
private Sequencer player;
private String[] songFile;
private int songToPlay;
public Meplay(String[] songs)
{
super();
songFile=songs;
}
public void play()
{
if(runner==null)
{ runner=new Thread(this);
runner.start();
}
}
public void stop()
{
if(runner!=null)
{ runner=null;}
}
public void run()
{
try{
player=MidiSystem.getSequencer();
}
catch(Exception e)
{ }
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();
while(player.isRunning()&&runner!=null)
{
try{
Thread.sleep(1000);
}
catch(InterruptedException e){}
}
player.close();
}
catch(Exception e)
{ break;
}
}
}
}
}
}
class SelectWindow extends Frame implements ActionListener
{
Meplay meplay;
private String[] midfile={"xlj.mid","xulijun.mid"};
Kaishi kaishi;
PersonSelected personSelected[];
InputPerson input;
SelectedPane select;
ResultArea show;
Button buttonInput,buttonSelect,buttonResult,reNew;
MenuBar menuBar;
Menu menu,meun1;
MenuItem item1,item2,item3,item4;
int max=5;
CardLayout card;
Panel center=new Panel(),
south=new Panel();
FileDialog filedialogsave;
SelectWindow()
{
meplay=new Meplay(midfile);
personSelected=new PersonSelected[1];
for(int k=0;k<personSelected.length;k++)
{ personSelected[k]=new PersonSelected("无名",0);
}
card=new CardLayout();
center.setLayout(card);
kaishi=new Kaishi();
input=new InputPerson(personSelected);
select=new SelectedPane(personSelected,max);
show=new ResultArea(personSelected);
menuBar=new MenuBar();
menu=new Menu("程序介绍");
meun1=new Menu("歌曲播放");
item3=new MenuItem("播放");
item4=new MenuItem("停止");
item1=new MenuItem("打开");
item2=new MenuItem("保存");
menuBar.add(menu);
menuBar.add(meun1);
menu.add(item1);
item1.addActionListener(this);
meun1.add(item3);
item3.addActionListener(this);
meun1.add(item4);
item4.addActionListener(this);
menu.add(item2);
setMenuBar(menuBar);
center.add("kaishi",kaishi);
center.add("input",input);
center.add("select",select);
center.add("show",show);
buttonInput=new Button("输入候选人界面");
buttonInput.addActionListener(this);
buttonSelect=new Button("统计选票界面,最多选"+max+"人");
buttonSelect.addActionListener(this);
buttonResult=new Button("查看得票界面");
buttonResult.addActionListener(this);
reNew=new Button("重新选举");
reNew.addActionListener(this);
south.add(buttonInput);
south.add(buttonResult);
south.add(buttonSelect);
south.add(reNew);
add(center,BorderLayout.CENTER);
add(south,BorderLayout.SOUTH);
filedialogsave=new FileDialog(this,"保存文件对话框",FileDialog.LOAD);
setSize(800,700);
setVisible(true);
setBackground(Color.pink);
validate();
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==item1) { filedialogsave.setVisible(true);}
if(e.getSource()==item3) {meplay.play();}
if(e.getSource()==item4) {meplay.stop();}
Button b=(Button)e.getSource();
setTitle(b.getLabel());
if(b==buttonInput)
{ card.show(center,"input");
buttonInput.setEnabled(false);
}
if(b==buttonSelect)
{ personSelected=input.getPersonSelected();
select.setPersonSelected(personSelected);
select.init();
card.show(center,"select");
}
if(b==buttonResult)
{ personSelected=select.getPersonSelected();
show.setPersonSelected(personSelected);
card.show(center,"show");
}
if(b==reNew)
{ personSelected=new PersonSelected[1];
for(int k=0;k<personSelected.length;k++)
{ personSelected[k]=new PersonSelected("无名",0);
}
center.removeAll();
input=new InputPerson(personSelected);
select=new SelectedPane(personSelected,3);
show=new ResultArea(personSelected);
center.add("kaishi",kaishi);
center.add("input",input);
center.add("select",select);
center.add("show",show);
card.show(center,"kaishi");
buttonInput.setEnabled(true);
center.validate();
}
}
}
public class Example7_40
{ public static void main(String args[])
{ String output;
SelectWindow win=new SelectWindow();
win.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) { System.exit(0); }
});
output="谢谢光顾\n本人水平欠缺欢迎多多指教\n";
JOptionPane.showMessageDialog(null,output);
}
}