我将向您展示两个不同的代码来展示我的要求。 第一个代码很好,它在一个类中包含JFrame和KeyListener,但我想要的是将它们分开。
下面的第一个代码......
public class Fgui extends JFrame implements KeyListener{
private JPanel jp;
private Icon background = new ImageIcon(getClass().getResource("back2.png"));
private Icon monster = new ImageIcon(getClass().getResource("mol.png"));
protected JLabel mon;
private JLabel backG;
protected int monX = 300;
private int monY = 225;
protected int monDX = 0;
private int monDY = 0;
protected JLabel ct = new JLabel("Change Text");
protected int code;
public Fgui(){
super("Crazy Monster Eating Game");
this.setSize(750,400);
this.setLayout(null);
mon = new JLabel(monster);
backG = new JLabel(background);
this.addKeyListener(this);
this.add(mon);
mon.setSize(150,150);
mon.setLocation(monX, 225);
this.add(ct);
ct.setSize(750,20);
ct.setLocation(0,0);
this.add(backG);
backG.setSize(750,400);
backG.setLocation(0,0);
}
public void keyPressed(KeyEvent e) {
code = e.getKeyCode();
if(code == KeyEvent.VK_LEFT){
monDX = -40;
ct.setText("left key pressed");
monX = monDX+monX;
}
else if(code == KeyEvent.VK_RIGHT){
monDX = 40;
ct.setText("right key pressed");
monX = monDX+monX;
}else if(code == KeyEvent.VK_ESCAPE){
try{ct.setText("ESC key pressed");
this.dispose();}catch(Exception excep){System.out.println("Failed to EXIT!");}
}else{
ct.setText("Key not registred");
}
mon.setLocation(monX, 225);
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
}
然后我尝试将它们分开,最后得到以下代码,
第二段代码......
JFrame Half
public class Fgui extends JFrame {
private JPanel jp;
private Icon background = new ImageIcon(getClass().getResource("back2.png"));
private Icon monster = new ImageIcon(getClass().getResource("mol.png"));
protected JLabel mon;
private JLabel backG;
protected int monX = 300;
private int monY = 225;
protected int monDX = 0;
private int monDY = 0;
protected JLabel ct = new JLabel("Change Text");
protected int code;
public Fgui(){
super("Crazy Monster Eating Game");
this.setSize(750,400);
this.setLayout(null);
mon = new JLabel(monster);
backG = new JLabel(background);
KeyL KeyLObj = new KeyL();
this.addKeyListener(KeyLObj);
this.add(mon);
mon.setSize(150,150);
mon.setLocation(monX, 225);
this.add(ct);
ct.setSize(750,20);
ct.setLocation(0,0);
this.add(backG);
backG.setSize(750,400);
backG.setLocation(0,0);
}
}
KeyListener Half
public class KeyL extends Fgui implements KeyListener{
public void keyPressed(KeyEvent e) {
code = e.getKeyCode();
if(code == KeyEvent.VK_LEFT){
monDX = -40;
ct.setText("left key pressed");
monX = monDX+monX;
}
else if(code == KeyEvent.VK_RIGHT){
monDX = 40;
ct.setText("right key pressed");
monX = monDX+monX;
}else if(code == KeyEvent.VK_ESCAPE){
try{ct.setText("ESC key pressed");
this.dispose();}catch(Exception excep){System.out.println("Failed to EXIT!");}
}else{
ct.setText("Key not registred");
}
mon.setLocation(monX, 225);
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
}
这里的第二个代码有两个独立的类,一个是JFrame类,另一个是KeyListener类,但是当我编译它时,它不起作用。我认为这是我以错误的方式添加addKeyListener语句的问题。
KeyL KeyLObj = new KeyL();
this.addKeyListener(KeyLObj);
我尝试通过KeyL类添加它,但它也没有用。
我想这样做的原因是因为我不希望代码在一个类中被占用,而是将它分成2以使其看起来更干净。如果我想添加更多关键事件,我可以在那一个类中进行。
答案 0 :(得分:2)
你看到的问题非常简单。因为KeyL
是Fgui
的子类,所以每次构造新的KeyL
时,所有Fgui
初始化代码都会运行。
所以你走到这一行:
KeyL KeyLObj = new KeyL();
这会创建一个新的KeyL
,因此在该行完成之前,必须创建一个新的KeyL
对象。由于KeyL
是Fgui
,因此会调用Fgui
构造函数。并且FGui
构造函数最终会到达同一行KeyL KeyLObj = new KeyL();
,因此该过程会重复。你永远不能越过那一行,因为每个新的KeyL
需要在完全构造之前创建另一个KeyL
,所以程序只是不断创建新对象,直到它用完空间。
答案是KeyL
没有理由成为Fgui
。让它只是实现KeyListener
,而不是扩展Fgui
。然后,确保它可以访问完成任务所需的所有变量。
您的大多数变量不应该是对象中的字段;它们应该只是局部变量。任何不需要存在于该函数之外的函数,或者在调用之间保持它的值,都应该更改为局部变量。看起来只有两个重要的是mon
,ct
,以及您使用Fgui
引用的this
。所以将这些参数放入KeyL
的构造函数中(作为两个JLabel
和一个JFrame
),然后在那些上调用你的方法。