?? chatkaro12mclient.java
字號:
import java.io.*;
import javax.swing.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class chatkaro12mClient extends Panel
{
TextArea receivedText;
Socket sock;
private GridBagConstraints c;
private GridBagLayout gridBag;
private Frame frame;
private Label label;
JButton send;
JButton exit;
private int port=5001;
private TextArea sendText;
private String hostname;
private String username;
private DataOutputStream remoteOut;
ImageIcon i1,i2;
public static void main(String args[])
{
if(args.length != 2)
{
System.out.println("Format is : java chatkaro12mClient (name) (inetaddress)");
return;
}
Frame f1=new Frame("Welcome Protocol");
f1.resize(800,600);
f1.show();
JOptionPane.showMessageDialog(f1,"Welcome "+args[0]+".. Have a nice Session.","Welcome",0);
Frame f= new Frame("Connecting to Mr. "+args[0]);
chatkaro12mClient chat=new chatkaro12mClient(f,args[0],args[1]);
f.add("Center",chat);
f.setSize(800,600);
f.show();
chat.client();
}
public chatkaro12mClient(Frame f,String user,String host)
{
frame=f;
frame.addWindowListener(new WindowExitHandler());
username=user;
hostname=host;
//Insets insets=new Insets(10,20,5,10);
//gridBag=new GridBagLayout();
//setLayout(gridBag);
/*c=new GridBagConstraints();
c.insets=insets;
c.gridy=0;
c.gridx=0;*/
label=new Label("Text to send :");
add(label);
/*gridBag.setConstraints(label,c);
c.gridx=1;*/
sendText=new TextArea(15,30);
/*sendText.addActionListener(new TextActionHandler());
gridBag.setConstraints(sendText,c);*/
add(sendText);
//c.gridy=1;
//c.gridx=0;
label= new Label("Text received :");
//gridBag.setConstraints(label,c);
add(label);
//c.gridx=1;
receivedText=new TextArea(15,30);
//gridBag.setConstraints(receivedText,c);
add(receivedText);
ImageIcon i1=new ImageIcon("click.gif");
ImageIcon i2=new ImageIcon("doorin2.gif");
send=new JButton(i1);
exit=new JButton(i2);
add(send);
add(exit);
send.addActionListener(new TextActionHandler());
exit.addActionListener(new EXIT());
}
void client()
{
try
{
if(hostname.equals("local"))
hostname=null;
InetAddress serverAddr= InetAddress.getByName(hostname);
sock=new Socket(serverAddr.getHostName(),port);
remoteOut=new DataOutputStream(sock.getOutputStream());
System.out.println("Connected to server " + serverAddr.getHostName() + "on port " + sock.getPort());
new chatkaro12mClientReceive(this).start();
}
catch(IOException e)
{
System.out.println(e.getMessage() + " : Failed to connect to server.");
}
}
protected void finalize() throws Throwable
{
try
{
if(remoteOut != null)
remoteOut.close();
if(sock != null)
sock.close();
}
catch(IOException x)
{}
super.finalize();
}
class WindowExitHandler extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
Window w=e.getWindow();
w.setVisible(false);
w.dispose();
System.exit(0);
}
}
class EXIT implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if((JOptionPane.showConfirmDialog(new Frame(),"Are You Sure to close the Session?"))==JOptionPane.YES_OPTION)
{
JOptionPane.showMessageDialog(new Frame(),"Thank U. Visit Again.","Good Bye",0);
System.exit(0);
}
}
}
class TextActionHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
remoteOut.writeUTF(sendText.getText());
JOptionPane.showMessageDialog(new Frame(),"Your msg has been sent ","Confirmation",0);
sendText.setText("");
}
catch(IOException x)
{
System.out.println(x.getMessage() + " : connection to peer lost.");
}
}
}
}
class chatkaro12mClientReceive extends Thread
{
private chatkaro12mClient chat;
chatkaro12mClientReceive(chatkaro12mClient chat)
{
this.chat=chat;
}
public synchronized void run()
{
String s;
DataInputStream remoteIn=null;
try
{
remoteIn= new DataInputStream(chat.sock.getInputStream());
while(true)
{
s=remoteIn.readUTF();
chat.receivedText.setText(s);
}
}
catch(IOException e)
{
System.out.println(e.getMessage() + " : connection to peer lost.");
}
finally
{
try
{
if(remoteIn !=null)
remoteIn.close();
}
catch(IOException x)
{}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -