1、public void addWindowListener(WindowListener l)添加指定的窗口偵聽(tīng)器,以從此窗口接收窗口事件。如果 l 為 null,則不拋出任何異常,且不執(zhí)行任何操作。
創(chuàng)新互聯(lián)公司主營(yíng)銀州網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都app軟件開(kāi)發(fā),銀州h5微信小程序搭建,銀州網(wǎng)站營(yíng)銷推廣歡迎銀州等地區(qū)企業(yè)咨詢
這個(gè)是API中的方法定義,此方法參數(shù)為接口WindowListener,任何實(shí)現(xiàn)該接口的類都可以作為參數(shù)。
2、public abstract class WindowAdapter?implements WindowListener, WindowStateListener, WindowFocusListener
接收窗口事件的抽象適配器類。此類中的方法為空。此類存在的目的是方便創(chuàng)建偵聽(tīng)器對(duì)象。
擴(kuò)展此類可創(chuàng)建 WindowEvent 偵聽(tīng)器并為所需事件重寫(xiě)該方法。(如果要實(shí)現(xiàn)
WindowListener 接口,則必須定義該接口內(nèi)的所有方法。此抽象類將所有方法都定義為
null,所以只需針對(duì)關(guān)心的事件定義方法。)
使用擴(kuò)展的類可以創(chuàng)建偵聽(tīng)器對(duì)象,然后使用窗口的 addWindowListener
方法向該窗口注冊(cè)偵聽(tīng)器。當(dāng)通過(guò)打開(kāi)、關(guān)閉、激活或停用、圖標(biāo)化或取消圖標(biāo)化而改變了窗口狀態(tài)時(shí),將調(diào)用該偵聽(tīng)器對(duì)象中的相關(guān)方法,并將
WindowEvent 傳遞給該方法。
3、如果我想在代碼中一次性使用某個(gè)類(抽象類或具體類)或接口,可以使用匿名類的方式,這樣不需自己定義一個(gè)My***類,然后再使用,比較方便。用法就是直接在new WindowAdapter()后面加入類定義,在其中實(shí)現(xiàn)或覆蓋方法就可以了。
匿名類不是返回值,而是相當(dāng)于new String(“hello”)這種的擴(kuò)展形式。我覺(jué)得匿名類的最多用處就是加監(jiān)聽(tīng)器時(shí)。
附上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)聽(tīng)1234這個(gè)端口,代碼如下:
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)聽(tīng)...");
if((socket = server.accept()) != null){
System.out.println("接收到一個(gè)請(qǐng)求"+ 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();
}
}
}
}
先開(kāi)服務(wù)端,再開(kāi)客戶端。