色婷婷狠狠18禁久久YY,CHINESE性内射高清国产,国产女人18毛片水真多1,国产AV在线观看

java帶界面群聊和私聊

張越彬1年前9瀏覽0評論

Java是一種強大的編程語言,可以用來開發各種各樣的應用,包括聊天應用。在這里我們介紹一個使用Java編寫的帶界面的群聊和私聊應用程序。

首先,我們需要實現一個基本的聊天界面。可以使用Java Swing庫來實現界面創建和設計。下面是一個簡單的聊天界面的代碼:

import javax.swing.*;
import java.awt.*; 
public class ChatUI extends JFrame {
private JTextArea messageArea;
private JTextField messageField;
private JButton sendButton;
public ChatUI() {
setTitle("Chat App");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setLocationRelativeTo(null);
messageArea = new JTextArea();
messageField = new JTextField(40);
sendButton = new JButton("Send");
JPanel panel = new JPanel();
panel.add(messageField);
panel.add(sendButton);
add(messageArea, BorderLayout.CENTER);
add(panel, BorderLayout.SOUTH);
setVisible(true);
}
public static void main(String[] args) {
new ChatUI();
}
}

接下來,我們需要實現群聊和私聊的功能。可以使用Java Socket編程實現TCP連接,來進行消息的發送和接收。下面是一個簡單的群聊和私聊功能的代碼:

import java.io.*;
import java.net.*;
import java.util.Scanner;
public class ChatApp {
private static final String HOST = "localhost";
private static final int GROUP_PORT = 8080;
private static final int PRIVATE_PORT = 9090;
private BufferedReader reader;
private PrintWriter groupWriter;
private PrintWriter privateWriter;
public ChatApp() {
try {
Socket groupSocket = new Socket(HOST, GROUP_PORT);
Socket privateSocket = new Socket(HOST, PRIVATE_PORT);
reader = new BufferedReader(
new InputStreamReader(System.in));
groupWriter = new PrintWriter(
groupSocket.getOutputStream(), true);
privateWriter = new PrintWriter(
privateSocket.getOutputStream(), true);
new Thread(new GroupReader(groupSocket)).start();
new Thread(new PrivateReader(privateSocket)).start();
System.out.println("Chat App is running...");
System.out.println("Enter 'q' to quit.");
while (true) {
String message = reader.readLine();
if (message == null || message.equals("q")) {
System.exit(0);
}
if (message.startsWith("@")) {
int spaceIndex = message.indexOf(" ");
String to = message.substring(1, spaceIndex);
String text = message.substring(spaceIndex + 1);
privateWriter.println(to + " " + text);
} else {
groupWriter.println(message);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private class GroupReader implements Runnable {
private BufferedReader reader;
public GroupReader(Socket socket) {
try {
reader = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
while (true) {
String message = reader.readLine();
if (message == null) break;
System.out.println(message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class PrivateReader implements Runnable {
private BufferedReader reader;
public PrivateReader(Socket socket) {
try {
reader = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
while (true) {
String message = reader.readLine();
if (message == null) break;
System.out.println("[PRIVATE] " + message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new ChatApp();
}
}

使用這個代碼,可以創建一個基本的Java群聊和私聊應用程序。

上一篇acos php