<b id="yt35u"></b>
  • <b id="yt35u"></b>

  • <tt id="yt35u"><form id="yt35u"></form></tt>
    <button id="yt35u"></button>

    <button id="yt35u"></button>

    <strike id="yt35u"><dfn id="yt35u"></dfn></strike>

    <i id="yt35u"></i>
    1. <tt id="yt35u"></tt>
      <b id="yt35u"><sub id="yt35u"><tr id="yt35u"></tr></sub></b>

      教育路上

      全國站>Java>網絡編程>java網絡聊天室客戶端
      學員需求

      java網絡聊天室客戶端

      摘要:實現一個可視化的網絡聊天室客戶端程序 java 網絡 聊天室 客戶端 任務目標 掌握常用窗體控件的使用,掌握網絡 API,掌握聊天室客戶端程序的實現思路。以下是我們為大家整理的,相信大家閱讀完后肯定有了自己的選擇吧。

      2022-12-02 16:14網絡推薦

      發布時間:
      2022-12-02 16:14
      信息來源:
      網絡推薦
      瀏覽次數:
      1487
      java網絡聊天室客戶端

      網絡聊天室客戶端
      任務介紹
      實現一個可視化的網絡聊天室客戶端程序
      任務目標
      掌握常用窗體控件的使用,掌握網絡 API,掌握聊天室客戶端程序的實現思路
      實現思路
      設計一個聊天室界面,通過輸入用戶名并單擊連接按鈕進入聊天室,通過該客

      戶端,可以向所有用戶發送信息,也可以向指定用戶發送點對點信息。

      實現代碼:
      ChatRoomClientUI.java


      package aw;
      
      import java.awt.BorderLayout;
      import java.awt.Dimension;
      import java.awt.FlowLayout;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.util.Set;
      import javax.swing.DefaultListModel;
      import javax.swing.JButton;
      import javax.swing.JComboBox;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JList;
      import javax.swing.JPanel;
      import javax.swing.JScrollPane;
      import javax.swing.JTextArea;
      import javax.swing.JTextField;
      import javax.swing.ListModel;
      import javax.swing.ListSelectionModel;
      import javax.swing.border.Border;
      
      public class ChatRoomClientUI extends JFrame {
      	private JTextField jtfIP, jtfPort, jtfChat, jtfReciver, jtfName;
      	private JLabel lblIP, lblPort, lblName;
      	private JButton btnConnect, btnSend;
      	private JTextArea jtaChat;
      	private JComboBox jcbChatState;
      	private JList userList;
      	private ChatRoomClientUI ui;
      	private ChatRoomClient client;
      
      	public ChatRoomClientUI() {
      //頂部
      		ui = this;
      		JPanel topPanel = new JPanel();
      		lblIP = new JLabel("IP:");
      		lblPort = new JLabel("端口");
      		jtfIP = new JTextField("127.0.0.1");
      		jtfPort = new JTextField("6666");
      		lblName = new JLabel("昵稱");
      		jtfName = new JTextField(8);
      		btnConnect = new JButton("連接");
      		topPanel.add(lblIP);
      		topPanel.add(jtfIP);
      		topPanel.add(lblPort);
      		topPanel.add(jtfPort);
      		topPanel.add(lblName);
      		topPanel.add(jtfName);
      		topPanel.add(btnConnect);
      		add(topPanel, BorderLayout.NORTH);
      //主體
      		jtaChat = new JTextArea();
      		JScrollPane mainPanel = new JScrollPane(jtaChat);
      //
      		jtaChat.setPreferredSize(new Dimension(600, 480));
      		mainPanel.setPreferredSize(new Dimension(600, 480));
      		add(mainPanel);
      //底部
      		JPanel bottomPanel = new JPanel();
      		bottomPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
      		String[] str = { "所有人", "私聊" };
      		jcbChatState = new JComboBox(str);
      		jtfChat = new JTextField(50);
      		jtfReciver = new JTextField(8);
      		btnSend = new JButton("發送");
      		bottomPanel.add(jcbChatState);
      		bottomPanel.add(jtfReciver);
      		bottomPanel.add(jtfChat);
      		bottomPanel.add(btnSend);
      		add(bottomPanel, BorderLayout.SOUTH);
      //右邊 在線列表
      		JScrollPane rightPanel = new JScrollPane();
      		rightPanel.setPreferredSize(new Dimension(200, 480));
      		DefaultListModel dlm = new DefaultListModel();
      		userList = new JList(dlm);
      		userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      		rightPanel.setViewportView(userList);
      		add(rightPanel, BorderLayout.EAST);
      		pack();
      		setTitle("簡單聊天室客戶端");
      		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      		setVisible(true);
      		btnConnect.addActionListener(new ActionListener() {
      			@Override
      			public void actionPerformed(ActionEvent e) {
      // TODO Auto-generated method stub
      				client = new ChatRoomClient(ui);
      				client.connect(jtfIP.getText(), Integer.parseInt(jtfPort.getText()));
      				Thread tr = new Thread(client);
      				tr.start();
      				client.sendWelcome(jtfName.getText());
      			}
      		});
      		btnSend.addActionListener(new ActionListener() {
      			@Override
      			public void actionPerformed(ActionEvent arg0) {// TODO Auto-generated method stub
      				String state = (String) jcbChatState.getSelectedItem();
      				if ("所有人".equals(state)) {
      					jtaChat.append(jtfChat.getText() + "\r\n");
      					String msg = "COMMEN@" + jtfName.getText() + "@" + jtfChat.getText();
      					client.sendMsg(msg);
      				} else {
      					String reciverName = jtfReciver.getText();
      					if (isReciverExist(reciverName)) {
      						if (reciverName.equals(jtfName.getText()))
      							jtaChat.append("你對自己說:" + jtfChat.getText());
      						else {
      							String msg = "PRIVACY@" + jtfName.getText() + "@" + reciverName + "@" + jtfChat.getText();
      							client.sendMsg(msg);
      							jtaChat.append(msg + "\r\n");
      						}
      					}
      				}
      			}
      		});
      	}
      
      	public void updateUI(String str) {
      		jtaChat.append(str + "\r\n");
      	}
      
      	public void addUser(String[] users) {
      		DefaultListModel model = (DefaultListModel) userList.getModel();
      		for (String user : users)
      			model.addElement(user);
      	}
      
      	public void addUser(String user) {
      		DefaultListModel model = (DefaultListModel) userList.getModel();
      		if (!user.equals(jtfName.getText()))
      			model.add(0, user);
      	}
      
      	private boolean isReciverExist(String reciverName) {
      		DefaultListModelmodel = (DefaultListModel) userList.getModel();
      		for (int i = 0; i < model.size(); i++) if (model.get(i).equals(reciverName)) return true; return false; } }




      ChatRoomClient.java


      package aw;
      
      import java.io.BufferedInputStream;
      import java.io.BufferedOutputStream;
      import java.io.IOException;
      import java.io.ObjectInputStream;
      import java.io.PrintWriter;
      import java.net.Socket;
      import java.net.UnknownHostException;
      import java.util.Scanner;
      import java.util.Set;
      import javax.swing.SwingUtilities;
      
      public class ChatRoomClient implements Runnable {
      	ChatRoomClientUI ui;
      	Socket socket;
      	PrintWriter pw;
      
      	public ChatRoomClient(ChatRoomClientUI ui) {
      // TODO Auto-generated constructor stub
      		this.ui = ui;
      	}
      
      	@Override
      	public void run() {
      // TODO Auto-generated method stub
      		BufferedInputStream bis;
      		try {
      			bis = new BufferedInputStream(socket.getInputStream());
      			Scanner scanner = new Scanner(bis);
      			while (true) {
      				String rawString = scanner.nextLine();
      				String[] str = rawString.split("@");
      				switch (str[0]) {
      				case "WELCOME":
      					SwingUtilities.invokeLater(new Runnable() {
      						@Override
      						public void run() {
      // TODO Auto-generated method stub
      							ui.updateUI("歡迎你" + str[1] + "\r\n");
      							ui.addUser(str[1]);
      						}
      					});
      					break;
      				case "LIST":
      					String[] users = new String[str.length - 1];
      					for (int i = 1; i < str.length; i++) users[i - 1] = str[i]; SwingUtilities.invokeLater(new Runnable() { @Override public void run() { // TODO Auto-generated method stub ui.addUser(users); } }); break; case "COMMEN": SwingUtilities.invokeLater(new Runnable() { @Override public void run() { // TODO Auto-generated method stub ui.updateUI(str[1]); } }); break; case "PRIVACY": String msg = str[1] + "悄悄對你說:" + str[2]; SwingUtilities.invokeLater(new Runnable() { @Override public void run() { // TODO Auto-generated method stub ui.updateUI(msg); } }); default: break; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void connect(String ip, int port) { try { socket = new Socket(ip, port); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void sendWelcome(String name) { try { pw = new PrintWriter(new BufferedOutputStream(socket.getOutputStream())); pw.println("WELCOME@" + name); pw.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void sendMsg(String msg) { // TODO Auto-generated method stub try { pw = new PrintWriter(new BufferedOutputStream(socket.getOutputStream())); pw.println(msg); pw.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
       Example02.java



      package aw;
      
      public class Example02 {
      public static void main(String[] args) {
      // TODO Auto-generated method stub
      ChatRoomClientUI frame=new ChatRoomClientUI();
      }
      }


      上一篇:
      java聊天室服務器端程序
      下一篇:
      java實現文件上傳服務器端和客戶端
      標簽: 聊天室 客戶端
      網友評論
      發布評論

      訪客的評論 2024/07/10 12:43

      文中描述的是準確的嗎,如何報名!

      我也來發表評價關閉
      我對該內容的評價:
      0
      評價500
      驗證碼: 看不清 換一張
      提交 (匿名發布,無須擔心別人知道您的身份)
      學校免費發布信息關閉
      我們審核后會盡快展示,如有圖片請發郵件到:edu63@foxmail.com

      姓      名:

      內      容:

      手機號碼:

      驗  證  碼:  換一張

      確認提交
      填寫需求信息關閉
      我們會根據您的需求匹配并審核留言

      姓      名:

      意向城市:

      留      言:

      手機號碼:

      驗  證  碼:  換一張

      確認提交
      完善補充本文信息關閉
      非常感謝您幫助完善補充本文信息


       換一張

      確認提交
      女班长给我看她的小内内

        <b id="yt35u"></b>
    1. <b id="yt35u"></b>

    2. <tt id="yt35u"><form id="yt35u"></form></tt>
      <button id="yt35u"></button>

      <button id="yt35u"></button>

      <strike id="yt35u"><dfn id="yt35u"></dfn></strike>

      <i id="yt35u"></i>
      1. <tt id="yt35u"></tt>
        <b id="yt35u"><sub id="yt35u"><tr id="yt35u"></tr></sub></b>