JAVA 键盘监听程序是什么

JAVA 键盘监听程序是什么,第1张

thisaddKeyListener(newKeyAdapter(){\x0d\publicvoidkeyPressed(KeyEvente){\x0d\if(egetKeyCode()==KeyEventVK_RIGHT)\x0d\by=by+5;\x0d\elseif(egetKeyCode()==KeyEventVK_LEFT)\x0d\by=by-5;\x0d\}\x0d\});\x0d\这里这样改就ok了,不过你这个程序还有好多问题呢。\x0d\publicvoidpaint(Graphicsg){\x0d\\x0d\gdrawImage(img2,0,0,this);\x0d\gdrawImage(img3,by,490,this);\x0d\gdrawImage(img,580,260,this);\x0d\repaint();\x0d\}\x0d\这里面还要加一个repaint();方法,要不看不出移动的,问题太多改不完,你问的问题我算是解决了,嘿嘿,给分吧,有什么问题可以hi我。

代码一

implements

ActionListener

说明:实现接口,没有为什么

代码二

b1addActionListener(this)

说明:为b1注册,参数是指交给谁处理,这里是this

代码三

这里是做什么用的?

代码四

b1setText(“已登录”)

好像是这个函数。

*** 作系统,有两个层次,第一个层次是对组装起电脑的各个硬件的驱动程序;第二个是建立在驱动程序上的各项应用程序,比如桌面显示、键盘监听后的处理程序等等。如果想要进行监听键盘,那么就需要获取到所使用 *** 作系统给出的api接口,以windows系统为例,是用c和c++进行编写的,所以他们提供的api都是c和c++的api接口。于是用java的话就需要用到java的native方法,一个Native Method就是一个java调用非java代码的接口。所以3个步骤:

1、引用java native方法。

2、研究windows api。

3、调用windows api中的键盘监听接口,完成程序。

/

WindowListener:窗口监听事件 ex:关闭、最小化

@author Administrator

/

public class WindowListenerDemo {

public static void main(String[] args) {

// TODO Auto-generated method stub

JFrame frame = new JFrame("窗口程序");//创建一个带标题的窗口对象frame

framesetVisible(true);//显示窗口

framesetSize(600, 400);//设置窗口大小

framesetLocationRelativeTo(null);//设置窗口居中

//framesetDefaultCloseOperation(3);//点击X关闭程序 3为结束程序

framesetLayout(null);//清空布局

MyWindowListener myWindowListener = new MyWindowListener();//创建一个窗口监听事件对象

frameaddWindowListener(myWindowListener);//添加myMouseListener鼠标监听事件

framedispose();//关闭程序

}

}

class MyWindowListener implements WindowListener{

@Override

public void windowOpened(WindowEvent e) {

// TODO Auto-generated method stub

}

@Override

public void windowClosing(WindowEvent e) {

// TODO Auto-generated method stub

}

/

关闭窗口之后,自动调用此方法

/

@Override

public void windowClosed(WindowEvent e) {

// TODO Auto-generated method stub

Systemoutprintln("您关闭了窗口");

}

/

最小化窗口之后,自动调用此方法

/

@Override

public void windowIconified(WindowEvent e) {

// TODO Auto-generated method stub

Systemoutprintln("您把窗口最小化了");

}

@Override

public void windowDeiconified(WindowEvent e) {

// TODO Auto-generated method stub

}

@Override

public void windowActivated(WindowEvent e) {

// TODO Auto-generated method stub

}

@Override

public void windowDeactivated(WindowEvent e) {

// TODO Auto-generated method stub

}

}

String ip = "127001";

int port = 1234;

try {

Socket socket = new Socket(ip, port);

socketsetSoTimeout(5539900);

javaioOutputStream out = socketgetOutputStream();

byte[] date = "hello world"getBytes();

outwrite(data);

outflush();

byte[] buffer = new byte[1024];

int len = -1;

javaioFileOutputStream fout = new javaioFileOutputStream(

"d:/responsetxt");

javaioByteArrayOutputStream bout = new javaioByteArrayOutputStream();

javaioInputStream in = socketgetInputStream();

while ((len = inread(buffer, 0, bufferlength)) > 0) {

boutwrite(buffer, 0, len);

}

inclose();

boutflush();

boutclose();

byte[] rdata = bouttoByteArray();

Systemoutprintln(new String(rdata));

foutwrite(rdata);

foutflush();

foutclose();

socketclose();

} catch (UnknownHostException e) {

eprintStackTrace();

} catch (IOException e) {

eprintStackTrace();

}

}

服务器端的

ServerSocket ss = new ServerSocket(1234);

Socket socket=null;

BufferedReader in;

PrintWriter out;

while (true) {

socket = ssaccept();

in = new BufferedReader(new InputStreamReader(socketgetInputStream()));

out = new PrintWriter(socketgetOutputStream(),true);

String line = inreadLine();

outprintln("you input is :" + line);

outclose();

inclose();

socketclose();

}

以上就是关于JAVA 键盘监听程序是什么全部的内容,包括:JAVA 键盘监听程序是什么、java为按钮b1注册事件监听程序 在线求讲解、如何实现java在后台监听键盘等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/10140186.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-05
下一篇2023-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存