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));
score = new JLabel("分数");
gameStart = new JButton("游戏开始");
gameStop = new JButton("游戏暂停");
gameResume = new JButton("游戏继续");
this.setLayout(new GridLayout(5,1,100,100));
this.add(nextSquares);
this.add(score);
this.add(gameStart);
this.add(gameStop);
this.add(gameResume);
this.setPreferredSize(new Dimension(TOOLPANEL_WIDTH,TOOLPANEL_HEIGHT));
this.setBackground(Color.GRAY);
}
}
}为什么给增加了键盘监听器后,按下键盘都没反应
测试实现鼠标监听器也没反应,但是给按钮增加监听器就没问题为啥呢?
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));
score = new JLabel("分数");
gameStart = new JButton("游戏开始");
gameStop = new JButton("游戏暂停");
gameResume = new JButton("游戏继续");
this.setLayout(new GridLayout(5,1,100,100));
this.add(nextSquares);
this.add(score);
this.add(gameStart);
this.add(gameStop);
this.add(gameResume);
this.setPreferredSize(new Dimension(TOOLPANEL_WIDTH,TOOLPANEL_HEIGHT));
this.setBackground(Color.GRAY);
}
}
}为什么给增加了键盘监听器后,按下键盘都没反应
测试实现鼠标监听器也没反应,但是给按钮增加监听器就没问题为啥呢?
jsp.addKeyListener(new KeyBoardListener());
刚刚试了下将Button注释掉,就可以了,确定是焦点的问题,不知道要怎么设置焦点!。
给我瞎碰中了,用setFocusable()把!