如何在java中使用keytyped输入数字?

a64a0gku  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(246)

抱歉,如果这是一个重复的问题,但我一直在寻找答案,无法找到一个解决我的问题。我对java相当陌生,我正在创建一个游戏,希望使用键盘输入键输入,但是当我尝试输入一个数字时,它不会显示在我的gui上。我创建了一个addkeylistener,它获取键代码,然后从另一个类调用一个方法来分配移动。没有显示错误,因此很难调试。我是做错了什么还是有办法调试这个问题?
panelcell类

public static Font font = new Font("Dialog", Font.BOLD, 20);
  private GamePanel panel;
  private int row;
  private int col;
  private static final int KEY_TYPED = 0;

  public PanelCell(GamePanel p,  int r, int c)
  {
    if (p == null)
     throw new FutoshikiException("cannot have null panel");
    if ((r<1) || (r>Futoshiki.SETSIZE))
      throw new FutoshikiException("invalid row (" + r + ")");
    if ((c<1) || (c>Futoshiki.SETSIZE))
      throw new FutoshikiException("invalid col (" + c + ")");

    panel = p;
    row   = r;
    col   = c;

    addMouseListener(new MouseAdapter() {
      @Override
      public void mouseEntered(MouseEvent e) {
        requestFocusInWindow();
      }
    });

    addKeyListener(new KeyAdapter() {
      @Override
      public void keyTyped(KeyEvent e) {
        int num = e.getKeyChar();
        panel.assignMove(row, col, num);
      }
    });

    setHorizontalAlignment(CENTER);
    setMargin(new Insets(5,5,5,5));
    setForeground(Color.black);
    setFont(font);
    setPreferredSize(new Dimension(50,50));
  }
}

游戏面板类

private Futoshiki game = null;
private PanelCell[][] cells = null;
private JTextArea status = null;

  public GamePanel()
  {
    int row, col;
    grid  = new JPanel(new GridLayout(Futoshiki.SETSIZE,Futoshiki.SETSIZE));
    cells = new PanelCell[Futoshiki.SETSIZE+1][Futoshiki.SETSIZE+1];  

    for (row=1; row<=Futoshiki.SETSIZE; row++) 
    {
      for (col=1; col<=Futoshiki.SETSIZE; col++) 
      {
        cells[row][col] = new PanelCell(this,row,col);
        grid.add(cells[row][col]);
      }
    }

    setLayout(new BorderLayout());
    add(grid, BorderLayout.NORTH);
    status = new JTextArea();
    add(new JScrollPane(status), BorderLayout.SOUTH);
    game = new Futoshiki();

  }

  void assignMove(int row, int col, int num)
  {
    if ((row<1) || (row>Futoshiki.SETSIZE)) {
      throw new FutoshikiException("invalid row (" + row + ")");
    }

    if ((col<1) || (col>Futoshiki.SETSIZE)) {
      throw new FutoshikiException("invalid column (" + col + ")");
    }

    if (game.isAssigned(row, col)) {
      throw new FutoshikiException("the cell at (" + row + "," + col + ") is assigned (" + game.getNum(row,col) + ")");
    }

    if ((num<1) || (num>Futoshiki.SETSIZE)) {
      throw new FutoshikiException("invalid value");

    }
    if (!game.isValidAssign(row, col, num)) {
      throw new FutoshikiException("invalid value assignment");
    }

    Assign userMove = new Assign(row, col, num);
    game.assign(userMove);
  }

  public static void main(String[] args) 
  {
    JFrame frame = new JFrame("Futoshiki");
    GamePanel panel = new GamePanel();
    frame.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
  }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题