Talk.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import java.awt.BorderLayout;
18 import java.awt.FlowLayout;
19 import java.awt.event.ActionEvent;
20 import java.awt.event.ActionListener;
21
22 import javax.swing.*;
23 import java.io.*;
24
25 import java.net.Socket;
26
27 class AccionEnviar implements ActionListener{
28 private JTextField areaTexto;
29 private PrintStream salida;
30
31 public AccionEnviar(Socket s, JTextField at){
32 areaTexto = at;
33 try {
34 salida = new PrintStream(s.getOutputStream());
35 } catch (IOException e) {
36 e.printStackTrace();
37 }
38 }
39
40 public void actionPerformed(ActionEvent e){
41 salida.println(areaTexto.getText());
42 areaTexto.setText("");
43 }
44 }
45
46 public class Talk {
47 private Socket socket;
48 private String titulo;
49
50 public Talk(Socket s, String t){
51 socket = s;
52 titulo = t;
53 }
54
55 public void hablar(){
56 JFrame marco = new JFrame(titulo);
57 marco.setLayout(new BorderLayout());
58 JTextArea areaTexto = new JTextArea("");
59 areaTexto.setEditable(false);
60 marco.add(areaTexto, "Center");
61 JPanel panel = new JPanel(new FlowLayout());
62 marco.add(panel, "South");
63 JTextField campoTexto = new JTextField(30);
64 panel.add(campoTexto);
65 JButton botonEnviar = new JButton("Enviar");
66 AccionEnviar ae = new AccionEnviar(socket, campoTexto);
67 botonEnviar.addActionListener(ae);
68 panel.add(botonEnviar);
69 marco.setSize(600,800);
70 marco.setVisible(true);
71
72 BufferedReader entrada;
73 try {
74 entrada = new BufferedReader(new InputStreamReader(socket.getInputStream()));
75 String mensaje;
76 while( (mensaje = entrada.readLine()) != null){
77 areaTexto.setText(areaTexto.getText() + mensaje + "\n");
78 }
79 } catch (IOException e) {
80 e.printStackTrace();
81 }
82 }
83 }
CategoryJava | CategoryProgramacion