package gameFrame;
import gameServices.Services;
import gameTool.Prices;
import gameTool.Prices1;
import gameTool.Squares;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GameFrame extends JFrame{
public static final int GAMEPANEL_WIDTH = 400;
public static final int GAMEPANEL_HEIGHT = 700;
public static final int TOOLPANEL_WIDTH = 200;
public static final int TOOLPANEL_HEIGHT = 700;
private JPanel nextSquares ;
private JLabel score;
private JButton gameStart;
private JButton gameStop;
private JButton gameResume;
//现在下落的大方块
Prices currentPrices = null;
//游戏窗口
GamePanel gp;
//工具窗口
ToolPanel tp;
public GameFrame(){
gp = new GamePanel();
tp = new ToolPanel();
JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT ,gp,tp);
jsp.setDividerLocation(GAMEPANEL_WIDTH);
this.add(jsp);
System.out.println(this.getKeyListeners().length); //打印0
this.addKeyListener(new KeyBoardListener());
System.out.println(this.getKeyListeners().length); //打印1
currentPrices = new Prices1();
Timer timer = new Timer(1000,new PricesFallDowmListener());
timer.start();
}
/*
* 监听器:游戏自动控制大方块下落
*/
class PricesFallDowmListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
Services.fallDowm(currentPrices);
gp.repaint();
}
}
/*
* 键盘监听器
*/
class KeyBoardListener extends KeyAdapter{
public void keyPressed(KeyEvent e){
System.out.println("dfs");
if(e.getKeyCode() == KeyEvent.VK_K){
System.out.println("ldsfk");
}
}
public void keyReleased(KeyEvent e){
System.out.println("dfs");
}
public void keyTyped(KeyEvent e) {
System.out.println("dfs");
}
}
/*
* 游戏主窗口
*/
class GamePanel extends JPanel{
public GamePanel(){
this.setPreferredSize(new Dimension(GAMEPANEL_WIDTH,GAMEPANEL_HEIGHT));
this.setBackground(Color.PINK);
}
public void paint(Graphics g){
super.paint(g);
if(currentPrices != null){
java.util.List<Squares> list = currentPrices.getCurrentSquares();
if(list == null)
System.out.println("sdlf");
for(int i = 0 ; i < list.size() ; i++){
Squares squares = list.get(i);
// System.out.println(squares.getCurrentX() +":"+squares.getCurrentY());
g.drawImage(squares.getImage(), squares.getCurrentX(), squares.getCurrentY(),
Squares.SQUARES_WIDTH, Squares.SQUARES_HIGHT, null);
}
}
}
}
/*
* 工具窗口
* 用来显示 开始游戏,暂停游戏,继续游戏,下一个方块,分数
*/
class ToolPanel extends JPanel{
public ToolPanel(){
nextSquares = new JPanel();
nextSquares.setPreferredSize(new Dimension(200,200));
详细解决方案
为什么给增加了键盘监听器后,按上键盘都没反应
热度:170 发布时间:2016-04-21 23:46:11.0
相关解决方案