package chat;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.net.*;
import java.io.*;
public class ChatClientFrame extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField jtfEingabe;
private JLabel label;
private JButton button;
private JEditorPane editorPane;
private Container contentPane;
private String labelText = "";
private JMenuBar menuBar;
private JMenu menuOperation, menuHelp;
private JMenuItem opClose, opConnect, opDisconnect, hlpHelp, hlpAbout;
private Action actClose, actConnect, actDisconnect, actHelp, actAbout;
private KeyStroke stroke;
private JToolBar toolBar;
private JButton jbClose, jbConnect, jbDisconnect, jbHelp, jbAbout;
private final String TTT_SCHL = "Schließt ";
private final String TTT_VERB = "Verbindet ";
private final String TTT_ABBR = "Verbindung-Abbruch ";
private final String TTT_HLP = "Help";
private final String TTT_ABT = "About";
private BufferedReader serverIn;
private PrintWriter serverOut;
private String host, user="";
private ClientInputThread inputThread;
private Socket clientSocket;
private int port=4444;
private InetAddress inetadr;
public ChatClientFrame (){
super ("Chat-Client von ");
setSize (400, 200);
contentPane=this.getContentPane();
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
//Eingabefeld einbauen
jtfEingabe = new JTextField ("");
jtfEingabe.setBackground (Color.white);
jtfEingabe.setForeground (Color.blue);
jtfEingabe.addKeyListener (new KeyAdapter (){
public void keyPressed (KeyEvent ke) {
// bei Enter Text übermitteln
if (ke.getKeyCode ()==KeyEvent.VK_ENTER) {
String text = jtfEingabe.getText().trim();
if (text.length() > 0){
if (serverOut != null){
serverOut.println(text);
//Eingabefeld wieder löschen
jtfEingabe.setText ("");
}
else {
jtfEingabe.setText (
"zunächst Verbindung zu einem server herastellen!");
}
}
else {jtfEingabe.setText ("Bitte Text eingeben!");
}
}
}
});
editorPane = new JEditorPane();
JScrollPane editorScroll = new JScrollPane (editorPane);
editorPane.setContentType("text/html");
this.createMenu();
this.createToolBar ();
JPanel panel = new JPanel ();
panel.setLayout (new BorderLayout());
contentPane.add (BorderLayout.CENTER, panel);
contentPane.add (BorderLayout.NORTH, toolBar);
panel.add (BorderLayout.CENTER, editorScroll);
panel.add (BorderLayout.SOUTH, jtfEingabe);
setVisible (true);
}
public void createMenu () {
menuBar = new JMenuBar();
this.setJMenuBar (menuBar);
menuOperation = new JMenu ("Operationen");
menuOperation.setMnemonic ('o');
menuHelp= new JMenu ("Hilfe");
menuHelp.setMnemonic ('h');
menuBar.add (menuOperation);
menuBar.add (menuHelp);
actClose = new AbstractAction ("Schließen", new ImageIcon ("images/Stop16.gif")) {
public void actionPerformed (ActionEvent ae) {
if (clientSocket != null){
disconnectServer ();
}
System.exit (0);
System.out.println ("Close wurde gedrückt");
}
};
// Key-Stroke-Objekt (Shortcut) definieren
stroke = KeyStroke.getKeyStroke (KeyEvent.VK_S, InputEvent.CTRL_MASK);
// Shortcut im Action-Objekt speichern
actClose.putValue (Action.ACCELERATOR_KEY, stroke);
actClose.putValue (Action.SHORT_DESCRIPTION, TTT_SCHL);
opClose = menuOperation.add (actClose);
opClose.setMnemonic ('s');
actConnect = new AbstractAction ("Verbinden", new ImageIcon ("images/Export16.gif")) {
public void actionPerformed (ActionEvent ae) {
connectServer();
}
};
// Key-Stroke-Objekt (Shortcut) definieren
stroke = KeyStroke.getKeyStroke (KeyEvent.VK_V, InputEvent.CTRL_MASK);
// Shortcut im Action-Objekt speichern
actConnect.putValue (Action.ACCELERATOR_KEY, stroke);
actConnect.putValue (Action.SHORT_DESCRIPTION, TTT_VERB);
opConnect = menuOperation.add (actConnect);
opConnect.setMnemonic ('v');
actDisconnect = new AbstractAction ("Verb. Abbrechen", new ImageIcon ("images/Import16.gif")) {
public void actionPerformed (ActionEvent ae) {
disconnectServer();
System.out.println ("Disconnect wurde gedrückt");
}
};
// Key-Stroke-Objekt (Shortcut) definieren
stroke = KeyStroke.getKeyStroke (KeyEvent.VK_A, InputEvent.CTRL_MASK);
// Shortcut im Action-Objekt speichern
actDisconnect.putValue (Action.ACCELERATOR_KEY, stroke);
actDisconnect.putValue (Action.SHORT_DESCRIPTION, TTT_ABBR);
opDisconnect = menuOperation.add (actDisconnect);
opDisconnect.setMnemonic ('a');
actHelp = new AbstractAction ("Hilfe", new ImageIcon ("images/Help16.gif")) {
public void actionPerformed (ActionEvent ae) {
}
};
// Key-Stroke-Objekt (Shortcut) definieren
stroke = KeyStroke.getKeyStroke (KeyEvent.VK_H, InputEvent.CTRL_MASK);
// Shortcut im Action-Objekt speichern
actHelp.putValue (Action.ACCELERATOR_KEY, stroke);
actHelp.putValue (Action.SHORT_DESCRIPTION, TTT_HLP);
hlpHelp = menuHelp.add (actHelp);
hlpHelp.setMnemonic ('h');
actAbout = new AbstractAction ("About", new ImageIcon ("images/About16.gif")) {
public void actionPerformed (ActionEvent ae) {
}
};
// Key-Stroke-Objekt (Shortcut) definieren
stroke = KeyStroke.getKeyStroke (KeyEvent.VK_B, InputEvent.CTRL_MASK);
// Shortcut im Action-Objekt speichern
actAbout.putValue (Action.ACCELERATOR_KEY, stroke);
actAbout.putValue (Action.SHORT_DESCRIPTION, TTT_HLP);
hlpAbout = menuHelp.add (actAbout);
hlpAbout.setMnemonic ('h');
}
private void createToolBar () {
toolBar = new JToolBar ();
jbClose = toolBar.add(actClose);
jbConnect = toolBar.add(actConnect);
jbDisconnect = toolBar.add(actDisconnect);
toolBar.addSeparator ();
jbHelp = toolBar.add(actHelp);
jbAbout = toolBar.add(actAbout);
}
private void connectServer () {
try {
host = InetAddress.getLocalHost().getHostName();
clientSocket = new Socket (host, port);
serverIn = new BufferedReader (new InputStreamReader (
clientSocket.getInputStream()));
serverOut = new PrintWriter (
clientSocket.getOutputStream (), true);
// BenutzerName an Server
serverOut.println (user);
inputThread = new ClientInputThread (editorPane, serverIn);
inputThread.start ();
jtfEingabe.setText ("");
}
catch (UnknownHostException uhe) {
System.out.println ("bei dem Connect: UnknownHostException");
System.out.println ("*** "+ uhe.toString());
System.exit(-1);
}
catch (IOException ioe) {
System.out.println ("bei dem Connect: IOException");
System.out.println ("*** "+ ioe.toString());
System.exit(-1);
}
}
private void disconnectServer(){
serverOut.println ("exit");
inputThread = null;
try {
serverIn.close();
serverOut.close();
clientSocket.close();
}
catch (IOException ioe) {
System.out.println ("*** "+ioe.toString());
}
}
public static void main(String [] args) {
new ChatClientFrame();
}
public void actionPerformed (ActionEvent ae) {
}
}