我有大量的代碼,我不知道如何實現關鍵偵聽器。基本上,我想讓關鍵聽衆檢測像「w」這樣的按鍵,並更改一些字段(寬度和長度),程序將在其中進行交互。我有一大堆進口報表,所以我沒有包含它們。我最初的想法是讓setSizeWidth和setSizeLength調用keylistener方法,這兩個方法由frame.setSize中的ok()方法調用。這應該使按鍵「w」縮小窗口大小。我真的很抱歉很長的代碼,但我很難過。忽略其他方法,因爲我還沒有完成這個遊戲和它的水平呢!我完全失去了如何在我的java程序中使用keylistener
@SuppressWarnings("serial")
public class Game extends JFrame implements KeyListener{
public static Game game;
public static JFrame frame = new JFrame("Bouncy Ball");
public static int x = 0;
public static int y = 0;
public static int xa = 1;
public static int ya = 1;
public static int x1 = 700;
public static int y1 = 500;
public static int x1a = 1;
public static int y1a = 1;
public static int f = 1;
public static boolean gg = true;
public static boolean t = false;
public static int width = 1000;
public static int length = 1000;
private void moveBall() {
if(f==1){
if (x + xa < 0)
xa = 1;
if (x + xa > getWidth() - 30)
xa = -1;
if (y + ya < 0)
ya = 1;
if (y + ya > getHeight() - 30)
ya = -1;
x = x + xa;
y = y + ya;
}
else if(f==2){
if (x + xa < 0)
xa = 1;
if (x + xa > getWidth() - 30)
xa = -1;
if (x + xa > 670&&y+ya<770)
xa = -1;
if (x + xa < 680&&y+ya>770)
xa=1;
if (y + ya < 0)
ya = 1;
if (y + ya > getHeight() - 30)
ya = -1;
x = x + xa;
y = y + ya;
}
if(f==3){
if (x + xa < 0)
xa = 1;
if (x + xa > getWidth() - 30)
xa = -1;
if (y + ya < 0)
ya = 1;
if (y + ya > getHeight() - 30)
ya = -1;
if(gg==true){
if (x1 + x1a < 0)
x1a = 1;
if (x1 + x1a > getWidth() - 30)
x1a = -1;
if (y1+ y1a < 0)
y1a = 1;
if (y1 + y1a > getHeight() - 30)
y1a = -1;
x1+=x1a;
y1+=y1a;
}
x = x + xa;
y = y + ya;
}
}
public static int setSizeWidth(){
????
return width;
}
public static int setSizeLength(){
?????
return length;
}
public void drawLevelOne(Graphics2D g2d) {
g2d.fillOval((int)x, (int)y, 30, 30);
if(t)g2d.setColor(Color.GREEN);
else g2d.setColor(Color.RED);
g2d.fillRect(300,500,30,30);
}
public void drawLevelTwo(Graphics2D g2d) {
g2d.fillOval((int)x, (int)y, 30, 30);
if(t)g2d.setColor(Color.GREEN);
else g2d.setColor(Color.BLUE);
g2d.fillRect(750,300,30,30);
g2d.fillRect(700,0,15,800);
}
public void drawLevelThree(Graphics2D g2d) {
g2d.fillOval((int)x, (int)y, 30, 30);
if(t)g2d.setColor(Color.GREEN);
else g2d.setColor(Color.RED);
g2d.fillRect(x1,y1,30,30);
g2d.fillRect(600,750,30,30);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
if(f==1) drawLevelOne(g2d);
else if(f ==2) drawLevelTwo(g2d);
else if(f==3) drawLevelThree(g2d);
}
public void checkCollision() throws InterruptedException {
boolean a = false;
if(270 < x && x < 330 && 470 < y && y < 530&&f==1) a = true;
else if(720 < x && x < 780 && 270 < y && y < 330&&f==2) a = true;
else if(f==3&&x>x1-30&&x<x1+30&&y>y1-30&&y<y1+30) gg = false;
else if(gg==false&& 570 < x && x < 630 && 720 < y && y < 780) a = true;
if(a) {
t=true;
game.repaint();
Thread.sleep(1300);
frame.setVisible(false);
System.out.println("You win! Play again?");
Scanner ggg = new Scanner (System.in);
String l = ggg.nextLine();
if(l.equals("yes")){
x=0;
y=0;
x1 = 300;
y1 = 500;
gg=true;
t = false;
ok();
frame.dispose();
System.exit(0);
}
else {
frame.dispose();
System.exit(0);
}
System.exit(0);
}
}
public void endGame() throws InterruptedException {
frame.setVisible(false);
System.out.println("You went out of bounds! Retry?");
Scanner ggg = new Scanner (System.in);
String l = ggg.nextLine();
if(l.equals("yes")){
x=0;
y=0;
x1 = 300;
y1 = 500;
gg=true;
t=false;
ok();
frame.dispose();
System.exit(0);
}
else {
frame.dispose();
System.exit(0);
}
}
public void checkOutOfBounds() throws InterruptedException {
if(f == 1 && (frame.getWidth() < 315 || frame.getHeight() < 540)) endGame();
else if(f == 2 && (frame.getWidth() < 765 || frame.getHeight() < 340)) endGame();
else if(f==3 && (frame.getWidth() < 615 || frame.getHeight() < 790)) endGame();
}
public void ok() throws InterruptedException {
game = new Game();
frame.add(game);
frame.setSize(1000, 1000);
addFocusable(true);
frame.addKeyListner(this);
Scanner scan = new Scanner (System.in);
System.out.println("Level one, two, or three?(must be an int)");
f = scan.nextInt();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
while (true) {
game.moveBall();
frame.setSize(setSizeWidth(),setSizeLength());
game.repaint();
Thread.sleep(5);
checkCollision();
checkOutOfBounds();
}
}
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case 119:
length -= 5;
break;
case 115:
width += 5;
break;
case 97:
length -= 5;
break;
case 100:
length += 5;
break;
}
}
@Override
public void keyReleased(KeyEvent e){
}
@Override
public void keyTyped(KeyEvent e){
}
}
使用鍵綁定而不是'KeyListener'。請參閱[如何使用鍵綁定](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html)以獲取更多詳細信息 – MadProgrammer
*「我最初的想法是讓setSizeWidth和setSizeLength調用keylistener方法「*爲什麼?它通常會以另一種方式完成... – MadProgrammer