⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientint.java

📁 《J2EE专业项目实例开发》源代码
💻 JAVA
字号:
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import javax.swing.Timer;

public class clientInt extends JFrame implements ActionListener
{

	Timer t = new Timer(5000,new TimerAction());
	String usr_name;
	public String remUser;

	class TimerAction implements ActionListener
	{
		Socket toServer;
		ObjectInputStream streamFromServer;
		PrintStream streamToServer;
		public void actionPerformed(ActionEvent e2)
		{
		
      		try
		{
        	toServer=new Socket("machine-name",1001);
        	streamFromServer=new ObjectInputStream(toServer.getInputStream());
        	streamToServer=new PrintStream(toServer.getOutputStream());
       		message=txtMsg.getText();
		
		//send a message to the server
        	streamToServer.println("From Timer");
        	
		//receive vectors from the server
       		Vector vector=(Vector)streamFromServer.readObject();
		Vector vector1=(Vector)streamFromServer.readObject();
		
		//show the online users
		txtListUsers.setText("");
		for(int j=1;j<vector1.capacity();j++)
		{
          	txtListUsers.append((String)vector1.elementAt(j));
		txtListUsers.append("\n");
	        }
	
		//show the messsages
		int i=messageCount;
      		for(;i<vector.capacity();i++)
		{
		
          	txtMessages.append((String)vector.elementAt(i));
		txtMessages.append("\n");
       		
		}
		messageCount=i;
	}//end of try
               
	catch(Exception e)
	{
		System.out.println("Exception "+e);
	}

	}//end of actionPerformed
}//end of TimerListener class	
	

int messageCount=0;
String name;
PrintStream  streamToServer;
ObjectInputStream streamFromServer;
Socket toServer;


JTextArea txtMessages;
JTextArea txtListUsers;
JTextField txtMsg;
JButton msgSendBtn;
JButton userLoginBtn;
JButton userRegisterBtn;
JButton userLogoutBtn;
 
JLabel lblChatRoom;
JLabel lblUserList;

JScrollPane jspSendMsgPane;
JScrollPane jspTxtMsgPane;	 
JScrollPane jspUserListPane;

JTextField textWriteMsg;
String message;
int nSend;

 
public clientInt(String nm)
{
	remUser=nm;
	usr_name=nm;
	this.setTitle("FunChat: "+usr_name); //set the title name 
        JPanel panel=new JPanel();
		
 	panel.setLayout(new GridBagLayout());
 	GridBagConstraints gbCons=new GridBagConstraints();

	gbCons.gridx=0;
	gbCons.gridy=0;
	lblChatRoom=new JLabel("Chat Room",SwingConstants.LEFT);
	panel.add(lblChatRoom, gbCons);

	gbCons.gridx=1;
	gbCons.gridy=0;
	
	lblUserList=new JLabel("Online Users",SwingConstants.LEFT);
	panel.add(lblUserList, gbCons);
		
	gbCons.gridx=0;
	gbCons.gridy=1;
	gbCons.gridwidth=1;
	gbCons.gridheight=1;
	gbCons.weightx=1.0;
	gbCons.weighty=1.0;
	txtMessages=new JTextArea(25,35);
	txtMessages.setEditable(false);
	jspTxtMsgPane=new JScrollPane(txtMessages,  	JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	panel.add(jspTxtMsgPane, gbCons);

	gbCons.gridx=1;
	gbCons.gridy=1;
	gbCons.gridwidth=1;
	gbCons.gridheight=1;
	gbCons.weightx=1.0;
	gbCons.weighty=1.0;
	txtListUsers=new JTextArea(25,10);
	txtListUsers.setEditable(false);
	jspUserListPane=new JScrollPane(txtListUsers,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	panel.add(jspUserListPane, gbCons);

	gbCons.gridx=0;
	gbCons.gridy=2;
	gbCons.gridwidth=1;
	gbCons.gridheight=1;
	gbCons.weightx=1.0;
	gbCons.weighty=1.0;
	txtMsg=new JTextField(35);

	jspSendMsgPane=new JScrollPane(txtMsg, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	panel.add(jspSendMsgPane, gbCons);

	gbCons.gridx=1;
	gbCons.gridy=2;
	gbCons.gridwidth=1;
	gbCons.gridheight=1;
	gbCons.weightx=1.0;
	gbCons.weighty=1.0;
	gbCons.anchor=GridBagConstraints.WEST;
	msgSendBtn=new JButton("Send");
	panel.add(msgSendBtn, gbCons);
	
	msgSendBtn.addActionListener(this);

	JPanel btnPanel=new JPanel();

	userLogoutBtn=new JButton("Logout");
	userLogoutBtn.addActionListener(this);
	
	//add a listerener to the window
	this.addWindowListener(
                 new WindowAdapter()
		 {
                     public void windowClosing(WindowEvent e1)
			{
				try
				{
					Socket toServer;
					ObjectInputStream streamFromServer;
					PrintStream streamToServer;
        				toServer=new Socket("machine-name",1001);
		     			   streamToServer=new PrintStream(toServer.getOutputStream());
        				streamToServer.println("User Logout");
        				streamToServer.println(remUser);
       				}//end of try
				catch(Exception e2)
				{
					System.out.println("Exception Occured: "+e2);
				}
			}
                         
                 }
              );  

	btnPanel.add(userLogoutBtn);
	
	gbCons.gridx=0;
	gbCons.gridy=3;
	gbCons.gridwidth=1;
	gbCons.gridheight=1;
	gbCons.weightx=1.0;
	gbCons.weighty=1.0;
	gbCons.anchor=GridBagConstraints.EAST;
	gbCons.fill=GridBagConstraints.HORIZONTAL;
	panel.add(btnPanel, gbCons);

	getContentPane().add(panel);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        
	setVisible(true);
	setSize(546,567);
	t.start();
    	
}//end of clientInt()
public void actionPerformed(ActionEvent e1)
{
	JButton button=(JButton)e1.getSource();
	
	//if logout button clicked
	if(button.equals(userLogoutBtn))
	{
		try
		{
        		toServer=new Socket("machine-name",1001);
        		streamToServer=new PrintStream(toServer.getOutputStream());
        		
			//send a msg to server for logging out
			streamToServer.println("User Logout");
        		streamToServer.println(remUser);
       		}
		catch(Exception e)
		{
			System.out.println("Exception Occured: "+e);
		}
		this.dispose();
	}
	//else if Send button is clicked
	else
	{
		int num1=0,num2=0,res=0;
		String name="";
		try
		{
        	toServer=new Socket("machine-name",1001);
        	streamFromServer=new ObjectInputStream(toServer.getInputStream());
        	streamToServer=new PrintStream(toServer.getOutputStream());
       
		message=txtMsg.getText();
		String msg=message;
		
		//send the user name and msg typed to the server
		streamToServer.println(usr_name+":"+msg);
	
	        txtMsg.setText("");
		
		//read the reply from the server
		Vector vector=(Vector)streamFromServer.readObject();
		int i=messageCount;
        	for(;i<vector.capacity();i++)
		{
            		txtMessages.append((String)vector.elementAt(i)); //display the messages
		 	txtMessages.append("\n");
        	}//end of for
		messageCount=i;
	
	        }//end of try
		catch(Exception e)
		{
			System.out.println("Exception "+e);
		}
	}//end of else         
}//actionPerformed()
	

	public static void main(String args[])
	{
		String nm=new String();
		clientInt CI=new clientInt(nm);
	}
}//end of class






⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -