
JAVA中的KeyListener的问题!!!
有个程序,无法实现键盘的监听,高手帮忙看下是哪出错了啊!!!程序如下:importjava.awt.Color;importjava.awt.event.ActionEv...
有个程序,无法实现键盘的监听,高手帮忙看下是哪出错了啊!!!
程序如下:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Maze {
static private int WIDTH = 607;
static private int HEIGHT = 670;
static JButton reset;
static JButton help;
private static JPanel bb,cc;
public static void main(String[] args) {
JFrame frame = new JFrame("大虾帮帮忙");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(true);
frame.setLocation(300, 100);
frame.setBackground(Color.WHITE);
Maze maze=new Maze();
bb = maze.jpanel();
cc=maze.panel();
frame.add(cc);
frame.add(bb);
frame.setVisible(true);
}
public JPanel jpanel() {
JPanel jpanel = new JPanel();
jpanel.setBackground(new Color(208, 220, 255));
reset = new JButton("重新开始");
reset.addActionListener(new ActionListener_button());
reset.setActionCommand("reset");
help = new JButton("有问题点一下");
help.addActionListener(new ActionListener_button());
help.setActionCommand("help");
jpanel.add(reset);
jpanel.add(help);
return jpanel;
}
public JPanel panel(){
JPanel panel=new JPanel();
panel.addKeyListener(new listener());
return panel;
}
class ActionListener_button implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("help")) {
System.out.println("2");
} else if (e.getActionCommand().equals("reset")) {
System.out.println("3");
}
}
}
class listener implements KeyListener {
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
System.out.println("6");
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
System.out.println("7");
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
System.out.println("8");
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
System.out.println("9");
}
}
public void keyReleased(KeyEvent e) {
}
}
} 展开
程序如下:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Maze {
static private int WIDTH = 607;
static private int HEIGHT = 670;
static JButton reset;
static JButton help;
private static JPanel bb,cc;
public static void main(String[] args) {
JFrame frame = new JFrame("大虾帮帮忙");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(true);
frame.setLocation(300, 100);
frame.setBackground(Color.WHITE);
Maze maze=new Maze();
bb = maze.jpanel();
cc=maze.panel();
frame.add(cc);
frame.add(bb);
frame.setVisible(true);
}
public JPanel jpanel() {
JPanel jpanel = new JPanel();
jpanel.setBackground(new Color(208, 220, 255));
reset = new JButton("重新开始");
reset.addActionListener(new ActionListener_button());
reset.setActionCommand("reset");
help = new JButton("有问题点一下");
help.addActionListener(new ActionListener_button());
help.setActionCommand("help");
jpanel.add(reset);
jpanel.add(help);
return jpanel;
}
public JPanel panel(){
JPanel panel=new JPanel();
panel.addKeyListener(new listener());
return panel;
}
class ActionListener_button implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("help")) {
System.out.println("2");
} else if (e.getActionCommand().equals("reset")) {
System.out.println("3");
}
}
}
class listener implements KeyListener {
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
System.out.println("6");
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
System.out.println("7");
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
System.out.println("8");
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
System.out.println("9");
}
}
public void keyReleased(KeyEvent e) {
}
}
} 展开
3个回答

2023-08-29 广告
"StormProxies是全球大数据IP资源服务商,其住宅代理网络由真实的家庭住宅IP组成,可为企业或个人提供满足各种场景的代理产品。点击免费测试(注册即送1G流量)StormProxies有哪些优势?1、IP+端口提取形式,不限带宽,I...
点击进入详情页
本回答由Storm代理提供
展开全部
面板要实现KeyListener,必须让其setFocusable(true);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
应该是你的两个面板,有键盘事件的那个面板给盖住了吧?都没有出现啊,修改了一下代码!如文字注释,可以运行了!!
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Maze {
static private int WIDTH = 607;
static private int HEIGHT = 670;
static JButton reset;
static JButton help;
private static JPanel bb,cc;
public static void main(String[] args) {
JFrame frame = new JFrame("大虾帮帮忙");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(true);
frame.setLocation(300, 100);
frame.setBackground(Color.WHITE);
//改变布局,让两上面板都能看到
frame.getContentPane().setLayout(new java.awt.GridLayout());
Maze maze=new Maze();
bb = maze.jpanel();
cc=maze.panel();
frame.add(cc);
frame.add(bb);
frame.setVisible(true);
}
public JPanel jpanel() {
JPanel jpanel = new JPanel();
jpanel.setBackground(new Color(208, 220, 255));
reset = new JButton("重新开始");
reset.addActionListener(new ActionListener_button());
reset.setActionCommand("reset");
help = new JButton("有问题点一下");
help.addActionListener(new ActionListener_button());
help.setActionCommand("help");
jpanel.add(reset);
jpanel.add(help);
return jpanel;
}
public JPanel panel(){
JPanel panel=new JPanel();
//面板上多加入一个按钮,使其可以获得键盘焦点在上面
JButton btn=new JButton("hello");
//按钮也加上键盘事件
btn.addKeyListener(new listener());
panel.add(btn);
panel.addKeyListener(new listener());
return panel;
}
class ActionListener_button implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("help")) {
System.out.println("2-->help");
} else if (e.getActionCommand().equals("reset")) {
System.out.println("3-->reset");
}
}
}
class listener implements KeyListener {
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
System.out.println("6");
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
System.out.println("7");
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
System.out.println("8");
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
System.out.println("9");
}
}
public void keyReleased(KeyEvent e) {}
}
}
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Maze {
static private int WIDTH = 607;
static private int HEIGHT = 670;
static JButton reset;
static JButton help;
private static JPanel bb,cc;
public static void main(String[] args) {
JFrame frame = new JFrame("大虾帮帮忙");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(true);
frame.setLocation(300, 100);
frame.setBackground(Color.WHITE);
//改变布局,让两上面板都能看到
frame.getContentPane().setLayout(new java.awt.GridLayout());
Maze maze=new Maze();
bb = maze.jpanel();
cc=maze.panel();
frame.add(cc);
frame.add(bb);
frame.setVisible(true);
}
public JPanel jpanel() {
JPanel jpanel = new JPanel();
jpanel.setBackground(new Color(208, 220, 255));
reset = new JButton("重新开始");
reset.addActionListener(new ActionListener_button());
reset.setActionCommand("reset");
help = new JButton("有问题点一下");
help.addActionListener(new ActionListener_button());
help.setActionCommand("help");
jpanel.add(reset);
jpanel.add(help);
return jpanel;
}
public JPanel panel(){
JPanel panel=new JPanel();
//面板上多加入一个按钮,使其可以获得键盘焦点在上面
JButton btn=new JButton("hello");
//按钮也加上键盘事件
btn.addKeyListener(new listener());
panel.add(btn);
panel.addKeyListener(new listener());
return panel;
}
class ActionListener_button implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("help")) {
System.out.println("2-->help");
} else if (e.getActionCommand().equals("reset")) {
System.out.println("3-->reset");
}
}
}
class listener implements KeyListener {
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
System.out.println("6");
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
System.out.println("7");
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
System.out.println("8");
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
System.out.println("9");
}
}
public void keyReleased(KeyEvent e) {}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询