1、public void addWindowListener(WindowListener l)添加指定的窗口偵聽器,以從此窗口接收窗口事件。如果 l 為 null,則不拋出任何異常,且不執(zhí)行任何操作。
創(chuàng)新互聯(lián)公司主營銀州網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都app軟件開發(fā),銀州h5微信小程序搭建,銀州網(wǎng)站營銷推廣歡迎銀州等地區(qū)企業(yè)咨詢
這個是API中的方法定義,此方法參數(shù)為接口WindowListener,任何實現(xiàn)該接口的類都可以作為參數(shù)。
2、public abstract class WindowAdapter?implements WindowListener, WindowStateListener, WindowFocusListener
接收窗口事件的抽象適配器類。此類中的方法為空。此類存在的目的是方便創(chuàng)建偵聽器對象。
擴展此類可創(chuàng)建 WindowEvent 偵聽器并為所需事件重寫該方法。(如果要實現(xiàn)
WindowListener 接口,則必須定義該接口內(nèi)的所有方法。此抽象類將所有方法都定義為
null,所以只需針對關(guān)心的事件定義方法。)
使用擴展的類可以創(chuàng)建偵聽器對象,然后使用窗口的 addWindowListener
方法向該窗口注冊偵聽器。當(dāng)通過打開、關(guān)閉、激活或停用、圖標化或取消圖標化而改變了窗口狀態(tài)時,將調(diào)用該偵聽器對象中的相關(guān)方法,并將
WindowEvent 傳遞給該方法。
3、如果我想在代碼中一次性使用某個類(抽象類或具體類)或接口,可以使用匿名類的方式,這樣不需自己定義一個My***類,然后再使用,比較方便。用法就是直接在new WindowAdapter()后面加入類定義,在其中實現(xiàn)或覆蓋方法就可以了。
匿名類不是返回值,而是相當(dāng)于new String(“hello”)這種的擴展形式。我覺得匿名類的最多用處就是加監(jiān)聽器時。
附上WindowAdapter源代碼:
public?abstract?class?WindowAdapter
implements?WindowListener,?WindowStateListener,?WindowFocusListener
{
public?void?windowOpened(WindowEvent?e)?{}
public?void?windowClosing(WindowEvent?e)?{}
public?void?windowClosed(WindowEvent?e)?{}
public?void?windowIconified(WindowEvent?e)?{}
public?void?windowDeiconified(WindowEvent?e)?{}
public?void?windowActivated(WindowEvent?e)?{}
public?void?windowDeactivated(WindowEvent?e)?{}
public?void?windowStateChanged(WindowEvent?e)?{}
public?void?windowGainedFocus(WindowEvent?e)?{}
public?void?windowLostFocus(WindowEvent?e)?{}
}
比如我要監(jiān)聽1234這個端口,代碼如下:
String ip = "127.0.0.1";
int port = 1234;
try {
Socket socket = new Socket(ip, port);
socket.setSoTimeout(5539900);
java.io.OutputStream out = socket.getOutputStream();
byte[] date = "hello world".getBytes();
out.write(data);
out.flush();
byte[] buffer = new byte[1024];
int len = -1;
java.io.FileOutputStream fout = new java.io.FileOutputStream(
"d:/response.txt");
java.io.ByteArrayOutputStream bout = new java.io.ByteArrayOutputStream();
java.io.InputStream in = socket.getInputStream();
while ((len = in.read(buffer, 0, buffer.length)) 0) {
bout.write(buffer, 0, len);
}
in.close();
bout.flush();
bout.close();
byte[] rdata = bout.toByteArray();
System.out.println(new String(rdata));
fout.write(rdata);
fout.flush();
fout.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
服務(wù)器端的
ServerSocket ss = new ServerSocket(1234);
Socket socket=null;
BufferedReader in;
PrintWriter out;
while (true) {
socket = ss.accept();
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(),true);
String line = in.readLine();
out.println("you input is :" + line);
out.close();
in.close();
socket.close();
}
服務(wù)端:
package com.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket server = null;
Socket socket = null;
try {
server = new ServerSocket(5038);
while(true){
System.out.println("正在監(jiān)聽...");
if((socket = server.accept()) != null){
System.out.println("接收到一個請求"+ socket.getRemoteSocketAddress());
new Thread(new OperThread(socket)).start();
}
}
} catch (IOException e) {
System.out.println("waiting");
}
if(!socket.isConnected()){
socket.close();
server.close();
}
}
}
class OperThread implements Runnable{
Socket socket = null;
BufferedReader br = null;
public OperThread(Socket socket) throws IOException {
this.socket = socket;
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
@Override
public void run() {
try {
String s = null;
while( (s = br.readLine()) != null){
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
客戶端:
package com.socket;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = null;
PrintWriter pw = null;
Scanner scanner = new Scanner(System.in);
try {
socket = new Socket("127.0.0.1",5038);
pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
String s = null;
while((s = scanner.nextLine()) != null){
pw.println(s);
pw.flush();
}
} catch (Exception e) {
System.out.println("\nconnect error");
}finally{
if(pw != null){
pw.close();
}
try {
if(socket != null){
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
先開服務(wù)端,再開客戶端。
網(wǎng)站題目:監(jiān)聽無線設(shè)備JAVA代碼 監(jiān)聽事件java
網(wǎng)站地址:http://vcdvsql.cn/article36/dopeppg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、軟件開發(fā)、網(wǎng)站設(shè)計公司、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站策劃、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)