java,我添加的键盘监听类为什么就是没有反应呢?
源代码经过了删减:publicclassThe_Main{JFramemain_JFrame;JPanelmain_JPanel_left;JPanelmain_JPan...
源代码经过了删减:
public class The_Main
{
JFrame main_JFrame;
JPanel main_JPanel_left;
JPanel main_JPanel_right;
JButton start,end,pause,go_on;
boolean is_there_dot;
public Dot_Thread dot;
public Snake_Thread snake;
public Java_List list;
public int direction;
public void running()
{
direction=39;//左上右下;37,38,39,40
main_JPanel_right.setLayout(null);
main_JFrame.addKeyListener(new My_KeyListener(this));
main_JFrame.add(main_JPanel_left);
main_JFrame.add(main_JPanel_right);
main_JFrame.setBounds(320, 100, 700,500);
main_JFrame.setResizable(false);
main_JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main_JFrame.setVisible(true);
}
}
package theHungerSnake;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class My_KeyListener implements KeyListener
{
The_Main main;
public My_KeyListener(The_Main main)
{
this.main=main;
}
@Override
public void keyPressed(KeyEvent e)
{
//TODO Auto-generated method stub
main.direction=e.getKeyCode();
System.out.println(main.direction);
System.out.println(e.getID());
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
我已经选中了窗口 展开
public class The_Main
{
JFrame main_JFrame;
JPanel main_JPanel_left;
JPanel main_JPanel_right;
JButton start,end,pause,go_on;
boolean is_there_dot;
public Dot_Thread dot;
public Snake_Thread snake;
public Java_List list;
public int direction;
public void running()
{
direction=39;//左上右下;37,38,39,40
main_JPanel_right.setLayout(null);
main_JFrame.addKeyListener(new My_KeyListener(this));
main_JFrame.add(main_JPanel_left);
main_JFrame.add(main_JPanel_right);
main_JFrame.setBounds(320, 100, 700,500);
main_JFrame.setResizable(false);
main_JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main_JFrame.setVisible(true);
}
}
package theHungerSnake;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class My_KeyListener implements KeyListener
{
The_Main main;
public My_KeyListener(The_Main main)
{
this.main=main;
}
@Override
public void keyPressed(KeyEvent e)
{
//TODO Auto-generated method stub
main.direction=e.getKeyCode();
System.out.println(main.direction);
System.out.println(e.getID());
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
我已经选中了窗口 展开
展开全部
你的Frame在你上面的code中没有初始化, 会报null pointer exception.
我已经帮你改好了, 按键会触发event. 你看一下吧
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class The_Main
{
JFrame main_JFrame;
JPanel main_JPanel_left;
JPanel main_JPanel_right;
JButton start, end, pause, go_on;
boolean is_there_dot;
// public Dot_Thread dot;
// public Snake_Thread snake;
// public Java_List list;
public int direction;
public The_Main()
{
running();
}
public void running()
{
direction = 39;// 左上右下;37,38,39,40
// main_JPanel_right.setLayout(null);
main_JFrame = new JFrame();
main_JFrame.addKeyListener(new My_KeyListener(this));
// main_JFrame.add(main_JPanel_left);
// main_JFrame.add(main_JPanel_right);
main_JFrame.setBounds(320, 100, 700, 500);
main_JFrame.setResizable(false);
main_JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main_JFrame.setVisible(true);
}
public class My_KeyListener implements KeyListener
{
The_Main main;
public My_KeyListener(The_Main main)
{
this.main = main;
}
@Override
public void keyPressed(KeyEvent e)
{
// TODO Auto-generated method stub
main.direction = e.getKeyCode();
System.out.println(main.direction);
System.out.println(e.getID());
}
@Override
public void keyReleased(KeyEvent arg0)
{
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0)
{
// TODO Auto-generated method stub
}
}
public static void main(String[] args)
{
new The_Main();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询