Chat usando socket

2 respostas
S

Então galera, eu to desenvolvendo um chatzinho para aprender a usar thread e socket, eu ja tenho 3 classes uma com o chat pronto, as outras 2 são as classes de cliente e servidor, o problema é que nao tenho uma idéia inicial de como fazer o "mix" com as classes de socket e a interface grafica abaixo:

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.SystemColor;
import javax.swing.JScrollPane;
import java.awt.Font;


public class Chat {

	private JFrame frame;
	private final JTextPane textPane = new JTextPane();
	private final JTextField textField = new JTextField();
	private final JScrollPane scrollPane = new JScrollPane();
	/**
	 * Launch the application.
	 */
	static String apelido ="";
	
	public static void main(String[] args) {
		
		//SHOWINPUTDIALOG AQUI, GRAVANDO O APELIDO DIGITADO EM UMA VARIAVEL APELIDO
		apelido = JOptionPane.showInputDialog(null, "Informe um apelido:", "Login", JOptionPane.QUESTION_MESSAGE);
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Chat window = new Chat();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Chat() {
		
		initialize();
	}
	

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.getContentPane().setBackground(SystemColor.inactiveCaptionText);
		frame.setBounds(100, 100, 494, 365);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		scrollPane.setBounds(10, 31, 458, 246);
		
		frame.getContentPane().add(scrollPane);
		textPane.setFont(new Font("Arial", Font.PLAIN, 13));
		scrollPane.setViewportView(textPane);
		
		textPane.setBackground(SystemColor.inactiveCaption);
		textPane.setEditable(false);
		
		
		textField.setBounds(10, 288, 321, 28);
		frame.getContentPane().add(textField);
		textField.setColumns(10);
		textField.addActionListener(new InsereTexto());
		
		JButton btnEnviar = new JButton("Enviar");
		btnEnviar.setBackground(SystemColor.inactiveCaptionText);
		btnEnviar.addActionListener(new InsereTexto());
		btnEnviar.setBounds(348, 287, 110, 30);
		frame.getContentPane().add(btnEnviar);
	}
	
      class InsereTexto implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			textPane.setText(textPane.getText() + "\n" + apelido + " : "+ textField.getText());   
			textField.setText("");
		}
	}
}

alguem poderia me dar uma luz, mostrando os passos que eu devo segui ?
os codigos deixa que eu me lasco pesquisando depois, eu queria mesmo é saber implementar os sockets com o aplicativo :cry: :cry:

2 Respostas

S

alguem?

caio.ribeiro.pereira

Da uma olhada nessa apostila básica de Java da caelum, tem um capítulo sobre Threads que mostra na prática um mini-chat usando Sockets + Threads.

Criado 14 de novembro de 2011
Ultima resposta 22 de nov. de 2011
Respostas 2
Participantes 2