日期:2014-05-19 浏览次数:20698 次
//大爱伙可以先把源码copy到机器上试一下,再回贴,这是我上学时写的一直没有公开过(不过还有些bug代码也没重构,后来//没时间放下了 一直没写下去了,不怎么会玩贴多次发送吧),分不多谢谢大家的支持谢谢大家无声的掌声^_^
package game;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.border.Border;
public class QuareGame extends JFrame implements ActionListener {
private GamePanel gamePanel ;
//菜单工具组件
JMenuBar menuBar = new JMenuBar();
//menuBar.setSize(20, 20);
JMenu menuFile = new JMenu("游戏");
JMenuItem newGame = new JMenuItem("新游戏");
JMenuItem pauseGame = new JMenuItem("暂停");
JMenuItem overGame = new JMenuItem("结束");
JMenu menuHelp=new JMenu("帮助");
//
JLabel labLevel=new JLabel("选择关卡:");
JTextField txtLevel=new JTextField();
public QuareGame(GamePanel gp) {
setBounds(500, 100, 400, 480);
setTitle("my small game");
setLayout(new BorderLayout());
JFrame.setDefaultLookAndFeelDecorated(true);
//添加菜单条
setJMenuBar(createMenuBar());
this.gamePanel = gp;
if(gamePanel!=null){
add(gamePanel);
addKeyListener(gamePanel);
}
labLevel.setBounds(260, 140, 50, 30);
txtLevel.setBounds(260, 180, 50, 30);
// add(labLevel);
// add(txtLevel);
setVisible(true);
// setResizable(false);
}
// 菜单条
public JMenuBar createMenuBar() {
menuFile.add(newGame);
menuFile.add(pauseGame);
menuFile.add(overGame);
menuBar.add(menuFile);
menuBar.add(menuHelp);
menuHelp.add(new JMenuItem("版本号"));
//添加事件
newGame.addActionListener(this);
pauseGame.addActionListener(this);
overGame.addActionListener(this);
return menuBar;
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
QuareGame game = new QuareGame(new GamePanel(new int[22][12] ,new int[2][2]));
}
@Override
public void actionPerformed(ActionEvent e) {
//System.out.println(e.getActionCommand());
if(e.getActionCommand(). equals("新游戏")){
System.out.println("newgame");
if(gamePanel!=null){
remove(gamePanel);
removeKeyListener(gamePanel);
}
this.gamePanel = new GamePanel(new int[22][12] ,new int[2][2]);
add(gamePanel);
addKeyListener(gamePanel);
}else if(e.getActionCommand().equals("暂停")){
pauseGame.setText("继续");
gamePanel.timer.stop();
}else if(e.getActionCommand().e