Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package csit890chatclient;
- import java.net.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.*;
- import java.io.*;
- public class CSITChatClient extends JFrame implements ActionListener {
- private JTextArea messages;
- private JTextField message;
- private BufferedReader inChannel;
- private PrintWriter outChannel;
- public CSITChatClient() {
- setSize(600,400);
- setLocation(300,200);
- BorderLayout layout = new BorderLayout();
- Container contentPane = this.getContentPane();
- contentPane.setLayout(layout);
- messages = new JTextArea();
- messages.setEditable(false);
- messages.setFocusable(false);
- contentPane.add(messages,BorderLayout.CENTER);
- message = new JTextField();
- message.addActionListener(this);
- contentPane.add(message, BorderLayout.SOUTH);
- Properties properties = new Properties();
- try {
- properties.load(new FileInputStream("CSIT890Chat.properties"));
- }catch (IOException ex){
- ex.printStackTrace();
- }
- String host = properties.getProperty("host");
- int port = Integer.parseInt(properties.getProperty("port"));
- String name = properties.getProperty("name");
- Socket socket = null;
- try {
- socket = new Socket(host, port);
- inChannel = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- outChannel = new PrintWriter(socket.getOutputStream(), true);
- outChannel.println(name);
- } catch (UnknownHostException ex) {
- Logger.getLogger(CSITChatClient.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IOException ex) {
- Logger.getLogger(CSITChatClient.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public static void main(String[] args) {
- CSITChatClient client = new CSITChatClient();
- client.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- client.setVisible(true);
- }
- public void actionPerformed(ActionEvent e) {
- String line = this.message.getText().trim();
- if (line.length() > 0){
- message.setText("");
- messages.setText(messages.getText() + '\n' + line);
- outChannel.println(line);
- }
- throw new UnsupportedOperationException("Not supported yet.");
- }
- }
Add Comment
Please, Sign In to add comment