Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. package chat.client;
  2.  
  3. import chat.network.TCPConnection;
  4. import chat.network.TCPConnectionListener;
  5.  
  6. import javax.swing.*;
  7. import java.awt.*;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.io.IOException;
  11.  
  12. public class ClientWindow extends JFrame implements ActionListener, TCPConnectionListener {
  13.  
  14. private static final String IP_ADDR = "localhost";
  15. private static final int PORT = 8189;
  16. private static final int WIDTH = 600;
  17. private static final int HEIGHT = 400;
  18.  
  19. public static void main(String[] args){
  20. SwingUtilities.invokeLater(new Runnable() {
  21. @Override
  22. public void run() {
  23. new ClientWindow();
  24. }
  25. });
  26. }
  27.  
  28.  
  29. private final JTextArea log = new JTextArea();
  30. private final JTextField fieldNickname = new JTextField("name");
  31. private final JTextField fieldInput = new JTextField();
  32.  
  33. private TCPConnection connection;
  34.  
  35. private ClientWindow(){
  36. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  37. setSize(WIDTH, HEIGHT);
  38. setLocationRelativeTo(null);
  39. setAlwaysOnTop(true);
  40. log.setEditable(false);
  41. log.setLineWrap(true);
  42. add(log, BorderLayout.CENTER);
  43.  
  44. fieldInput.addActionListener(this);
  45. add(fieldInput, BorderLayout.SOUTH);
  46. add(fieldNickname, BorderLayout.NORTH);
  47.  
  48. setVisible(true);
  49. try{
  50. connection = new TCPConnection(this, IP_ADDR, PORT);
  51. } catch(IOException e){
  52. printMsg("Connection exception: " + e);
  53. }
  54.  
  55. }
  56.  
  57. @Override
  58. public void actionPerformed(ActionEvent e) {
  59. String msg = fieldInput.getText();
  60. if(msg.equals("")) return;
  61. fieldInput.setText(null);
  62. connection.sendString((fieldNickname.getText() + ": " + msg));
  63. }
  64.  
  65.  
  66. @Override
  67. public void onCOnnectionReady(TCPConnection tcpConnection) {
  68. printMsg("Connection ready...");
  69. }
  70.  
  71. @Override
  72. public void onRecieveString(TCPConnection tcpConnection, String value) {
  73. printMsg(value);
  74. }
  75.  
  76. @Override
  77. public void onDisconnect(TCPConnection tcpConnection) {
  78. printMsg("Connection close");
  79. }
  80.  
  81. @Override
  82. public void onException(TCPConnection tcpConnection, Exception e) {
  83. printMsg("Connection exception: " + e);
  84. }
  85.  
  86. private synchronized void printMsg(String msg){
  87. SwingUtilities.invokeLater(new Runnable() {
  88. @Override
  89. public void run() {
  90. log.append(msg + "\n");
  91. log.setCaretPosition(log.getDocument().getLength());
  92. }
  93. });
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement