Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.65 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.GridLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.KeyEvent;
  8. import java.awt.event.*;
  9. import java.io.DataInputStream;
  10. import java.io.DataOutputStream;
  11. import java.io.IOException;
  12. import java.net.Socket;
  13. import java.net.UnknownHostException;
  14.  
  15. import javax.swing.Box;
  16. import javax.swing.JButton;
  17. import javax.swing.JFrame;
  18. import javax.swing.JLabel;
  19. import javax.swing.JPanel;
  20. import javax.swing.JScrollPane;
  21. import javax.swing.JTextArea;
  22. import javax.swing.JTextField;
  23.  
  24.  
  25. public class ChatClientGUI extends JFrame implements ActionListener,KeyListener, Runnable{
  26.  
  27. private Socket socket = null;
  28. private final String serverName = "localhost"; //"localhost"//or your friend's ip address
  29. private final int serverPortNumber = 8070; //needs to ma
  30.  
  31. private DataInputStream strIn = null;
  32. private DataOutputStream strOut = null;
  33.  
  34. private ChatClientThread client = null;
  35. private boolean done = true;//until connected you are "done"
  36. private String line = "";
  37.  
  38. private JTextArea displayText = new JTextArea(16,50);
  39. private JTextField input = new JTextField(1);
  40. private JButton btnConnect = new JButton("Connect");
  41. private JButton btnSend = new JButton("Send");
  42. private JButton btnQuit = new JButton("Bye");
  43. private JButton btnPrivate = new JButton("Private");
  44. private JPanel mainJP = new JPanel();//everything goes in here
  45. private JPanel displayJP = new JPanel();//textarea.. plus whatever
  46. private JPanel btnsJP = new JPanel();//put this on the bottom
  47. //private JPanel east = new JPanel();
  48. private JPanel west = new JPanel();
  49. private JPanel north = new JPanel();
  50. EastJP east = new EastJP();
  51. public ChatClientGUI(){
  52.  
  53. mainJP.setLayout(new BorderLayout());
  54.  
  55.  
  56. displayJP.setLayout(new GridLayout(2,1));
  57. displayJP.add(displayText); //added textarea to jpanel
  58. displayJP.add(input);//added input below textarea to jpanel
  59.  
  60. btnsJP.setLayout(new GridLayout(1,4));
  61.  
  62. btnPrivate.addActionListener(this);
  63. btnConnect.addActionListener(this);
  64. btnSend.addActionListener(this);
  65. btnQuit.addActionListener(this);
  66.  
  67.  
  68. Box box = Box.createHorizontalBox();
  69. box.add(btnPrivate);box.add(btnConnect);box.add(btnSend);box.add(btnQuit);
  70. btnsJP.add(box);
  71. /*btnsJP.add(btnPrivate);
  72. btnsJP.add(btnConnect);
  73. btnsJP.add(btnSend);
  74. btnsJP.add(btnQuit); */
  75. east.setBackground(Color.gray);
  76. west.setBackground(Color.gray);
  77. north.setBackground(Color.gray);
  78. east.setBackground(Color.gray);
  79. east.setBackground(Color.gray);
  80. btnsJP.setBackground(Color.gray);
  81.  
  82. mainJP.add(displayJP, BorderLayout.CENTER);//add to center
  83. mainJP.add(btnsJP, BorderLayout.SOUTH);//add to bottom
  84. mainJP.add(east, BorderLayout.EAST);
  85. mainJP.add(west, BorderLayout.WEST);
  86. mainJP.add(north, BorderLayout.NORTH);
  87.  
  88. input.setEditable(false);
  89. displayText.setEditable(false);
  90. //input.setPreferredSize(new Dimension(500,500));
  91.  
  92. add(mainJP);
  93. btnQuit.setEnabled(false);
  94. btnPrivate.setEnabled(false);
  95. btnSend.setEnabled(false);
  96. btnPrivate.setEnabled(false);
  97. addKeyListener(this);
  98.  
  99. }
  100.  
  101. @Override
  102. public void run() {
  103. while(!done){
  104. try {
  105. line = strIn.readUTF();
  106. //displayMessage(line);
  107. displayMsg(line);
  108. } catch (IOException e) {
  109. // TODO Auto-generated catch block
  110. e.printStackTrace();
  111. }
  112.  
  113. }
  114.  
  115. }
  116. private void displayMsg(String msg){
  117. displayText.append(msg +"\n");
  118. }
  119.  
  120. @Override
  121. public void actionPerformed(ActionEvent e) {
  122. //do something when connect
  123. //connect(serverName, serverPortNumber);
  124. if(e.getSource() == btnConnect)
  125. connect(serverName, serverPortNumber);
  126.  
  127. //do something when send
  128. if(e.getSource()== btnSend)
  129. send();
  130.  
  131. if(e.getSource() == btnQuit)
  132. disconnect();
  133. if(e.getSource() == btnPrivate)
  134. privatemsg();
  135.  
  136. //send()
  137. //do something when private
  138. //send... private...
  139. //do something when quit
  140. //disconnect();
  141.  
  142. }
  143. public void connect(String serverName, int portNum){
  144. try {
  145. done=false;
  146. socket = new Socket(serverName, portNum);
  147. System.out.println("Client just got connected");
  148. //display("hooray that we got connected");...
  149. open();
  150. input.setText("Connected");
  151. send();
  152. btnSend.enable();
  153. btnConnect.enable();
  154. btnQuit.enable();
  155. btnPrivate.enable();
  156.  
  157.  
  158. btnSend.setEnabled(true);
  159. btnConnect.setEnabled(false);
  160. btnPrivate.setEnabled(true);
  161. btnQuit.setEnabled(true);
  162. input.setEditable(true);
  163. //enable our buttons....
  164. //either.... use .setEnabled... and have separate buttons and use
  165. //getSource from within actionPerformed
  166. //or....... use .setText ... and have 1 button whose face changes and use
  167. //getActionCommand from within actionPerformed
  168. //or... still use getSource... and compare against the boolean done
  169.  
  170. } catch (UnknownHostException e) {
  171. e.printStackTrace();
  172. done=true;
  173. //displayMessage("OOPSY"+ e.getMessage()+"... or something nicer");
  174.  
  175. } catch (IOException e) {
  176. // TODO Auto-generated catch block
  177. e.printStackTrace();
  178. done=true;
  179. //displayMessage("OOPSY"+ e.getMessage()+"... or something nicer");
  180. }
  181. finally{
  182. //displayMessage("the connection attempt completed... success/failure..");
  183. //onto the next task.. blah blha
  184. }
  185.  
  186. }
  187.  
  188.  
  189.  
  190. public void send(){
  191. try {
  192. //get the message from the input textfield getText();
  193. //...store it in a String
  194. // make sure to
  195. input.requestFocus();
  196.  
  197. String msgs = input.getText();
  198. strOut.writeUTF(msgs);
  199. strOut.flush();
  200. input.setText("");
  201. btnSend.setEnabled(true);
  202. btnConnect.setEnabled(false);
  203. btnPrivate.setEnabled(true);
  204. btnQuit.setEnabled(true);
  205. input.setEditable(true);
  206.  
  207. } catch (IOException e) {
  208. // TODO Auto-generated catch block
  209. e.printStackTrace();
  210. //displayMessage("BLAH");
  211. }
  212.  
  213. }
  214.  
  215. public void privatemsg(){
  216. try {
  217. //get the message from the input textfield getText();
  218. //...store it in a String
  219. // make sure to
  220. input.requestFocus();
  221. String msgs = input.getText();
  222. strOut.writeUTF(msgs);
  223. strOut.flush();
  224. input.setText("");
  225. btnConnect.setEnabled(false);
  226. btnPrivate.setEnabled(true);
  227. btnSend.setEnabled(true);
  228. btnQuit.setEnabled(false);
  229. } catch (IOException e) {
  230. // TODO Auto-generated catch block
  231. e.printStackTrace();
  232.  
  233. }
  234. }
  235. public void disconnect(){
  236. done=true;
  237. input.setText("Left the chat");
  238. send();
  239. btnSend.setEnabled(false);
  240. btnConnect.setEnabled(true);
  241. btnPrivate.setEnabled(false);
  242. btnQuit.setEnabled(false);
  243. input.setEditable(false);
  244. ///either setEnabled(false/true)... for your buttons
  245. //or...setText("Connect")... for your buttons...
  246. //if ... stream... is !=null
  247. //stream.close();
  248. //do that for in... and for out... do that for the socket...
  249. }
  250. public void open(){
  251. try {
  252. strOut = new DataOutputStream(socket.getOutputStream());
  253. strIn = new DataInputStream(socket.getInputStream());
  254. new Thread(this).start();//to be able to listen in
  255.  
  256. } catch (IOException e) {
  257. // TODO Auto-generated catch block
  258. e.printStackTrace();
  259. }
  260. //strIn =...
  261. }
  262.  
  263. @Override
  264. public void keyPressed(KeyEvent e) {
  265. if (e.getKeyCode() == KeyEvent.VK_ENTER)
  266. System.out.println("texting");
  267. send();
  268.  
  269.  
  270. }
  271.  
  272. public static void main(String[] args) {
  273. javax.swing.SwingUtilities.invokeLater( new Runnable(){
  274. public void run() {
  275. ChatClientGUI chatclient = new ChatClientGUI();
  276. chatclient.pack();
  277. chatclient.setVisible(true);
  278. //chatclient.setSize(500,500);
  279. chatclient.setTitle("Chatmination");
  280.  
  281. }
  282.  
  283.  
  284. }
  285.  
  286. );
  287. }
  288.  
  289. @Override
  290. public void keyReleased(KeyEvent arg0) {
  291. // TODO Auto-generated method stub
  292.  
  293. }
  294.  
  295. @Override
  296. public void keyTyped(KeyEvent arg0) {
  297. // TODO Auto-generated method stub
  298.  
  299. }
  300.  
  301.  
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement