Advertisement
chrisversloot

Softwaresystemen ss.week7.chatbox

Mar 4th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.76 KB | None | 0 0
  1. package ss.week7.chatbox;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Container;
  5. import java.awt.FlowLayout;
  6. import java.awt.GridLayout;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.WindowAdapter;
  10. import java.awt.event.WindowEvent;
  11. import java.io.IOException;
  12. import java.net.InetAddress;
  13. import java.net.UnknownHostException;
  14.  
  15. import javax.swing.JButton;
  16. import javax.swing.JFrame;
  17. import javax.swing.JLabel;
  18. import javax.swing.JPanel;
  19. import javax.swing.JTextArea;
  20. import javax.swing.JTextField;
  21.  
  22. /**
  23.  * ServerGui. A GUI for the Server.
  24.  * @author  Theo Ruys
  25.  * @version 2005.02.21
  26.  */
  27. public class ClientGUI extends JFrame implements ActionListener, MessageUI {
  28.  
  29.     private JButton bConnect;
  30.     private JTextField tfPort;
  31.     private JTextField tfName;
  32.     private JTextField tfAddress;
  33.     private JTextArea taMessages;
  34.     private JTextField msgType;
  35.     private JButton msgSend;
  36.     private Client client;
  37.  
  38.     /** Constructs a ServerGUI object. */
  39.     public ClientGUI() {
  40.         super("ClientGUI");
  41.  
  42.         buildGUI();
  43.         setVisible(true);
  44.  
  45.         addWindowListener(new WindowAdapter() {
  46.             public void windowClosing(WindowEvent e) {
  47.                 e.getWindow().dispose();
  48.             }
  49.             public void windowClosed(WindowEvent e) {
  50.                 System.exit(0);
  51.             }
  52.         });
  53.     }
  54.  
  55.     /** builds the GUI. */
  56.     public void buildGUI() {
  57.         setSize(400, 400);
  58.  
  59.         // Panel p1 - Listen
  60.  
  61.         JPanel p1 = new JPanel(new FlowLayout());
  62.         JPanel pp = new JPanel(new GridLayout(3,2));
  63.  
  64.         JLabel lbAddress = new JLabel("Address: ");
  65.         tfAddress = new JTextField(getHostAddress(), 12);
  66.        
  67.         JLabel lbPort = new JLabel("Port:");
  68.         tfPort = new JTextField("2727", 5);
  69.        
  70.         JLabel lbName = new JLabel("Name:");
  71.         tfName = new JTextField("", 5);
  72.  
  73.         pp.add(lbAddress);
  74.         pp.add(tfAddress);
  75.         pp.add(lbName);
  76.         pp.add(tfName);
  77.         pp.add(lbPort);
  78.         pp.add(tfPort);
  79.  
  80.         bConnect = new JButton("Connect");
  81.         bConnect.addActionListener(this);
  82.        
  83.         JPanel sendMsg = new JPanel();
  84.         sendMsg.setLayout(new GridLayout(1,2));
  85.        
  86.         msgType = new JTextField("Bericht");
  87.         msgType.setEnabled(false);
  88.         msgSend = new JButton("Stuur");
  89.         msgSend.addActionListener(this);
  90.         msgSend.setEnabled(false);
  91.        
  92.         sendMsg.add(msgType);
  93.         sendMsg.add(msgSend);
  94.        
  95.  
  96.         p1.add(pp, BorderLayout.WEST);
  97.         p1.add(bConnect, BorderLayout.EAST);
  98.  
  99.         // Panel p2 - Messages
  100.  
  101.         JPanel p2 = new JPanel();
  102.         p2.setLayout(new BorderLayout());
  103.  
  104.         JLabel lbMessages = new JLabel("Messages:");
  105.         taMessages = new JTextArea("", 15, 50);
  106.         taMessages.setEditable(false);
  107.         p2.add(lbMessages);
  108.         p2.add(taMessages, BorderLayout.SOUTH);
  109.  
  110.         Container cc = getContentPane();
  111.         cc.setLayout(new FlowLayout());
  112.         cc.add(p1);
  113.         cc.add(p2);
  114.         cc.add(sendMsg);
  115.        
  116.     }
  117.  
  118.     /** returns the Internetadress of this computer */
  119.     private String getHostAddress() {
  120.         try {
  121.             InetAddress iaddr = InetAddress.getLocalHost();
  122.             return iaddr.getHostAddress();
  123.         } catch (UnknownHostException e) {
  124.             return "?unknown?";
  125.         }
  126.     }
  127.  
  128.     /**
  129.      * listener for the "Start Listening" button
  130.      */
  131.     public void actionPerformed(ActionEvent ev) {
  132.         Object src = ev.getSource();
  133.         if (src == bConnect) {
  134.             startListening();
  135.         }
  136.         if (src == msgSend) {
  137.             //addMessage(msgType.getText(),client.getClientName());
  138.             client.sendMessage(msgType.getText());
  139.         }
  140.     }
  141.  
  142.     /**
  143.      * Construct a Server-object, which is waiting for clients. The port field and button should be disabled
  144.      */
  145.     private void startListening() {
  146.         int port = 0;
  147.         int max = 0;
  148.  
  149.         try {
  150.             port = Integer.parseInt(tfPort.getText());
  151.         } catch (NumberFormatException e) {
  152.             addMessage("ERROR: not a valid portnumber!");
  153.             return;
  154.         }
  155.        
  156.         String name = tfName.getText();
  157.         String hoststr = tfAddress.getText();
  158.         InetAddress host;
  159.         try {
  160.             host = InetAddress.getByName(hoststr);
  161.            
  162.             tfPort.setEditable(false);
  163.             bConnect.setEnabled(false);
  164.             tfName.setEditable(false);
  165.             tfAddress.setEditable(false);
  166.  
  167.             try {
  168.                 client = new Client(name, host, port, this);
  169.             } catch (IOException e) {
  170.                 // TODO Auto-generated catch block
  171.                 e.printStackTrace();
  172.             }
  173.             client.start();
  174.            
  175.         } catch (UnknownHostException e) {
  176.             // TODO Auto-generated catch block
  177.             e.printStackTrace();
  178.         }
  179.         msgSend.setEnabled(true);
  180.         msgType.setEditable(true);
  181.         msgType.setEnabled(true);
  182.         addMessage("User " + name + " joined on port " + port + "...");
  183.         client.sendMessage(name + "\n");
  184.     }
  185.  
  186.     /** add a message to the textarea  */
  187.     public void addMessage(String msg) {
  188.         taMessages.append(msg + "\n");
  189.     }
  190.    
  191.     public void addMessage(String msg, String clientName) {
  192.         taMessages.append("<"+clientName+"> " + msg + "\n");
  193.     }
  194.  
  195.     /** Start a ServerGUI application */
  196.     public static void main(String[] args) {
  197.         ClientGUI gui = new ClientGUI();
  198.     }
  199.  
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement