java - 失去焦点

标签 java applet focus

我有一个小程序,您可以使用 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

最佳答案

在 Swing 中使用 KeyListener 时,KeyEvent 仅传递给具有焦点的组件。

我猜想,默认情况下,当没有组件添加到小程序中时,它就会获得焦点。一旦添加标签,它可能就会获得焦点。您可以尝试使用

this.setFocusable( true );

在请求关注 Applet 之前。

另外,为什么不使用 Swing 而使用 AWT?

关于java - 失去焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3217628/

相关文章:

java - 如何确定 JFrame 被移动的原因(Windows、JDK6)

javascript - 图片 :focus doesn't stay focus in CSS

qt - 在QML中forceActiveFocus()vs focus = true

iis-7 - 错误 : KeyUsage does not allow digital signatures - Java-applet + mutual SSL

聚焦时 tvOS 中的 UITextField 边框

JAVA:创建 boolean 变量或在 If 语句中放置参数?

Java 泛型 : instantiating A with a single constructor argument B

java - 使用 JAX-RS 定义自定义参数类型以按标识符返回数据

java - 2 个 Web 应用程序中的两个小程序访问相同的 DLL

java - 运行签名小程序时出现"Class No Found"错误