FineNerds

Client.java

Jul 17th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. package org.finenerds.chat.client;
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5. import javax.swing.*;
  6.  
  7. import java.awt.*;
  8. import java.awt.event.*;
  9.  
  10. public final class Client extends JFrame implements ActionListener{
  11.  
  12. static String username;
  13. PrintWriter pw;
  14. static BufferedReader br;
  15. static JTextArea cm;
  16. static JTextField cip;
  17. Socket chatUsers;
  18.  
  19. public Client(String cname, String servername) throws IOException
  20. {
  21. super(cname);
  22. this.username = cname;
  23. chatUsers = new Socket(servername,80);
  24. br = new BufferedReader(new InputStreamReader(chatUsers.getInputStream()));
  25. pw = new PrintWriter(chatUsers.getOutputStream(),true);
  26. pw.println(cname);
  27. createInterface();
  28. new MessageThread().start();
  29. }
  30.  
  31. public static void createInterface()
  32. {
  33. JFrame Frame = new JFrame();
  34. JLabel send = new JLabel("Send");
  35. cm = new JTextArea();
  36. cm.setRows(40);
  37. cm.setColumns(70);
  38. cip = new JTextField(50);
  39. JScrollPane scroll = new JScrollPane(cm, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  40. Frame.add(scroll, "Center");
  41. JPanel bp = new JPanel(new FlowLayout());
  42. bp.add(cip);
  43. bp.add(send);
  44. bp.setBackground(new Color(240, 240, 240));
  45. Frame.add(bp, "North");
  46. send.addMouseListener(this);
  47. bp.setSize(200, 300);
  48. Frame.setVisible(true);
  49. Frame.setTitle("FN - Chat || Logged in as " + username + ". Server: " + cip);
  50. Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  51. Frame.pack();
  52. }
  53.  
  54. public void actionPerformed(ActionEvent evt)
  55. {
  56.  
  57. }
  58.  
  59. public static void main(String args[])
  60. {
  61. String setUserName = JOptionPane.showInputDialog(null, "Username: ", JOptionPane.PLAIN_MESSAGE);
  62. String serverName = JOptionPane.showInputDialog(null, "Server: ", JOptionPane.PLAIN_MESSAGE);
  63.  
  64. try
  65. {
  66. new Client(setUserName, serverName);
  67. }
  68. catch(Exception e)
  69. {
  70. System.out.println(e.getMessage());
  71. }
  72. }
  73.  
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment