首页 \ 问答 \ 求《贪食蛇》源代码 (Java编写的)

求《贪食蛇》源代码 (Java编写的)

更新时间:2023-12-12 12:12

最满意答案

你这个问题是由于焦点(Focus)不正确引起的问题.
在窗口打开之后,如果焦点落在Frame上面,按键被Frame捕获,提示输出,如果焦点落在Button上面,刚按键被Button捕获,当然就没有输出.
更改方法有如下建议:
1.this.setFocusable(true);加在构造函数最后,可以保证Frame可以响应键盘事件,但是按下按扭之后焦点转移,键盘事件不再被Frame响应,因此需要按Tab键重新让Frame得到焦点.(不推荐此方法,不知道按Tab键的用户就惨了...)
2. 将键盘响应KeyListener分别注册给所有的Button.
最后代码如下:
import java.awt.*;
import java.awt.event.*;

public class ButtonTest extends Frame implements KeyListener,ActionListener{
private Button b1;
private Button b2;

public ButtonTest(int i,int j) {
addKeyListener(this);
setLayout(new FlowLayout(1));
b1=new Button("yellow");
b1.addActionListener(this);
b1.addKeyListener(this);
b2=new Button("blue");
b2.addActionListener(this);
b2.addKeyListener(this);
setSize(i, j);
add(b1);add(b2);
pack();
setVisible(true);
this.setFocusable(true);
}
//实现ActionListener接口方法
public void actionPerformed(ActionEvent a) {
if (a.getActionCommand().equals("yellow")) {
b1.setBackground(Color.red);
b2.requestFocus();//点击button1时把事件焦点给b2
} else if (a.getActionCommand().equals("blue")) {
b2.setBackground(Color.BLUE);
}
}
//实现keylistener的3个方法
public void keyTyped(KeyEvent e){
System.out.println("KeyTyped"+" "+e);
}
public void keyPressed(KeyEvent e){
System.out.println("KeyPressed"+" "+e);
}
public void keyReleased(KeyEvent e){
System.out.println("KeyReleased"+" "+e);
}

public static void main(String[] args) {
ButtonTest my=new ButtonTest(300,300);
my.setSize(200,200);
}

}

相关问答

更多
  • 很难实现吧? 即使是满屏窗口的话,下面的任务栏也不可能监听... 要做的话应该还是用鼠标事件监听..
  • JAVA 监听事件[2022-04-01]

    第1个,不是一般的事件,而是日历提醒的功能,要用定时器,或QuartZ开源强大工具 第2个 一般的socket通讯,加强一些。socket 搜索知道就有。
  • 你这个问题是由于焦点(Focus)不正确引起的问题. 在窗口打开之后,如果焦点落在Frame上面,按键被Frame捕获,提示输出,如果焦点落在Button上面,刚按键被Button捕获,当然就没有输出. 更改方法有如下建议: 1.this.setFocusable(true);加在构造函数最后,可以保证Frame可以响应键盘事件,但是按下按扭之后焦点转移,键盘事件不再被Frame响应,因此需要按Tab键重新让Frame得到焦点.(不推荐此方法,不知道按Tab键的用户就惨了...) 2. 将键盘响应KeyLi ...
  • 你问的是JAVASE么? 使用Button的addActionListener就好了 JFrame frame = new JFrame(); frame.setTitle("my frame"); frame.setBounds(0, 0, 200, 100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton btn1 = new JButton("OpenBrowser"); JButton btn2 = new JButton ...
  • 不是通过线程实现的,它是通过一种注册--通知机制实现的。 在java的设计模式中,有一种模式叫:观察者模式,和这个类似。
  • java 事件监听[2022-03-08]

    import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import java.util.*; public class Demo1 extends JFrame implements ActionListener { int i=0; ArrayList al = new ArrayList(2); JPanel img=new JPa ...
  • lz你要明白观察者模式就可以了 这个是我写的 事件、事件源和观察者 明白这个你就明白事件监听了 事件 package test; public class Event { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public Event(String name) { super(); this.name = name ...
  • 让想监听该事件的程序声明进行鼠标事件监听,添加addMouseListener(MouseListener l)方法即可,里面的参数是一个MouseListener的实现者,需要实现一系列关于鼠标事件的响应处理方法
  • 比如 你想实现一个 点下按钮跳出一段字符串来,,, 就需要在按钮上添加一个事件监听,,,, 当按钮被按下的时候,,, 触发跳出一段字符串这个事件.
  • import javax.swing.*; import java.awt.*; import java.awt.event.*; class test extends JFrame implements Runnable{ public static int x,y,direction=0; public test(){ this.setSize(600,400); this.setVisible(true); x=this.getContentPane().getWidth()/2; y=this.ge ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。