日期:2014-05-20 浏览次数:20870 次
package gesture_now;
//This is the main window of the program
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.leapmotion.leap.Controller;
import com.leapmotion.leap.Listener;
import java.io.*;
public class MainFrame implements Strings{
JFrame mainFrame; //主窗口
int scoreNumber = 0;
int timeNumber = 0;
int roundNumber = 0;
MenuBar menuBar; //菜单栏
Menu menu1,menu2; //菜单栏两个按钮
MenuItem menu1_Start,menu1_Score,menu1_Exit; //第一个按钮下的三个按钮
MenuItem menu2_Help,menu2_About; //第二个按钮下的两个按钮
JPanel game;
JLabel gesturePic1,gesturePic2,gesturePic3,gesturePic4,gesturePic5;
JPanel score;
JLabel scoreTitle,labelScoreNumber;
JPanel bottom;
JLabel bottomTimeTitle,bottomTimeNumber,bottomRoundTitle,bottomRoundNumber;
Icon icon = new ImageIcon("DownGray.png");
ScoreBoardFrame sbf;
ScoreBoardMenu sbm;
ControllerListener controllerListener;
Controller controller;
public MainFrame(){
controllerListener = new ControllerListener();
controller = new Controller();
controller.addListener(controllerListener);
sbf = new ScoreBoardFrame();
sbm = new ScoreBoardMenu();
mainFrame = new JFrame(GAME_TITLE); //实例化窗体对象
mainFrame.setLayout(new BorderLayout(5,5));
//创建菜单栏
menuBar = new MenuBar();
menu1 = new Menu(MENU_FILE);
menu2 = new Menu(MENU_HELP);
menu1_Start = new MenuItem(MENU_ITEM_START);
menu1_Score = new MenuItem(MENU_ITEM_SCORE);
menu1_Exit = new MenuItem(MENU_ITEM_EXIT);
menu2_Help = new MenuItem(MENU_ITEM_HELP);
menu2_About = new MenuItem(MENU_ITEM_ABOUT);
menu1.add(menu1_Start);
menu1.add(menu1_Score);
menu1.addSeparator();
menu1.add(menu1_Exit);
menu2.add(menu2_About);
menu2.addSeparator();
menu2.add(menu2_Help);
menuBar.add(menu1);
menuBar.add(menu2);
mainFrame.setMenuBar(menuBar);
//添加菜单栏监听器
menu1_Start.addActionListener(new ActionListener(){ //Start监听器
public void actionPerformed(ActionEvent e){
if(!controller.isConnected()){ //如果没有连接,则弹出提示消息
JOptionPane.showMessageDialog( null , DISCONNECT_INFORMATION ,DISCONNECT_TITLE , JOptionPane.ERROR_MESSAGE);
}else{
start();//开始游戏
}
}
});
menu1_Score.addActionListener(new ActionListener(){ //Score监听器
public void actionPerformed(ActionEvent e){
sbm.setVisible(true);
}
});
menu1_Exit.addActionListener(new ActionListener(){ //Exit监听器
public void actionPerformed(ActionEvent e){
System.exit(1);
}
});
menu2_Help.addActionListener(new ActionListener(){ //Help监听器
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog( null , HELP_INFORMATION ,MENU_ITEM_HELP , JOptionPane.INFORMATION_MESSAGE);
}
});
menu2_About.addActionListener(new ActionListener(){ //About监听器
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog( null , ABOUT_INFORMATION ,MENU_ITEM_ABOUT , JOptionPane.INFORMATION_MESSAGE);
}
});
mainFrame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent arg0){
System.exit(1);
}
});
//设置分数面板
score = new JPanel(new BorderLayout(0,3));
scoreTitle = new JLabel(SCORE_TITLE,JLabel.CENTER);
Font scoreTitleFont = new Font("Arial Black",Font.PLAIN,40);
scoreTitle.setFont(scoreTitleFont);
labelScoreNumber = new JLabel(Integer.toString(scoreNumber),JLabel.CENTER);
Font scoreNumberFont = new Font("Arial Black",Font.PLAIN,60);
labelScoreNumber.setFont(scoreNumberFont);
score.add(scoreTitle, BorderLayout.NORTH);
score.add(labelScoreNumber,BorderLayout.SOUTH);
score.setBackground(Color.WHITE);
mainFrame.add(score,BorderLayout.NORTH);
//设置游戏面板
game = new JPanel(new GridLayout(1,5,3,3));
gesturePic1 = new JLabel("",icon,JLabel.CENTER);
gesturePic2 = new JLabel("",icon,JLabel.CENTER);
gesturePic3 = new JLabel("",icon,JLabel.CENTER);
gesturePic4 = new JLabel("",icon,JLabel.CENTER);
gesturePic5 = new JLabel("",icon,JLabel.CENTER);
game.add(gesturePic1);
game.add(gesturePic2);