import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
class ChatClient extends Frame
{
private TextField tf1 = new TextField("", 50);
private TextArea ta;
private Button sendButton;
private Button quitButton;
private Button okButton;
private MenuItem exitMT, aboutMT;
private Choice choice;
private String user;
private AboutDialog dialog;
private Thread runner;
private Socket s;
private OutputStream os;
private BufferedReader br;
public ChatClient()
{
super("ChatClient");
runner = null;
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e) {System.exit(0); }});
addWindowListener(new WindowAdapter()
{public void windowActivated(WindowEvent e)
{
tf1.requestFocusInWindow();
}
});
setTitle("Chat Room");
Panel p = new Panel(new BorderLayout());
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new GridLayout(3,1));
MenuBar mb = new MenuBar ();
setMenuBar (mb);
Menu fileMenu = new Menu ("File");
mb.add(fileMenu);
Menu helpMenu = new Menu ("Help");
mb.add(helpMenu);
fileMenu.add (exitMT= new MenuItem ("Quit"));
helpMenu.add (aboutMT = new MenuItem ("About"));
exitMT.addActionListener(new ExitCommand());
aboutMT.addActionListener(new AboutCommand());
//creating main panel
add(p, BorderLayout.NORTH);
ta = new TextArea(10, 50);
add(ta, BorderLayout.CENTER);
ta.setSize(500,300);
ta.setEditable(false);
//adding buttons and choice box to the right side of the window
sendButton = new Button("Send");
Panel p3 = new Panel();
// sendButton.setSize(15,15);
p3.add(sendButton);
p2.add(p3);
sendButton.addActionListener(new CopyCommand());
quitButton = new Button("Quit");
Panel p4 = new Panel();
// quitButton.setSize(15,15);
p4.add(quitButton);
p2.add(p4);
quitButton.addActionListener(new ExitCommand());
Panel p5 = new Panel();
choice = new Choice();
choice.addItem("John");
choice.addItem("Jena");
choice.addItem("Colleen");
choice.addItem("Steve");
user=choice.getItem(0);
p5.add(choice);
choice.addItemListener(new CopyCommand());
p2.add(p5);
add(p2, BorderLayout.EAST);
//adding the textfield to the south end of the window
p1.add(tf1);
add(p1, BorderLayout.SOUTH);
tf1.addActionListener(new CopyCommand());
//connecting to the server
try
{
s = new Socket("10.14.81.145", 7968);
br = new BufferedReader(
new InputStreamReader(s.getInputStream()));
// os =s.getOutputStream();
}
catch(Exception e)
{
System.out.println(e);
System.out.println("exiting ...");
System.exit(0);
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
s.close();
br.close();
os.close();
}
}
class ReadMessages implements Runnable
{
public void run()
{
//something having to do with bufferedreader and thread. I think!
runner = new Thread(this);
runner.start();
}
}
//handling ActionEvent from buttons and menu items
class ExitCommand implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
tf1.setText("");
ta.setText("");
choice.select(0);
s.close();
br.close();
s.close();
System.exit(0);
}
}
class AboutCommand implements ActionListener
{
public void actionPerformed(ActionEvent e)
{ String arg = e.getActionCommand();
if(arg.equals("About"))
{ if (dialog == null) // first time
dialog = new AboutDialog(ChatClient.this);
dialog.show();
}
}
}
class AboutDialog extends Dialog
{
public AboutDialog(Frame parent)
{
super(parent, "About ChatClient", true);
Panel p6 = new Panel();
p6.add(new Label("Chat Client ver 1.0"));
p6.add(new Label("By Jennifer McAuliffe"));
add(p6, "Center");
Panel p7 = new Panel();
Button ok = new Button("Ok");
p7.add(ok);
add(p7, "South");
ok.addActionListener(new ActionListener() { public void
actionPerformed(ActionEvent evt) { setVisible(false); } } );
addWindowListener(new WindowAdapter() { public void
windowClosing(WindowEvent e) { setVisible(false); } } );
setSize(220, 150);
}
}
class CopyCommand implements ActionListener, ItemListener
{
public void itemStateChanged(ItemEvent e)
{ if (e.getSource() instanceof Choice)
{
user = (String)e.getItem();
}
}
public void actionPerformed (ActionEvent e)
{
//getting data from textfield to server and back
//inserting server data to textarea
ta.append(br.readLine());
//ta.append(user + ": " + tf1.getText() + "\n");
tf1.setText("");
tf1.requestFocus();
}
public void keyPressed (KeyEvent ke)
{
//figure out if the enter key was pressed
if (ke.getKeyCode() == KeyEvent.VK_ENTER)
{
//getting data from textfield to server and back
//inserting server data to textarea
ta.append(br.readLine());
//ta.append(user + ": " + tf1.getText() + "\n");
tf1.setText("");
tf1.requestFocus();
}
}
public void keyTyped (KeyEvent ke) { }
public void keyReleased (KeyEvent ke) { }
}
public static void main(String[] args)
{
Runnable prog = new ReadMessages();
Frame f = new ChatClient();
f.setSize(600, 400);
f.show();
}
}