System.out.println(\ e.printStackTrace(); } }
图5.1服务器端启动
5.2.2客户端设计 (1)界面设计
public class SwingChatv2Client extends UnicastRemoteObject implements Chatv2Client,ActionListener{
Chatv2Server server=null;//界面的设计 boolean connected=false; String title=\ JFrame f;
String username=\ JTextArea area; JTextField inName; JButton connect; JButton disconnect; JButton send; JButton info;
JTextField contentIn; private ActionListener change;
public SwingChatv2Client() throws RemoteException{ super();
//System.setSecurityManager(new RMISecurityManager());
}
public void init(){
//setLayout(new BorderLayout());//界面的设计参数 f=new JFrame(title);
f.setBounds(250,250,500,500); f.setVisible(true);
JPanel pNorth,pSouth,pText; pNorth=new JPanel(); pSouth=new JPanel(); pText=new JPanel();
inName=new JTextField(6); contentIn=new JTextField(22); area=new JTextArea(15,35);
connect=new JButton(\
disconnect=new JButton(\ send=new JButton(\ info=new JButton(\
pNorth.add(new JLabel(\ pNorth.add(inName); pNorth.add(connect); pNorth.add(disconnect);
pSouth.add(new JLabel(\ pSouth.add(contentIn); pSouth.add(send); pSouth.add(info);
pText.add(new JScrollPane(area)); connect.addActionListener(this); disconnect.addActionListener(this); send.addActionListener(this); info.addActionListener(this);
contentIn.addActionListener(this); f.add(pNorth,BorderLayout.NORTH); f.add(pSouth,BorderLayout.SOUTH); f.add(pText,BorderLayout.CENTER);
f.validate();
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){ System.exit(0); } }); /*try{
UnicastRemoteObject.export(this); }
catch(Exception e){
}*/ }
public static void main(String args[])throws RemoteException{
SwingChatv2Client client=new SwingChatv2Client(); client.init();
}
public void connect(){ try{
server=(Chatv2Server)Naming.lookup(\r\客户端连接
//此处localhost可改为服务端所在主机IP
//server=(IChatServer)Naming.lookup(\r\
System.out.println(\ connected=true; setStatus();
username=inName.getText().trim(); server.addClient(this,username+\Entered!\
} catch(Exception e){
area.append(\ //area.append(\ connected=false; setStatus();
server=null;
System.out.println(e); } }
public void disconnect(){ try{
if(server==null) return;
server.removeClient(this,username+\ server=null; }
catch(Exception e){
area.append(\ int length = area.getText().length(); area.setCaretPosition(length); }
finally{
connected=false; setStatus(); } }
/*Add some other information by this button!*/ public void info(){ try{
if(server==null) return;
server.checkinfosavve(this); }
catch(Exception e){ } }
public void actionPerformed(ActionEvent e){//按钮事件函数 if(e.getSource()==connect){ //连接函数 connect(); }
else if(e.getSource()==info){ info(); }
else if(e.getSource()==disconnect){ //断开连接 disconnect(); } else if(e.getSource()==send){ //send function //else if(e.getSource()==contentIn){ if(server==null)
return; try{
String news=contentIn.getText(); server.sendMessage(this,news);
server.savve(this, inName.getText().trim(), news); contentIn.setText(\ } catch(Exception ee){
area.append(\ int length = area.getText().length(); area.setCaretPosition(length); } } else{
if (server==null) return;
try{ server.sendMessage(this,contentIn.getText()); contentIn.setText(\ }
catch(Exception ex){
area.append(\ int length = area.getText().length(); area.setCaretPosition(length);
}
public void setStatus(){ if(connected)
f.setTitle(title+\ else
f.setTitle(title+\ }
public String getName() throws RemoteException{ return username; }
public void sendMessage(String msg) throws RemoteException{ area.append(msg+\
int length = area.getText().length(); area.setCaretPosition(length); (3)客户端界面
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库基于远程方法调用的聊天室软件设计与实现—毕业论文(5)在线全文阅读。
相关推荐: