Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ServerChat extends Thread implements ActionListener {
- private Frame mainFrame;
- private Panel controlPanel;
- private Button sendButton;
- private JTextArea resultArea;
- private JTextField writeMessage;
- PersonList personList = new PersonList();
- String name ;
- private static int port = 9000;
- private static ServerSocket server;
- private static Socket conn;
- public ServerChat() throws UnknownHostException, IOException {
- // The programme recognize the user who's logged in and get his name
- name=personList.getPersonFromIndex(personList.getPersonLogged()).getName() +" "+personList.getPersonFromIndex(personList.getPersonLogged()).getSurname() ;
- mainFrame = new Frame("Chat - SERVER area");
- mainFrame.setSize(500, 500);
- mainFrame.setLayout(new GridLayout(1, 2));
- mainFrame.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent windowEvent) {
- System.exit(0);
- }
- });
- Border border = BorderFactory.createLineBorder(Color.BLACK);
- Dimension d = new Dimension();
- d.equals(mainFrame);
- resultArea = new JTextArea(20,40);
- JScrollPane scrollingArea = new JScrollPane(resultArea);
- JLabel lblMessage = new JLabel("MESSAGE: ");
- lblMessage.setForeground(Color.BLACK);
- writeMessage = new JTextField(40);
- writeMessage.setBackground(Color.white);
- writeMessage.setBorder(new LineBorder(Color.BLACK, 1));
- sendButton = new Button();
- sendButton.setLabel("SEND");
- sendButton.setFont(new Font("Tahoma", 1, 13));
- controlPanel = new Panel();
- controlPanel.setLayout(new FlowLayout());
- controlPanel.add(scrollingArea, border);
- controlPanel.add(lblMessage);
- controlPanel.add(writeMessage);
- controlPanel.add(sendButton);
- mainFrame.add(controlPanel);
- mainFrame.setResizable(false);
- mainFrame.setVisible(true);
- sendButton.setActionCommand("SEND");
- sendButton.addActionListener(this);
- server = new ServerSocket(port, 1, InetAddress.getLocalHost());
- conn = server.accept();
- while (true) {
- try {
- DataInputStream dis = new DataInputStream(conn.getInputStream());
- String string = dis.readUTF();
- if (string != " " && string != null && !string.isEmpty()) {
- resultArea.append(string + "n");
- }
- } catch (Exception e1) {
- try {
- Thread.sleep(3000);
- System.exit(0);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- public static void main(String[] args) throws IOException {
- // TODO code application logic here
- ServerChat swingControlDemo = new ServerChat();
- }
- public void actionPerformed(ActionEvent e) {
- String comand = sendButton.getActionCommand();
- if ((comand == "SEND") && (writeMessage.getText() != " ")) {
- try {
- DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
- dos.writeUTF(name + " : "+writeMessage.getText());
- resultArea.append(name +" : "+ writeMessage.getText());
- } catch (Exception e1) {
- writeMessage.setText("There is an error in connection.");
- JOptionPane.showMessageDialog(null, "There is error in something...");
- try {
- Thread.sleep(3000);
- System.exit(0);
- } catch (InterruptedException e2) {
- e2.printStackTrace();
- }
- }
- writeMessage.setText("");
- }
- }
- public class ClientChat implements ActionListener {
- private Frame mainFrame;
- private Panel controlPanel;
- private Button sendButton;
- private JTextArea resultArea;
- private JTextField writeMessage;
- private static int port = 9000;
- private String sName = "localhost";
- private Socket conn;
- PersonList personList = new PersonList();
- String name ;
- public ClientChat() throws IOException {
- name= personList.getPersonFromIndex(personList.getPersonLogged()).getName() +" "+personList.getPersonFromIndex(personList.getPersonLogged()).getSurname() ;
- mainFrame = new Frame("Chat - CLIENT area");
- mainFrame.setSize(500, 500);
- mainFrame.setLayout(new GridLayout(1, 2));
- mainFrame.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent windowEvent) {
- System.exit(0);
- }
- });
- Border border = BorderFactory.createLineBorder(Color.BLACK);
- resultArea = new JTextArea(20,40);
- JScrollPane scrollingArea = new JScrollPane(resultArea);
- JLabel lblMessage = new JLabel("MESSAGE: ");
- lblMessage.setForeground(Color.BLACK);
- writeMessage = new JTextField(37);
- writeMessage.setBackground(Color.LIGHT_GRAY);
- writeMessage.setBorder(new LineBorder(Color.BLACK, 1));
- sendButton = new Button();
- sendButton.setLabel("SEND");
- sendButton.setFont(new Font("Tahoma", 1, 13));
- controlPanel = new Panel();
- controlPanel.setLayout(new FlowLayout());
- controlPanel.add(scrollingArea, border);
- controlPanel.add(lblMessage);
- controlPanel.add(writeMessage);
- controlPanel.add(sendButton);
- mainFrame.add(controlPanel);
- mainFrame.setResizable(false);
- mainFrame.setBounds(620, 1, 600, 600);
- mainFrame.setVisible(true);
- sendButton.setActionCommand("SEND");
- sendButton.addActionListener(this);
- try {
- conn = new Socket(InetAddress.getLocalHost(), port);
- } catch (Exception e) {
- resultArea.setText("The server is not up.");
- //JOptionPane.showMessageDialog(null, "There is error in something...");
- }
- while (true) {
- try {
- DataInputStream dis = new DataInputStream(conn.getInputStream());
- String string = dis.readUTF(); //it will read the data from dis
- if (string != " " && string != null && !string.isEmpty()) {
- resultArea.append(string + "n");
- }
- } catch (Exception e1) {
- }
- }
- }
- public static void main(String[] args) throws IOException {
- // TODO code application logic here
- char a;
- Scanner in = new Scanner(System.in);
- //a = in.n
- ClientChat swingControlDemo = new ClientChat();
- }
- public void actionPerformed(ActionEvent e) {
- String comand = sendButton.getActionCommand();
- if ((comand == "SEND") && (writeMessage.getText() != "")) {
- try {
- DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
- dos.writeUTF(name + " : "+writeMessage.getText());
- resultArea.append(name +" : "+ writeMessage.getText());
- } catch (Exception e1) {
- try {
- Thread.sleep(3000);
- System.exit(0);
- } catch (InterruptedException e2) {
- e2.printStackTrace();
- }
- }
- writeMessage.setText("");
- }
- }
- private void chatRoomButtonActionPerformed(java.awt.event.ActionEvent evt) {
- try {
- Thread r = new Thread(new ServerChat());
- r.start();
- } catch (IOException ex) {
- Logger.getLogger(StaffMainPage.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement