我按照课堂上的指示规范编写河内塔计划。在继续之前,我想声明我已经搜索了类似的答案并理解它最好使用KeyBinding。我们需要在类中使用KeyListener。
如果您想了解河内塔的工作方式,请在此处阅读:https://en.wikipedia.org/wiki/Tower_of_Hanoi
我的程序使用三个杆中的每个杆的键1,2和3。您单击这两次以进行一次移动:第一次单击指定发送杆,第二次单击指定接收杆。
我在构造函数中添加了addKeyListener(this)
,并编写了addNotify()
方法{requestFocus();}
。编译不会产生任何错误。我在KeyPressed方法的几个区域中添加了System.out.println("This ran!");
以查看它是否运行 - 它没有。
可能有用的唯一其他信息是我使用getKeyChar()方法来识别正在按下哪个键。
如果我错过了正确使用KeyListener和KeyPressed的某些方面的任何帮助或评论将非常感激。 感谢。
以下是重新创建示例所需的最少代码:
主类:
class MainFile {
public static void main(String[] args) {
new TFrame("Frame");
}
}
小组类:
import java.awt.*;
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class TPanel extends JPanel implements KeyListener {
boolean towerA;
boolean towerB;
boolean towerC;
public TPanel() {
super();
setSize(600, 600);
addKeyListener(this);
}
public void keyTyped(KeyEvent e) {
}
public void addNotify() {
requestFocus();
}
public void paint(Graphics g) {
addNotify();
if (towerA == true) {
g.setColor(Color.GREEN);
g.fillRect(20, getHeight(), 40, 100);
} else {
g.setColor(Color.RED);
g.fillRect(20, 0, 40, 100);
}
if (towerB == true) {
g.setColor(Color.GREEN);
g.fillRect(100, 0, 40, 100);
} else {
g.setColor(Color.RED);
g.fillRect(100, 0, 40, 100);
}
if (towerC == true) {
g.setColor(Color.GREEN);
g.fillRect(180, 0, 40, 100);
} else {
g.setColor(Color.RED);
g.fillRect(180, 0, 40, 100);
}
repaint();
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
addNotify();
if (e.getKeyChar() == '1') {
towerA = true;
}
if (e.getKeyChar() == '2') {
towerB = true;
}
if (e.getKeyChar() == '3') {
towerC = true;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
帧类:
import javax.swing.JFrame;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.awt.Dimension;
public class TFrame extends JFrame implements java.awt.event.KeyListener {
public TFrame(String title) {
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
pack();
addKeyListener(this);
TPanel p = new TPanel();
Insets frameInsets = getInsets();
int frameWidth = p.getWidth() + (frameInsets.left + frameInsets.right);
int frameHeight = p.getHeight() + (frameInsets.top + frameInsets.bottom);
setPreferredSize(new Dimension(frameWidth, frameHeight));
setLayout(null);
add(p);
pack();
setVisible(true);
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
更新:我已经进行了一些进一步的研究,发现它可能是关注组件的东西。我在整个程序中添加了许多焦点和addNotify()调用,但似乎没有效果。
答案 0 :(得分:2)
这是一个单一的复制/粘贴,编译,运行是一个MCVE。在问题中看到的代码存在一些问题。在修复问题的根本原因之前,我修复了其中一些(请参阅代码中的注释),因为代码会覆盖addNotify()
方法。请注意,在任何情况下,对requestFocus()
的调用都将失败,因为组件不仅需要可聚焦,而且在屏幕上也可以看到它可以工作。
该部分留作练习让您实施,但如果您遇到问题,请使用MCVE发布另一个问题。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TPanel extends JPanel implements KeyListener {
boolean towerA;
boolean towerB;
boolean towerC;
public TPanel() {
super();
setSize(600, 600);
// for testing
setBackground(Color.BLACK);
addKeyListener(this);
// A component must BE focusable before it can hop to accept the focus.
setFocusable(true);
// create an animation listener
ActionListener animationListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
};
// create and start an animation timer
Timer timer = new Timer(200, animationListener);
timer.start();
requestFocus();
}
public void keyTyped(KeyEvent e) {
}
@Override
/* I've never bothered to find out what addNotify() method is for.
Inadvisable to use it to request the focus!
public void addNotify() {
requestFocus();
} */
/* For any JComponent, we should override the paintComponent(Graphics)
method rather than the paint(Graphics) method. */
public void paintComponent(Graphics g) {
/* 1st thing should be done in any overridden paint method is to
call the super method. */
super.paintComponent(g);
//addNotify();
if (towerA == true) {
g.setColor(Color.GREEN);
g.fillRect(20, getHeight(), 40, 100);
} else {
g.setColor(Color.RED);
g.fillRect(20, 0, 40, 100);
}
if (towerB == true) {
g.setColor(Color.GREEN);
g.fillRect(100, 0, 40, 100);
} else {
g.setColor(Color.RED);
g.fillRect(100, 0, 40, 100);
}
if (towerC == true) {
g.setColor(Color.GREEN);
g.fillRect(180, 0, 40, 100);
} else {
g.setColor(Color.RED);
g.fillRect(180, 0, 40, 100);
}
/* As mentioned in comment, this will cause an infinite loop & block
the EDT! Use a Swing Timer for animation. */
// repaint();
}
@Override
public void keyPressed(KeyEvent e) {
// for testing
System.out.println("e: " + e);
addNotify();
if (e.getKeyChar() == '1') {
towerA = true;
}
if (e.getKeyChar() == '2') {
towerB = true;
}
if (e.getKeyChar() == '3') {
towerC = true;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
public static void main(String[] args) {
new TFrame("Frame");
}
}
class TFrame extends JFrame implements java.awt.event.KeyListener {
public TFrame(String title) {
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
pack();
addKeyListener(this);
TPanel p = new TPanel();
Insets frameInsets = getInsets();
int frameWidth = p.getWidth() + (frameInsets.left + frameInsets.right);
int frameHeight = p.getHeight() + (frameInsets.top + frameInsets.bottom);
setPreferredSize(new Dimension(frameWidth, frameHeight));
// Java GUIs were designed to work with layouts. Use them!
// setLayout(null);
add(p);
pack();
setVisible(true);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
}