Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将KeyListener和JFrame分为两个不同的类_Java_Jframe_Jpanel_Keypress_Keylistener - Fatal编程技术网

Java 将KeyListener和JFrame分为两个不同的类

Java 将KeyListener和JFrame分为两个不同的类,java,jframe,jpanel,keypress,keylistener,Java,Jframe,Jpanel,Keypress,Keylistener,我将向您展示两种不同的代码,以演示我的要求。 第一个代码运行良好,它在一个类中包含JFrame和KeyListener,但是我想将它们分开 下面的第一个代码… public class Fgui extends JFrame implements KeyListener{ private JPanel jp; private Icon background = new ImageIcon(getClass().getResource("back2.png")); private Icon mo

我将向您展示两种不同的代码,以演示我的要求。 第一个代码运行良好,它在一个类中包含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) {}
}
然后我试着把它们分开,最后得到了下面的代码

第二个代码…

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);
}

}
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) {}
}
KeyListener一半

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);
}

}
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类添加它,但也没有成功


我之所以要这样做,是因为我不想把代码都压缩在一个类中,而是把它分成两个类,使它看起来更干净。如果我想添加更多的关键事件,我可以在一个类中完成。

您看到的问题非常简单。由于将
KeyL
作为
Fgui
的子类,因此每次构造新的
KeyL
时,所有
Fgui
初始化代码都将运行

那么你就到这一行了:

KeyL KeyLObj = new KeyL();
这将创建一个新的
KeyL
,因此在该行结束之前,必须创建一个新的
KeyL
对象。由于
KeyL
Fgui
,因此会调用
Fgui
构造函数。而
FGui
构造函数最终到达同一行,
keylkeylobj=newkeyl(),因此该过程会自动重复。你永远无法越过那一行,因为每个新的
KeyL
在完全构建之前都需要制作另一个
KeyL
,所以程序只是不断制作新对象,直到空间用完为止

答案是,
KeyL
没有理由成为
Fgui
。让它只实现
keylister
,而不是扩展
Fgui
。然后,确保它可以访问完成任务所需的所有变量


大多数变量不应该是对象中的字段;它们应该只是局部变量。任何不需要存在于该函数之外或在调用之间保留其值的内容都应该更改为局部变量。看起来唯一重要的两个是
mon
ct
,以及您使用
this
引用的
Fgui
。因此,将这些参数放入
KeyL
的构造函数中(作为两个
JLabel
s和一个
JFrame
),然后调用这些参数的方法。

您看到的问题非常简单。由于将
KeyL
作为
Fgui
的子类,因此每次构造新的
KeyL
时,所有
Fgui
初始化代码都将运行

那么你就到这一行了:

KeyL KeyLObj = new KeyL();
这将创建一个新的
KeyL
,因此在该行结束之前,必须创建一个新的
KeyL
对象。由于
KeyL
Fgui
,因此会调用
Fgui
构造函数。而
FGui
构造函数最终到达同一行,
keylkeylobj=newkeyl(),因此该过程会自动重复。你永远无法越过那一行,因为每个新的
KeyL
在完全构建之前都需要制作另一个
KeyL
,所以程序只是不断制作新对象,直到空间用完为止

答案是,
KeyL
没有理由成为
Fgui
。让它只实现
keylister
,而不是扩展
Fgui
。然后,确保它可以访问完成任务所需的所有变量

大多数变量不应该是对象中的字段;它们应该只是局部变量。任何不需要存在于该函数之外或在调用之间保留其值的内容都应该更改为局部变量。看起来唯一重要的两个是
mon
ct
,以及您使用
this
引用的
Fgui
。因此,将这些参数放入
KeyL
的构造函数中(作为两个
JLabel
s和一个
JFrame
),然后对这些参数调用您的方法