Java 채팅 소스 예제 #1

Programming/JAVA,JSP 2016. 2. 4. 14:28 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Client {
	public static void main(String[] args) {
		JTextField textField;
		JTextArea textArea;
		PrintWriter out;
		try {
			Socket s = new Socket("118.46.60.67", 5555);
			out = new PrintWriter(s.getOutputStream(), true);
			JFrame f = new JFrame("채팅");
			f.setSize(600, 400);
			f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
			textArea = new JTextArea();
			textField = new JTextField(10);
			textField.addActionListener(
					new ActionListener() {
						public void actionPerformed(ActionEvent e) {
						out.println(textField.getText());
						}
					}
			);
			f.add(textField, BorderLayout.SOUTH);
			f.add(textArea, BorderLayout.CENTER);
			f.setVisible(true);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}

'Programming > JAVA,JSP' 카테고리의 다른 글

ImageFrameTest  (0) 2016.02.05
Java 채팅 프로그램  (0) 2016.02.04
Java CardLayout Test  (0) 2016.01.26
정올 알고리즘 2247 도서관 문제  (0) 2015.11.26
Java Server/Client Code  (2) 2015.11.12