Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么可以';在run()-methode中if语句为true时,我是否再使用keyPressed?_Java_If Statement_Graphics_Keylistener_Keyevent - Fatal编程技术网

Java 为什么可以';在run()-methode中if语句为true时,我是否再使用keyPressed?

Java 为什么可以';在run()-methode中if语句为true时,我是否再使用keyPressed?,java,if-statement,graphics,keylistener,keyevent,Java,If Statement,Graphics,Keylistener,Keyevent,我试图制作一种砖块破解游戏,但一旦我按下开始按钮(“starten”=true)。我不能再使用keyPressed()-工具,但在按下开始按钮之前,我可以使用keyPressed()-工具。有人能告诉我为什么我突然不能再使用keyPressed()-实用程序,并给我一个可能的解决方案吗 import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class VGKer

我试图制作一种砖块破解游戏,但一旦我按下开始按钮(“starten”=true)。我不能再使用keyPressed()-工具,但在按下开始按钮之前,我可以使用keyPressed()-工具。有人能告诉我为什么我突然不能再使用keyPressed()-实用程序,并给我一个可能的解决方案吗

import java.util.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class VGKernel extends JPanel implements KeyListener {

public Rectangle screen, ball, block; // The screen area and ball location/size.
public Rectangle bounds;  // The boundaries of the drawing area.
public JFrame frame; // A JFrame to put the graphics into.
public VGTimerTask vgTask; // The TimerTask that runs the game.
public boolean down, right, starten = false; // Direction of ball's travel.
public JButton start;

public VGKernel(){
    super();
    screen = new Rectangle(0, 0, 600, 400);
    ball   = new Rectangle(0, 0, 20, 20);
    block = new Rectangle(260, 350, 40, 10);
    bounds = new Rectangle(0, 0, 600, 400); // Give some starter values.
    frame = new JFrame("VGKernel");
    vgTask = new VGTimerTask();
    addKeyListener(this);
    setFocusable(true);
    start = new JButton("Start");
    start.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            starten = true;
        }
    });
    add(start);
}

class VGTimerTask extends TimerTask{
    public void run(){
        repaint();

        if(starten){
        moveBall();
        frame.repaint();
        }
    }
  }

// Now the instance methods:
  public void paintComponent(Graphics g){
    // Get the drawing area bounds for game logic.
    bounds = g.getClipBounds();
    // Clear the drawing area, then draw the ball.
    g.clearRect(screen.x, screen.y, screen.width, screen.height);
    g.fillRect(ball.x, ball.y, ball.width, ball.height);
    g.fillRect(block.x, block.y, block.width, block.height);
  }

  public void moveBall(){
  // Ball should really be its own class with this as a method.
    if (right) ball.x+=1; // If right is true, move ball right,
    else ball.x-=1;       // otherwise move left.
    if (down)  ball.y+=1; // Same for up/down.
    else ball.y-=1;
    if (ball.x > (bounds.width - ball.width)) // Detect edges and bounce.
      { right = false; ball.x = bounds.width -  ball.width; }
    if (ball.y > (bounds.height - ball.height))
      { down  = false; ball.y = bounds.height - ball.height;}
    if (ball.x == 0) { right = true; ball.x = 0; }
    if (ball.y == 0) { down  = true; ball.y = 0; }
  }

  public void keyPressed(KeyEvent evt) {
      if(evt.getKeyCode() == KeyEvent.VK_G && block.x > 0) {
          block.x -= 20;
      }

      if(evt.getKeyCode() == KeyEvent.VK_H && block.x < 540) {
          block.x += 20;
      }
  }

  public void keyTyped(KeyEvent evt){  }
  public void keyReleased(KeyEvent evt){ }

  public void startActionPerformed(java.awt.event.ActionEvent evt) {
      starten = true;
  }

  public static void main(String arg[]){
    java.util.Timer vgTimer = new java.util.Timer();  // Create a Timer.
    VGKernel panel = new VGKernel(); // Create and instance of our kernel.

    // Set the intial ball movement direction.
    panel.down = true;
    panel.right = true;

    // Set up our JFRame
    panel.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    panel.frame.setSize(panel.screen.width, panel.screen.height);
    panel.frame.setContentPane(panel); 
    panel.frame.setVisible(true);

    // Set up a timer to do the vgTask regularly.
    vgTimer.schedule(panel.vgTask, 0, 10);
  }
