Guest User

Untitled

a guest
Jul 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1.  
  2.  
  3. package csit890chatclient;
  4.  
  5. import java.net.*;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8. import javax.swing.*;
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. import java.util.*;
  12. import java.io.*;
  13.  
  14. public class CSITChatClient extends JFrame implements ActionListener {
  15.  
  16. private JTextArea messages;
  17. private JTextField message;
  18. private BufferedReader inChannel;
  19. private PrintWriter outChannel;
  20.  
  21. public CSITChatClient() {
  22.  
  23. setSize(600,400);
  24. setLocation(300,200);
  25. BorderLayout layout = new BorderLayout();
  26. Container contentPane = this.getContentPane();
  27. contentPane.setLayout(layout);
  28.  
  29. messages = new JTextArea();
  30. messages.setEditable(false);
  31. messages.setFocusable(false);
  32. contentPane.add(messages,BorderLayout.CENTER);
  33.  
  34. message = new JTextField();
  35. message.addActionListener(this);
  36. contentPane.add(message, BorderLayout.SOUTH);
  37.  
  38. Properties properties = new Properties();
  39.  
  40. try {
  41. properties.load(new FileInputStream("CSIT890Chat.properties"));
  42.  
  43. }catch (IOException ex){
  44. ex.printStackTrace();
  45.  
  46. }
  47.  
  48. String host = properties.getProperty("host");
  49. int port = Integer.parseInt(properties.getProperty("port"));
  50. String name = properties.getProperty("name");
  51.  
  52. Socket socket = null;
  53.  
  54. try {
  55. socket = new Socket(host, port);
  56. inChannel = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  57. outChannel = new PrintWriter(socket.getOutputStream(), true);
  58. outChannel.println(name);
  59. } catch (UnknownHostException ex) {
  60. Logger.getLogger(CSITChatClient.class.getName()).log(Level.SEVERE, null, ex);
  61. } catch (IOException ex) {
  62. Logger.getLogger(CSITChatClient.class.getName()).log(Level.SEVERE, null, ex);
  63. }
  64.  
  65.  
  66.  
  67.  
  68. }
  69.  
  70. public static void main(String[] args) {
  71. CSITChatClient client = new CSITChatClient();
  72. client.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  73. client.setVisible(true);
  74. }
  75.  
  76. public void actionPerformed(ActionEvent e) {
  77. String line = this.message.getText().trim();
  78. if (line.length() > 0){
  79. message.setText("");
  80. messages.setText(messages.getText() + '\n' + line);
  81. outChannel.println(line);
  82. }
  83. throw new UnsupportedOperationException("Not supported yet.");
  84. }
  85.  
  86. }
Add Comment
Please, Sign In to add comment