您好,登錄后才能下訂單哦!
本文為大家分享了java實現簡單聊天軟件的具體代碼,供大家參考,具體內容如下
介紹
這是我自己學習Socket時寫的,用到了多線程,Socket的知識,還有Java的IO技術,并且做了一個Java的UI,廢話不多說,先上效果圖
SendThread
這個類是用來實現數據的發送的,當按鍵按下時,會調用sendMessage方法
public class SendThread { ChatWindow chatWindow; private String remoteIP = ""; private int port = 0; private String message = ""; public SendThread(int port,ChatWindow window) { chatWindow = window; } public void notRun() { InetSocketAddress isa = new InetSocketAddress(remoteIP, port); try { Socket socket = new Socket(); socket.connect(isa); OutputStreamWriter writer = new OutputStreamWriter(socket.getOutputStream()); writer.write(message); writer.flush(); writer.close(); System.out.println("將數據寫入到流中"); } catch (IOException e) { e.printStackTrace(); }finally{ message = ""; } } public void senMessage(String host,int port,String message){ remoteIP = host; this.port = port; this.message = message; notRun(); } }
ReceiveThread
這個類實現的功能是維護一個阻塞線程,來對端口進行監聽,接受收到的信息,每當收到信息時,都會顯示。
public class ReceiveThread extends Thread { private ServerSocket server; private ChatWindow window; public ReceiveThread(ChatWindow window) { this.window = window; try { server = new ServerSocket(0); window.setLocalPort(server.getLocalPort()); start(); } catch (IOException e) { // TODO Auto-generated catch block window.printError("連接出錯"); } } @Override public void run() { // TODO Auto-generated method stub while(true){ Socket socket; try { socket = server.accept(); InputStreamReader reader = new InputStreamReader(socket.getInputStream()); int c; StringBuilder sb = new StringBuilder(); while((c = reader.read()) != -1){ sb.append((char)c); } window.setReceive(sb.toString()); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("線程將接受到的數據寫入對話框出錯"); } } } }
ChatWindow
這個類是UI的實現,通過綁定各個按鈕的操作來實現別的功能
public class ChatWindow extends JFrame { private JButton sendButton; private JButton cancelButton; private JButton receiveButton; private JTextArea receiveText; private JTextField sendText; private JLabel localPort; private JTextField remoteAddress; private JTextField remotePort; private SendThread sendThread; private ReceiveThread receiveThread; private void GUIini(){ Container c = this.getContentPane(); c.setLayout(new BorderLayout()); setSize(400, 500); //第一個panel JPanel panel1 = new JPanel(new GridLayout(4, 2)); // JPanel panel1 = new JPanel(new (4, 2)); panel1.setSize(400, 75); panel1.add(new JLabel("當前機器的IP地址是:")); try { panel1.add(new JLabel(InetAddress.getLocalHost().getHostAddress())); } catch (UnknownHostException e) { // TODO Auto-generated catch block panel1.add(new JLabel("當前機器的IP地址是:" + "Unknown")); } panel1.add(new JLabel("當前對話的端口是:")); panel1.add(localPort = new JLabel(""+0)); panel1.add(new JLabel("Remote Host IP Address:")); remoteAddress = new JTextField(); remoteAddress.setColumns(0); panel1.add(remoteAddress); panel1.add(new JLabel("Remote Host Port:")); remotePort = new JTextField(); remotePort.setColumns(0); panel1.add(remotePort); c.add(panel1,BorderLayout.NORTH); //第二個panel JPanel panel2 = new JPanel(new FlowLayout(FlowLayout.LEADING)); panel2.setSize(400,200); panel2.add(new JLabel("收到的內容")); receiveText = new JTextArea(15,30); receiveText.setEditable(false); receiveText.setAutoscrolls(true); JScrollPane jsp = new JScrollPane(receiveText); panel2.add(jsp); //第三個panel // JPanel panel3 = new JPanel(new GridLayout(2, 1)); panel2.add(new JLabel("請輸入發送的內容")); sendText = new JTextField(30); sendText.setAutoscrolls(true); panel2.add(sendText); c.add(panel2,BorderLayout.CENTER); //c.add(panel3); //第四個panel JPanel panel4 = new JPanel(new GridLayout(1, 0)); panel4.setSize(400,20); receiveButton = new JButton("開始接受數據"); sendButton = new JButton("發送"); cancelButton = new JButton("取消"); panel4.add(receiveButton); panel4.add(cancelButton); panel4.add(sendButton); c.add(panel4,BorderLayout.SOUTH); //四個面板內容設置完畢 setVisible(true); setResizable(false); setDefaultCloseOperation(EXIT_ON_CLOSE); } private void ActionIni() { //鍵盤動作 this.addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub if(e.getKeyCode() == KeyEvent.VK_ENTER){ //發送文本 sendThread.senMessage(remoteAddress.getText(), Integer.parseInt(remotePort.getText()), sendText.getText()); receiveText.setText(receiveText.getText() + "\n" + "發送:" + sendText.getText()); } } @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub } }); //取消按鈕的動作 cancelButton.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub sendText.setText(""); } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); sendButton.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { //發送文本 sendThread.senMessage(remoteAddress.getText(), Integer.parseInt(remotePort.getText()), sendText.getText()); receiveText.setText(receiveText.getText() + "發送:" + sendText.getText()+ "\n"); } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); } private void ThreadIni() { // TODO Auto-generated method stub sendThread = new SendThread(0, this); receiveThread = new ReceiveThread(this); } //構造函數 public ChatWindow() { GUIini(); ActionIni(); ThreadIni(); } public void printError(String err){ System.out.println("Error occur:" + err); } //回調函數,用于接受從線程中返回的數據 public void setReceive(String receive){ receiveText.setText(receiveText.getText() + "收到:" + receive+ "\n" ); } //當接受數據的線程開始工作以后,就調用該回調函數,設置當前聊天窗口使用的端口是哪個 public void setLocalPort(int localPortText){ localPort.setText(""+localPortText); } //放遠,這個是整個程序的起點 public static void main(String[] args) { // TODO Auto-generated method stub new ChatWindow(); } }
如何使用
將工程導入到Eclipse中以后,運行兩次,因為這個程序不屬于單例模式,所以可以出現兩個窗口,在遠程端口,遠程地址那個輸入框中分別輸入另一個窗口中顯示的IP和端口號,接下來在輸入框中輸入要發送的文字就可以了
源碼下載:java實現簡單聊天軟件
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。