Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JFrame;
- import javax.swing.JButton;
- import javax.swing.JTextField;
- import java.awt.FlowLayout;
- import java.awt.BorderLayout;
- import javax.swing.JOptionPane;
- import java.awt.event.*;
- import javax.swing.*;
- import java.io.*;
- import java.net.*;
- public class Client //change main class name
- {
- static JFrame chatFrame = new JFrame("ChatNow Client"); // Create a frame
- static JTextArea JTextArea_group = new JTextArea("Here goes the conversation\n", 15, 80);
- static NetworkClientModule networkClientModule;
- static String ip = JOptionPane.showInputDialog("Please enter IP Address");
- static String clientHandle = JOptionPane.showInputDialog("Please choose your handle");
- public static void main(String[] args)
- {
- chatFrame.setSize(900, 400); // Set the frame size
- chatFrame.setLocationRelativeTo(null);
- chatFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- chatFrame.setVisible(true); // Display the frame
- FlowLayout layoutManager = new FlowLayout(0,10,5);
- chatFrame.setLayout(layoutManager);
- // JTextArea JTextArea_group = new JTextArea("Here goes the conversation", 15, 80);
- JTextArea_group.setLineWrap(true);
- JTextArea_group.setWrapStyleWord(true);
- JTextArea_group.setEditable(false);
- //chatFrame.add(JTextArea_group);
- /* here add scroll to JTextArea - actually JScrollpane takes JTextArea */
- JScrollPane scrollPane = new JScrollPane(JTextArea_group);
- chatFrame.add(scrollPane);
- JTextField_group JTF = new JTextField_group();
- ///////////////**********maybe needs to be back up (global)
- }
- }
- class JTextField_group
- {
- JTextField TextFieldChatInput = new JTextField(80);
- JTextField_group()
- {
- Client.chatFrame.add(TextFieldChatInput);
- TextFieldChatInput.setText("Does some text show up?");
- TextFieldChatInput.addActionListener(new messageTextInputListener());
- }
- class messageTextInputListener implements ActionListener
- {
- @Override
- public void actionPerformed(ActionEvent e)
- {
- /*
- JOptionPane.showMessageDialog(null, "The text you entered is: " + TextFieldChatInput.getText());
- ChatGUI.JTextArea_group.setText( TextFieldChatInput.getText() );
- */
- String IhaveTextOfChatInput = TextFieldChatInput.getText();
- Client.JTextArea_group.append(Client.clientHandle + ": " + "\n");
- Client.JTextArea_group.append(IhaveTextOfChatInput + "\n");
- TextFieldChatInput.setText("");
- if (NetworkClientModule.oServer != null)
- {
- NetworkClientModule.oServer.println(Client.clientHandle + ": ");
- NetworkClientModule.oServer.println(IhaveTextOfChatInput);
- NetworkClientModule.oServer.flush();
- }
- }
- }
- }
- class NetworkClientModule
- {
- Socket socket = null;
- static DataInputStream iServer;
- static PrintWriter oServer;
- NetworkClientModule()
- {
- while(socket == null)
- {
- try
- {
- waitA();
- socket = new Socket(Client.ip, 8000);
- iServer = new DataInputStream(socket.getInputStream());
- oServer = new PrintWriter(socket.getOutputStream());
- for(int start = 0; start < 1000 ;start++) //////////////////////////////while(true)
- {
- waitA();
- String textFromServer = iServer.readLine(); ////// maybe need if-statement // writeUTF readUTF
- if (textFromServer != null)
- Client.JTextArea_group.append(textFromServer + "\n");
- }
- }
- catch(IOException ex)
- {
- System.err.println(ex);
- }
- }
- }
- void waitA()
- {
- try
- {
- Thread.sleep(300);
- }
- catch(InterruptedException ex)
- {
- Thread.currentThread().interrupt();
- }
- }
- }
Add Comment
Please, Sign In to add comment