java - 失去焦点
我有一个小程序,您可以使用 WASD 键以 4 种方式中的任何一种将播放器设为 32 像素。但是当我添加文本区域或标签时,我无法再移动图像。与专注力有关。
代码:
/**
Tile Generator
Programmer: Dan J.
Thanks to: g00se, pbl, Manny.
Started May 23, 2010
**/
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.io.*;
import java.util.*;
public class tileGen extends Applet implements KeyListener {
Image[] tiles; // tile arrays
Image player; // player image
int x, y, px, py, tx, ty; // x tile - y tile // player x - player y // tile x - tile y
boolean left, right, down, up, canMove, respawn; // is true?
int[][] board; // row tiles for ultimate mapping experience!
final int NUM_TILES = 33; // how many tiles are we implementing?
Label lx, ly; // to see where we are!
int lastX, lastY, row, col;
Label lbl1;
public void init() {
board = loadBoard();
tiles = new Image[NUM_TILES];
for(int i = 0;i < NUM_TILES;i++) {
tiles[i] = getImage(getClass().getResource(String.format("tiles/t%d.png", i)));
}
player = getImage(getClass().getResource("player.png")); // our player
addKeyListener(this);
canMove = true;
int spawnX = 4;
int spawnY = 4;
px =0;
py =0;
lastX = 0;
lastY= 0;
lbl1 = new Label("Label 2");
add(lbl1);
this.requestFocusInWindow();
}
private static final HashSet<Integer> BLOCKED_TILES = new HashSet<Integer>();
static {
BLOCKED_TILES.add(24);
BLOCKED_TILES.add(0);
BLOCKED_TILES.add(6);
BLOCKED_TILES.add(25);
BLOCKED_TILES.add(3);
//add more tiles here
}
public void keyPressed(KeyEvent e) {
if (isInBound(lastX,lastY) == true) {
System.out.println("\nYOU WENT OFF THE GRID.\n");
}
int r1 = lastX + 1;
if (lastX > 0) {
r1 = lastX + 1;
}else{
r1 = 0;
}
int r2 = lastY;
int u1 = lastX;
int u2;
if (lastY > 0) {
u2 = lastY - 1;
}else{
u2 = 0;
}
int l1;
if (lastX > 0) {
l1 = lastX - 1;
}else{
l1 = 0;
}
int l2 = lastY;
int d1 = lastX;
int d2;
if (lastY > 0) {
d2 = lastY + 1;
}else{
d2 = 0;
}
right = true;
left = true;
up = true;
down = true;
if (blocked(r1,r2) == true) right = false; // we cannot go right
if (blocked(u1,u2) == true) up = false; // we cannot go up
if (blocked(l1,l2) == true) left = false; // we cannot go left
if (blocked(d1,d2) == true) down = false; // we cannot go down
if (left == true) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
left = true;
px = px - 32;
lastX = lastX - 1;
}
}
if (right == true) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
right = true;
px = px + 32;
lastX = lastX + 1;
}
}
if (down == true) {
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
down = true;
py = py + 32;
lastY = lastY + 1;
}
}
if (up == true) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
up = true;
py = py - 32;
lastY = lastY - 1;
}
}
repaint();
}
public void keyReleased(KeyEvent e){
} // ignore
public void keyTyped(KeyEvent e){} // ignore
public void paint(Graphics g) {
for (row = 0; row < board.length; row++) {
for (col = 0; col < board[row].length; col++) {
int index = board[row][col];
g.drawImage(tiles[index], 32 * col, 32 * row, this);
}
}
if (respawn == false) {
g.drawImage(player, px, py, this);
}
if (respawn == true) {
g.drawImage(player, 0,0, this);
System.out.println("Respawned!");
respawn = false;
}
} // end paint method
public void update(Graphics g)
{
paint(g);
}
public int[][] loadBoard() {
int[][] board = {
{ 2,2,24,24,24,24,24,1,3,0,0,0 },
{ 2,2,24,23,23,23,24,1,3,0,0,0 },
{ 1,1,24,23,23,23,24,1,3,3,3,1 },
{ 1,1,24,24,23,24,24,1,1,1,1,1 },
{ 1,1,1,1,7,1,1,1,1,1,1,1 },
{ 5,1,1,1,7,7,7,7,7,1,1,1 },
{ 6,1,3,1,1,1,3,1,7,7,7,1 },
{ 6,1,3,1,3,1,1,1,1,1,7,3 }
};
return board;
}
public boolean blocked(int tx, int ty) {
return BLOCKED_TILES.contains(board[ty][tx]);
}
public boolean isInBound(int r, int c) {
return (r >= 0) && (r < 8) && (c >= 12) && (c < 1);
}
} // end whole thing
I have an applet which you can make a player 32 pixels in any of 4 ways using WASD keys. But when I add a textarea or label, I cannot move my image anymore. Something to do with focus.
Code:
/**
Tile Generator
Programmer: Dan J.
Thanks to: g00se, pbl, Manny.
Started May 23, 2010
**/
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.io.*;
import java.util.*;
public class tileGen extends Applet implements KeyListener {
Image[] tiles; // tile arrays
Image player; // player image
int x, y, px, py, tx, ty; // x tile - y tile // player x - player y // tile x - tile y
boolean left, right, down, up, canMove, respawn; // is true?
int[][] board; // row tiles for ultimate mapping experience!
final int NUM_TILES = 33; // how many tiles are we implementing?
Label lx, ly; // to see where we are!
int lastX, lastY, row, col;
Label lbl1;
public void init() {
board = loadBoard();
tiles = new Image[NUM_TILES];
for(int i = 0;i < NUM_TILES;i++) {
tiles[i] = getImage(getClass().getResource(String.format("tiles/t%d.png", i)));
}
player = getImage(getClass().getResource("player.png")); // our player
addKeyListener(this);
canMove = true;
int spawnX = 4;
int spawnY = 4;
px =0;
py =0;
lastX = 0;
lastY= 0;
lbl1 = new Label("Label 2");
add(lbl1);
this.requestFocusInWindow();
}
private static final HashSet<Integer> BLOCKED_TILES = new HashSet<Integer>();
static {
BLOCKED_TILES.add(24);
BLOCKED_TILES.add(0);
BLOCKED_TILES.add(6);
BLOCKED_TILES.add(25);
BLOCKED_TILES.add(3);
//add more tiles here
}
public void keyPressed(KeyEvent e) {
if (isInBound(lastX,lastY) == true) {
System.out.println("\nYOU WENT OFF THE GRID.\n");
}
int r1 = lastX + 1;
if (lastX > 0) {
r1 = lastX + 1;
}else{
r1 = 0;
}
int r2 = lastY;
int u1 = lastX;
int u2;
if (lastY > 0) {
u2 = lastY - 1;
}else{
u2 = 0;
}
int l1;
if (lastX > 0) {
l1 = lastX - 1;
}else{
l1 = 0;
}
int l2 = lastY;
int d1 = lastX;
int d2;
if (lastY > 0) {
d2 = lastY + 1;
}else{
d2 = 0;
}
right = true;
left = true;
up = true;
down = true;
if (blocked(r1,r2) == true) right = false; // we cannot go right
if (blocked(u1,u2) == true) up = false; // we cannot go up
if (blocked(l1,l2) == true) left = false; // we cannot go left
if (blocked(d1,d2) == true) down = false; // we cannot go down
if (left == true) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
left = true;
px = px - 32;
lastX = lastX - 1;
}
}
if (right == true) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
right = true;
px = px + 32;
lastX = lastX + 1;
}
}
if (down == true) {
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
down = true;
py = py + 32;
lastY = lastY + 1;
}
}
if (up == true) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
up = true;
py = py - 32;
lastY = lastY - 1;
}
}
repaint();
}
public void keyReleased(KeyEvent e){
} // ignore
public void keyTyped(KeyEvent e){} // ignore
public void paint(Graphics g) {
for (row = 0; row < board.length; row++) {
for (col = 0; col < board[row].length; col++) {
int index = board[row][col];
g.drawImage(tiles[index], 32 * col, 32 * row, this);
}
}
if (respawn == false) {
g.drawImage(player, px, py, this);
}
if (respawn == true) {
g.drawImage(player, 0,0, this);
System.out.println("Respawned!");
respawn = false;
}
} // end paint method
public void update(Graphics g)
{
paint(g);
}
public int[][] loadBoard() {
int[][] board = {
{ 2,2,24,24,24,24,24,1,3,0,0,0 },
{ 2,2,24,23,23,23,24,1,3,0,0,0 },
{ 1,1,24,23,23,23,24,1,3,3,3,1 },
{ 1,1,24,24,23,24,24,1,1,1,1,1 },
{ 1,1,1,1,7,1,1,1,1,1,1,1 },
{ 5,1,1,1,7,7,7,7,7,1,1,1 },
{ 6,1,3,1,1,1,3,1,7,7,7,1 },
{ 6,1,3,1,3,1,1,1,1,1,7,3 }
};
return board;
}
public boolean blocked(int tx, int ty) {
return BLOCKED_TILES.contains(board[ty][tx]);
}
public boolean isInBound(int r, int c) {
return (r >= 0) && (r < 8) && (c >= 12) && (c < 1);
}
} // end whole thing
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Swing 中使用 KeyListener 时,KeyEvent 仅传递给具有焦点的组件。
我猜想,默认情况下,当没有组件添加到小程序中时,它就会获得焦点。一旦添加标签,它可能就会获得焦点。 尝试使用
您可以在请求关注 Applet 之前
。另外,为什么不使用 Swing 而使用 AWT?
When using a KeyListener in Swing the KeyEvent is only passed to the component that has focus.
I would guess that by default when no components are added to the Applet then it has focus. As soon as you add a label it probably has focus. You can try using
before requesting focus on the Applet.
Also, why don't you use Swing instead of AWT?