📄 chatcl~1.jav
字号:
package chat;
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import java.awt.*;
import java.awt.event.*;
class chatclient
{
public static ORB orb;
private chatserver myserver;
public static void main(String[] args)
{
try
{
orb = ORB.init(args,null);
chatclient c = new chatclient();
c.connect();
new loginframe("Give the user name here",c.myserver);
}
catch(Exception e)
{
System.out.println("Eroor occured : "+e);
}
}
public void connect()
{
try
{
org.omg.CORBA.Object obj = orb.resolve_initial_references("NameService");
NamingContext nc = NamingContextHelper.narrow(obj);
NameComponent ncmp = new NameComponent("ChatServer"," ");
NameComponent path[] = { ncmp };
myserver = chatserverHelper.narrow(nc.resolve(path));
}
catch(Exception e)
{
System.out.println("Error connection :"+e);
myserver = null;
return ;
}
System.out.println("Bound to server");
}
}
class loginframe extends Frame
{
chatserver srv;
TextField t= null;
Button b= null;
public loginframe(String s,chatserver sv)
{
super(s);
srv = sv;
setBounds(50,50,450,150);
setLayout(null);
setBackground(new Color(170,90,190));
Label l = new Label("User Id :");
l.setBounds(25,25,100,35);
t = new TextField(15);
t.setBounds(120,25,260,35);
b = new Button("Enter");
b.setBounds(150,70,160,85);
b.addActionListener(new al());
t.addActionListener(new al());
add(l);
add(t);
add(b);
addWindowListener(new wl());
setVisible(true);
}
class wl extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
Window w = (Window)(we.getSource());
w.setVisible(false);
w.dispose();
System.exit(1);
}
};
class al implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
java.lang.Object obj = ae.getSource();
String str = t.getText();
str = str.trim();
if(str.equals("")||str.length()>15)
return;
if(srv.login(str))
{
setVisible(false);
dispose();
new chatframe(str,srv);
}
else
System.out.println("Error occured : Either the user already exists or the room is full");
}
};
}
class chatframe extends Frame
{
chatserver srv;
TextField tf=null;
TextArea ta=null;
Button send=null;
int x=20,y=20;
Dimension d=null;
String uname=null;
int current=0;;
public chatframe(String str,chatserver sr)
{
super(str);
uname=str;
srv = sr;
setLayout(new BorderLayout());
ta =new TextArea(20,30);
add(ta,BorderLayout.CENTER);
tf = new TextField(20);
send = new Button("Send");
Panel p = new Panel();
p.setLayout(new GridLayout());
p.add(tf);
tf.addActionListener(new al());
send.addActionListener(new al());
p.add(send);
add(p,BorderLayout.SOUTH);
addWindowListener(new wl());
setVisible(true);
}
class wl extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
Window w = (Window)(we.getSource());
w.setVisible(false);
srv.logout(uname);
w.dispose();
System.exit(1);
}
};
class al implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
java.lang.Object obj = ae.getSource();
String str = tf.getText();
str = str.trim();
if(str.equals(""))
return;
str = uname+" : "+str;
tf.setText("");
ta.append("\n"+str);
srv.addmessage(str);
String msg;
while(true)
{
msg = srv.getnewmesg(current);
if(msg.equals(" "))
return;
if(msg.startsWith(uname))
current++;
else
{
ta.append("\n"+msg);
current++;
}
}
}
};
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -