diff --git a/src/ChatClient.java b/src/ChatClient.java index 7dd1963..bd33147 100644 --- a/src/ChatClient.java +++ b/src/ChatClient.java @@ -1,5 +1,7 @@ import java.net.*; +import javax.swing.AbstractAction; +import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; @@ -7,6 +9,8 @@ import java.io.*; import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; @@ -17,6 +21,8 @@ public class ChatClient extends JFrame implements Runnable { protected TextField name; protected TextField input; protected Thread listener; + protected TextField introName; + protected JFrame nameScreen; public ChatClient(String title, InputStream i, OutputStream o) { super(title); @@ -35,22 +41,80 @@ public ChatClient(String title, InputStream i, OutputStream o) { add("North", name = new TextField()); name.setText("Enter Name"); pack(); + setLocationRelativeTo(null); setVisible(true); setSize(500, 500); input.requestFocus(); listener = new Thread(this); listener.start(); - - - addWindowListener(new WindowAdapter() - { - public void windowClosing(WindowEvent e) - { - try { - - ChatClient.this.o.writeUTF(name.getText() + " has left the chat! " ); + + nameScreen = new JFrame(); + introName = new TextField(); + introName.setEditable(true); + introName.addActionListener( new AbstractAction(){ + + @Override + public void actionPerformed(ActionEvent e) { + ChatClient.this.name.setText(ChatClient.this.introName.getText()); + try { + ChatClient.this.o.writeUTF(ChatClient.this.name.getText() + " has entered the chat! "); ChatClient.this.o.flush(); - Thread.sleep(100); + } catch (IOException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + ChatClient.this.nameScreen.dispose(); + } + + }); + introName.setText("Enter Name, Ya Dingus!"); + nameScreen.setLayout(new BorderLayout()); + nameScreen.add(introName); + nameScreen.setDefaultCloseOperation(DISPOSE_ON_CLOSE); + nameScreen.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + ChatClient.this.name.setText(ChatClient.this.introName.getText()); + try { + ChatClient.this.o.writeUTF(ChatClient.this.name.getText() + " has entered the chat! "); + ChatClient.this.o.flush(); + } catch (IOException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + } + }); + JButton b = new JButton(); + nameScreen.add("South", b); + b.setText("SetName"); + b.addActionListener(new ActionListener(){ + @Override + public void actionPerformed(ActionEvent arg0) { + ChatClient.this.name.setText(ChatClient.this.introName.getText()); + try { + ChatClient.this.o.writeUTF(ChatClient.this.name.getText() + " has entered the chat! "); + ChatClient.this.o.flush(); + } catch (IOException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + ChatClient.this.nameScreen.dispose(); + } + + }); + nameScreen.setLocationRelativeTo(null); + nameScreen.setVisible(true); + nameScreen.setSize(200, 100); + nameScreen.toFront(); + nameScreen.repaint(); + + addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + try { + + ChatClient.this.o.writeUTF(name.getText() + " has left the chat! "); + ChatClient.this.o.flush(); + Thread.sleep(100); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); @@ -58,13 +122,12 @@ public void windowClosing(WindowEvent e) // TODO Auto-generated catch block e1.printStackTrace(); } - - System.exit(0); - - - } + + System.exit(0); + + } }); - + } public void run() { diff --git a/src/ChatHandler.java b/src/ChatHandler.java index 59e6548..5e860de 100644 --- a/src/ChatHandler.java +++ b/src/ChatHandler.java @@ -24,7 +24,7 @@ public void run() { broadcast(msg); } } catch (IOException ex) { - System.out.print("Client has left or error has occured"); + System.out.println("Client has left or error has occured"); //ex.printStackTrace(); } finally { handlers.removeElement(this);