Guest User

Chat Client

a guest
Aug 20th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.31 KB | None | 0 0
  1. import javax.swing.JFrame;
  2. import javax.swing.JButton;
  3. import javax.swing.JTextField;
  4. import java.awt.FlowLayout;
  5. import java.awt.BorderLayout;
  6. import javax.swing.JOptionPane;
  7. import java.awt.event.*;
  8. import javax.swing.*;
  9.  
  10. import java.io.*;
  11. import java.net.*;
  12.  
  13. public class Client //change main class name
  14. {
  15.     static JFrame chatFrame = new JFrame("ChatNow Client"); // Create a frame
  16.     static JTextArea JTextArea_group = new JTextArea("Here goes the conversation\n", 15, 80);
  17.     static NetworkClientModule networkClientModule;
  18.     static String ip = JOptionPane.showInputDialog("Please enter IP Address");
  19.     static String clientHandle = JOptionPane.showInputDialog("Please choose your handle");
  20.    
  21.     public static void main(String[] args)
  22.     {
  23.  
  24.         chatFrame.setSize(900, 400); // Set the frame size
  25.         chatFrame.setLocationRelativeTo(null);
  26.         chatFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27.         chatFrame.setVisible(true); // Display the frame
  28.  
  29.        
  30.         FlowLayout layoutManager = new FlowLayout(0,10,5);
  31.         chatFrame.setLayout(layoutManager);
  32.  
  33.  
  34. //        JTextArea JTextArea_group = new JTextArea("Here goes the conversation", 15, 80);
  35.         JTextArea_group.setLineWrap(true);
  36.         JTextArea_group.setWrapStyleWord(true);
  37.         JTextArea_group.setEditable(false);
  38.  
  39.         //chatFrame.add(JTextArea_group);
  40.  
  41.  
  42.         /* here add scroll to JTextArea - actually JScrollpane takes JTextArea */        
  43.         JScrollPane scrollPane = new JScrollPane(JTextArea_group);
  44.         chatFrame.add(scrollPane);
  45.  
  46.              
  47.         JTextField_group JTF = new JTextField_group();
  48.  
  49.                                    ///////////////**********maybe needs to be back up (global)
  50.  
  51.     }
  52.  }
  53.  
  54. class JTextField_group  
  55. {
  56.     JTextField TextFieldChatInput = new JTextField(80);
  57.  
  58.     JTextField_group()
  59.     {    
  60.         Client.chatFrame.add(TextFieldChatInput);                  
  61.         TextFieldChatInput.setText("Does some text show up?");    
  62.  
  63.         TextFieldChatInput.addActionListener(new messageTextInputListener());
  64.     }
  65.  
  66.     class messageTextInputListener implements ActionListener
  67.     {
  68.         @Override
  69.         public void actionPerformed(ActionEvent e)
  70.         {
  71.             /*
  72.             JOptionPane.showMessageDialog(null, "The text you entered is: " + TextFieldChatInput.getText());
  73.             ChatGUI.JTextArea_group.setText( TextFieldChatInput.getText() );
  74.             */
  75.  
  76.             String IhaveTextOfChatInput = TextFieldChatInput.getText();
  77.             Client.JTextArea_group.append(Client.clientHandle + ": "  + "\n");
  78.             Client.JTextArea_group.append(IhaveTextOfChatInput + "\n");
  79.             TextFieldChatInput.setText("");
  80.  
  81.             if (NetworkClientModule.oServer != null)
  82.             {
  83.                 NetworkClientModule.oServer.println(Client.clientHandle + ": ");
  84.                 NetworkClientModule.oServer.println(IhaveTextOfChatInput);
  85.                 NetworkClientModule.oServer.flush();
  86.             }
  87.  
  88.         }
  89.     }
  90. }
  91.  
  92. class NetworkClientModule
  93. {
  94.     Socket socket = null;
  95.  
  96.     static DataInputStream iServer;
  97.     static PrintWriter oServer;
  98.  
  99.     NetworkClientModule()
  100.     {
  101.         while(socket == null)
  102.                                     {
  103.         try
  104.         {  
  105.             waitA();                  
  106.             socket = new Socket(Client.ip, 8000);                
  107.  
  108.             iServer = new DataInputStream(socket.getInputStream());
  109.             oServer = new PrintWriter(socket.getOutputStream());
  110.             for(int start = 0; start < 1000 ;start++)            //////////////////////////////while(true)
  111.             {
  112.                 waitA();
  113.  
  114.                 String textFromServer = iServer.readLine();         ////// maybe need if-statement   // writeUTF     readUTF
  115.    
  116.                 if (textFromServer != null)
  117.                     Client.JTextArea_group.append(textFromServer + "\n");
  118.  
  119.             }
  120.         }
  121.  
  122.         catch(IOException ex)
  123.         {
  124.             System.err.println(ex);
  125.         }
  126.                                     }
  127.  
  128.     }
  129.  
  130.     void waitA()
  131.     {
  132.         try
  133.         {
  134.             Thread.sleep(300);
  135.         }
  136.  
  137.         catch(InterruptedException ex)
  138.         {
  139.             Thread.currentThread().interrupt();
  140.         }
  141.     }
  142. }
Add Comment
Please, Sign In to add comment