import java.util.*;
导入java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类VGKernel扩展了JPanel实现了KeyListener{
公共矩形屏幕、球、块;//屏幕区域和球的位置/大小。
公共矩形边界;//绘图区域的边界。
public JFrame;//要将图形放入的JFrame。
public VGTimerTask vgTask;//运行游戏的TimerTask。
public boolean down,right,starten=false;//球的移动方向。
公共按钮启动;
公共VGKernel(){
超级();
屏幕=新矩形(0,0,600,400);
ball=新矩形(0,0,20,20);
块=新矩形(260、350、40、10);
bounds=新矩形(0,0,600,400);//给出一些起始值。
框架=新的JFrame(“VGKernel”);
vgTask=新的VGTimerTask();
addKeyListener(此);
设置聚焦(真);
开始=新的JButton(“开始”);
start.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
starten=正确;
}
});
添加(开始);
}
类VGTimerTask扩展了TimerTask{
公开募捐{
重新油漆();
如果(开始){
移动球();
frame.repaint();
}
}
}
//现在,实例方法:
公共组件(图形g){
//获取游戏逻辑的绘图区域边界。
bounds=g.getClipBounds();
//清除绘图区域,然后绘制球。
g、 clearRect(screen.x、screen.y、screen.width、screen.height);
g、 fillRect(ball.x,ball.y,ball.width,ball.height);
g、 fillRect(block.x,block.y,block.width,block.height);
}
公共空间移动球(){
//Ball应该是它自己的类,并以此作为方法。
如果(右)ball.x+=1;//如果right为true,则向右移动ball,
else ball.x-=1;//否则向左移动。
如果(向下)ball.y+=1;//向上/向下也是如此。
else-ball.y-=1;
if(ball.x>(bounds.width-ball.width))//检测边缘和反弹。
{right=false;ball.x=bounds.width-ball.width;}
if(ball.y>(bounds.height-ball.height))
{down=false;ball.y=bounds.height-ball.height;}
如果(ball.x==0){right=true;ball.x=0;}
如果(ball.y==0){down=true;ball.y=0;}
}
按下公共无效键(KeyEvent evt){
if(evt.getKeyCode()==KeyEvent.VK\u G&&block.x>0){
块x-=20;
}
if(evt.getKeyCode()==KeyEvent.VK_H&&block.x<540){
块x+=20;
}
}
public void keyTyped(KeyEvent evt){}
public void keyReleased(KeyEvent evt){}
public void startActionPerformed(java.awt.event.ActionEvent evt){
starten=正确;
}
公共静态void main(字符串arg[]){
java.util.Timer vgTimer=new java.util.Timer();//创建一个计时器。
VGKernel panel=new VGKernel();//创建内核的实例。
//设置初始球运动方向。
panel.down=true;
panel.right=true;
//建立我们的JFRame
panel.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
面板.框架.设置尺寸(面板.屏幕.宽度,面板.屏幕.高度);
panel.frame.setContentPane(面板);
面板.框架.设置可见(真);
//设置计时器以定期执行vgTask。
vgTimer.时间表(panel.vgTask,0,10);
}

}

您需要确保正在收听KeyEvents的任何组件都具有焦点

单击该按钮后,该按钮将具有焦点,而不是JPanel。更多信息请点击此处:

您可以在JPanel上使用requestFocusInWindow()方法,也可以在JButton上调用setFocusable(false)


或者可以使用键绑定:

Add
VGKernel.this.grabFocus()
ActionListener
以在单击按钮后将焦点返回到
VGKernel
面板

start.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        starten = true;
        VGKernel.this.grabFocus();
    }
});

“此方法供focus实现使用。客户端代码不应使用此方法;相反,它应使用requestFocusInWindow()。”-从中,我将为键绑定提供+1。无法保证调用
requestFocusInWindow
将导致组件获得焦点