{"comment":{"url":"https://api.github.com/repos/ProjetoChernobyl/chat/comments/14535053","html_url":"https://github.com/ProjetoChernobyl/chat/commit/acf21f96bb9d11095f5719c4a1c3100332f52869#commitcomment-14535053","id":14535053,"user":{"login":"RaulMaia","id":11757260,"avatar_url":"https://avatars.githubusercontent.com/u/11757260?v=3","gravatar_id":"","url":"https://api.github.com/users/RaulMaia","html_url":"https://github.com/RaulMaia","followers_url":"https://api.github.com/users/RaulMaia/followers","following_url":"https://api.github.com/users/RaulMaia/following{/other_user}","gists_url":"https://api.github.com/users/RaulMaia/gists{/gist_id}","starred_url":"https://api.github.com/users/RaulMaia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RaulMaia/subscriptions","organizations_url":"https://api.github.com/users/RaulMaia/orgs","repos_url":"https://api.github.com/users/RaulMaia/repos","events_url":"https://api.github.com/users/RaulMaia/events{/privacy}","received_events_url":"https://api.github.com/users/RaulMaia/received_events","type":"User","site_admin":false},"position":null,"line":null,"path":"","commit_id":"acf21f96bb9d11095f5719c4a1c3100332f52869","created_at":"2015-11-20T19:29:13Z","updated_at":"2015-11-20T19:29:13Z","body":"package SalaXTI;\r\n\r\nimport java.awt.BorderLayout;\r\nimport java.awt.Container;\r\nimport java.awt.Font;\r\nimport java.awt.event.ActionEvent;\r\nimport java.awt.event.ActionListener;\r\nimport java.awt.event.KeyEvent;\r\nimport java.awt.event.KeyListener;\r\nimport java.io.IOException;\r\nimport java.io.PrintWriter;\r\nimport java.net.Socket;\r\nimport java.util.Scanner;\r\n\r\nimport javax.swing.JButton;\r\nimport javax.swing.JFrame;\r\nimport javax.swing.JPanel;\r\nimport javax.swing.JScrollPane;\r\nimport javax.swing.JTextArea;\r\nimport javax.swing.JTextField;\r\n\r\n@SuppressWarnings(\"serial\")\r\npublic class ChatCliente extends JFrame {\r\n\r\n\tJTextField textoParaEnviar;\r\n\tSocket socket;\r\n\tPrintWriter escritor;\r\n\tString nome;\r\n\tJTextArea textoREcebido;\r\n\tScanner leitor;\r\n\r\n\tprivate class EscutaServidor implements Runnable {\r\n\r\n\t\t@Override\r\n\t\tpublic void run() {\r\n\t\t\ttry {\r\n\t\t\t\tString texto;\r\n\t\t\t\twhile ((texto = leitor.nextLine()) != null) {\r\n\t\t\t\t\ttextoREcebido.append(texto + \"\\n\");\r\n\t\t\t\t}\r\n\t\t\t} catch (Exception e) {\r\n\t\t\t\t// TODO: handle exception\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tpublic ChatCliente(String nome) {\r\n\t\tsuper(nome);\r\n\r\n\t\tthis.nome = nome;\r\n\t\t\r\n\t\tFont fonte = new Font(\"Serif\", Font.PLAIN, 26);\r\n\r\n\t\ttextoParaEnviar = new JTextField();\r\n\t\ttextoParaEnviar.setFont(fonte);\r\n\t\ttextoParaEnviar.addKeyListener(new TeclaListener());\r\n\r\n\t\tJButton botao = new JButton(\"Enviar\");\r\n\t\tbotao.setFont(fonte);\r\n\t\tbotao.addActionListener(new EnviarListener());\r\n\r\n\t\tContainer envio = new JPanel();\r\n\t\tenvio.setLayout(new BorderLayout());\r\n\t\tenvio.add(BorderLayout.CENTER, textoParaEnviar);\r\n\t\tenvio.add(BorderLayout.EAST, botao);\r\n\r\n\t\ttextoREcebido = new JTextArea();\r\n\t\ttextoREcebido.setFont(fonte);\r\n\t\tJScrollPane scroll = new JScrollPane(textoREcebido);\r\n\r\n\t\tgetContentPane().add(BorderLayout.CENTER, scroll);\r\n\t\tgetContentPane().add(BorderLayout.SOUTH, envio);\r\n\t\tconfigurarRede();\r\n\r\n\t\tsetDefaultCloseOperation(EXIT_ON_CLOSE);\r\n\t\tsetSize(500, 500);\r\n\t\tsetVisible(true);\r\n\t}\r\n\r\n\tprivate class TeclaListener implements KeyListener {\r\n\r\n\t\t@Override\r\n\t\tpublic void keyPressed(KeyEvent e) {\r\n\t\t\tif(e.getKeyCode() == KeyEvent.VK_ENTER) {\r\n\t\t\t\tescritor.println(nome + \" : \" + textoParaEnviar.getText());\r\n\t\t\t\tescritor.flush();\r\n\t\t\t\ttextoParaEnviar.setText(\" \");\r\n\t\t\t\ttextoParaEnviar.requestFocus();\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\t@Override\r\n\t\tpublic void keyReleased(KeyEvent e) {\r\n\t\t\t// TODO Auto-generated method stub\t\t\t\r\n\t\t}\r\n\t\t\r\n\t\t@Override\r\n\t\tpublic void keyTyped(KeyEvent e) {\r\n\t\t\t// TODO Auto-generated method stub\t\t\r\n\t\t}\t\t\t\r\n\t}\r\n\t\r\n\tprivate class EnviarListener implements ActionListener {\r\n\r\n\t\t@Override\r\n\t\tpublic void actionPerformed(ActionEvent e) {\r\n\t\t\tescritor.println(nome + \" : \" + textoParaEnviar.getText());\r\n\t\t\tescritor.flush();\r\n\t\t\ttextoParaEnviar.setText(\" \");\r\n\t\t\ttextoParaEnviar.requestFocus();\r\n\t\t}\r\n\r\n\t}\r\n\r\n\tprivate void configurarRede() {\r\n\t\ttry {\r\n\t\t\tsocket = new Socket(\"127.0.0.1\", 5000);\r\n\t\t\tescritor = new PrintWriter(socket.getOutputStream());\r\n\t\t\tleitor = new Scanner(socket.getInputStream());\r\n\t\t\tnew Thread(new EscutaServidor()).start();\r\n\r\n\t\t} catch (IOException e) {\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\t}\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tnew ChatCliente(\"Raul\");\r\n//\t\tnew ChatCliente(\"Exemplo\");\r\n\r\n\t}\r\n\r\n}\r\n"}}