채팅 프로그램

Posted by PeEn
2019. 5. 31. 16:33 Programing/Java

채팅 프로그램

package kr.ac.jj.report;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class WhanChatClient extends JFrame {
	private String mNick;
	private JTextField txtMessage;

	JButton sendBtn;
	JTextArea textArea;

	private Socket mSocket; // 연결소켓
	private DataInputStream dis;
	private DataOutputStream dos;

	public WhanChatClient(String nick, String ip, int port) {
		this.mNick = nick;
		drawChatPanel();

		try {
			mSocket = new Socket(ip, port);
			if (mSocket != null) {
				Connection();
			}
		} catch (IOException e) {
			textArea.append("소켓 접속 에러!!\n");
		}
	}

	private void drawChatPanel() {
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(400, 600);
		this.setLayout(null);

		JPanel panChat = new JPanel();
		panChat.setLayout(null);
		panChat.setBounds(this.getBounds());

		textArea = MyConst.makeTextArea(60, 6, 10, 10, 365, 450);
		textArea.setEditable(false);
		JScrollPane js = new JScrollPane();
		js.setBounds(textArea.getBounds());
		js.setViewportView(textArea);
		panChat.add(js);

		txtMessage = MyConst.makeTextField(60, 10, textArea.getY() + textArea.getHeight() + 10, 260, 50);
		txtMessage.addKeyListener(new KeyListener() {

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				System.out.println(e.getKeyChar() + " " + e.getKeyCode());
				if (e.getKeyCode() == 10) {
					// sendMessage(txtMessage.getText());
					// txtMessage.setText("");
					// txtMessage.grabFocus();
					sendBtn.doClick();
				}
			}

			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub

			}
		});
		panChat.add(txtMessage);

		sendBtn = new JButton("전   송");
		sendBtn.setBounds(txtMessage.getX() + txtMessage.getWidth() + 10, textArea.getY() + textArea.getHeight() + 10,
				textArea.getWidth() - txtMessage.getWidth() - 10, 50);
		sendBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				sendMessage(txtMessage.getText());
				txtMessage.setText("");
				textArea.setCaretPosition(textArea.getDocument().getLength());
				txtMessage.grabFocus();
			}
		});
		panChat.add(sendBtn);
		this.add(panChat);
		this.setVisible(true);
	}

	public void Connection() {
		try {
			dis = new DataInputStream(mSocket.getInputStream());
			dos = new DataOutputStream(mSocket.getOutputStream());
		} catch (IOException e) {
			textArea.append("스트림 설정 에러!!\n");
		}

		sendMessage(mNick);
		Thread th = new Thread(new Runnable() {
			@Override
			public void run() {
				while (true) {
					try {
						String msg = dis.readUTF();
						textArea.append(msg + "\n");
						textArea.setCaretPosition(textArea.getDocument().getLength());
					} catch (IOException e) {
						textArea.append("메세지 수신 에러!!\n");
						try {
							dos.close();
							dis.close();
							mSocket.close();
							break;
						} catch (IOException e1) {
						}
					}
				}
			}
		});
		th.start();
	}

	public void sendMessage(String str) {
		try {
			dos.writeUTF(str);
		} catch (IOException e) {
			textArea.append("메세지 송신 에러!!\n");
		}
	}

	public static void main(String[] args) {
		new LoginFrame();
	}
}

class LoginFrame extends JFrame {
	private JTextField tfID;
	private JTextField tfIP;
	private JTextField tfPort;

	public LoginFrame() {
		drawLoginPanel();
	}

	private void drawLoginPanel() {
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(400, 600);
		this.setLayout(null);
		JPanel panLogin = new JPanel();
		panLogin.setLayout(null);
		panLogin.setBounds(this.getBounds());

		panLogin.add(MyConst.makeLabel("ID : ", 10, 10, 100, 25));
		tfID = MyConst.makeTextField(60, 110, 10, 260, 25);
		panLogin.add(tfID);

		panLogin.add(MyConst.makeLabel("Server IP : ", 10, 40, 100, 25));
		tfIP = MyConst.makeTextField(60, 110, 40, 260, 25);
		tfIP.setText("202.31.234.45");
		panLogin.add(tfIP);

		panLogin.add(MyConst.makeLabel("Port : ", 10, 70, 100, 25));
		tfPort = MyConst.makeTextField(60, 110, 70, 260, 25);
		tfPort.setText("7777");
		panLogin.add(tfPort);

		JButton btnStart = new JButton("접    속");
		btnStart.setBounds(10, 100, 360, 30);
		panLogin.add(btnStart);

		btnStart.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				LoginFrame.this.setVisible(false);

				String nick = tfID.getText();
				String ip = tfIP.getText();
				int port = Integer.parseInt(tfPort.getText());
				new WhanChatClient(nick, ip, port);
			}
		});

		this.add(panLogin);
		this.setVisible(true);
	}
}

레이아웃 생성 클레스

package kr.ac.jj.report;

import java.awt.GridLayout;

import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class MyConst {
	public static final String DRIVER = "org.gjt.mm.mysql.Driver"; 		//= "oracle.jdbc.driver.OracleDriver";
	public static final String URL = "jdbc:mysql://localhost:3306/test"; 	//= "jdbc:oracle:thin:@192.168.0.3:1521:ORCL";
	public static final String USER = "test"; // DB ID
	public static final String PASS = "test123"; // DB 占쎈솭占쎈뮞占쎌뜖占쎈굡
	
	public static JLabel makeLabel(String cap, int x, int y, int width, int height) {
		JLabel lbl = new JLabel(cap);
		lbl.setBounds(x, y, width, height);
		return lbl;
	}

	public static JTextField makeTextField(int size, int x, int y, int width, int height) {
		JTextField txt = new JTextField(size);
		txt.setBounds(x, y, width, height);
		return txt;
	}

	public static JTextArea makeTextArea(int cols, int rows, int x, int y, int width, int height) {
		JTextArea txt = new JTextArea(cols, rows);
		txt.setBounds(x, y, width, height);
		return txt;
	}

	public static JPanel makeRadioButton(String[] cap, int x, int y, int width, int height) {
		JRadioButton[] rdo = new JRadioButton[cap.length];
		ButtonGroup bg = new ButtonGroup();
		JPanel panel = new JPanel(new GridLayout(1, cap.length));
		panel.setBounds(x, y, width, height);
		for (int i = 0; i < cap.length; i++) {
			rdo[i] = new JRadioButton(cap[i], true);
			bg.add(rdo[i]);
			panel.add(rdo[i]);
		}

		return panel;
	}

	public static JComboBox<String> makeComboBox(String[] cap, int x, int y, int width, int height) {
		JComboBox<String> cmb = new JComboBox<String>(cap);
		cmb.setBounds(x, y, width, height);

		return cmb;
	}

	public static JList<String> makeList(String[] cap, int x, int y, int width, int height) {
		JList<String> lst = new JList<String>(cap);
		lst.setBounds(x, y, width, height);

		return lst;
	}

}