ClientPoint.java
资源名称:chat.rar [点击查看]
上传用户:qaz007_wsx
上传日期:2022-06-21
资源大小:3k
文件大小:7k
源码类别:
ICQ/即时通讯
开发平台:
Java
- //ClientPoint.java
- import javax.swing.*;
- import java.awt.event.*;
- import java.io.*;
- import java.net.*;
- import java.awt.*;
- public class ClientPoint extends JFrame implements ActionListener,KeyListener{
- static int SERVERPORT=8088; //端口
- static String SERVERADDRESS="127.0.0.1"; // IP地址
- private BufferedReader cin=null;
- private PrintWriter cout=null;
- private Socket client=null;
- public String clientname;
- //初始化
- private JPanel jPanel1=new JPanel();
- private JPanel jPanel2=new JPanel();
- private JPanel jPanel3=new JPanel();
- private JLabel JLabelname=new JLabel("用户名");
- private JLabel JLabelInfo=new JLabel("请输入信息");
- private JLabel JLabelPassword=new JLabel("密码");
- private JTextField jTextSendInfo=new JTextField(25);
- private JTextField jTextname=new JTextField(10);
- private JTextField jTextPassword=new JTextField(10);
- private JTextArea jTextGetInfo=new JTextArea();
- public JScrollPane areaScrollPane=new JScrollPane(jTextGetInfo);
- private Button Send=new Button("发送");
- private Button Login=new Button("登录");
- // 初始化界面
- public ClientPoint(){
- super("聊天室^_^");
- enableEvents(AWTEvent.WINDOW_EVENT_MASK);
- setSize(480,450); //设置整个聊天界面
- jPanel2.add(JLabelname );
- jPanel2.add(jTextname);
- jPanel2.add(JLabelPassword );
- jPanel2.add(jTextPassword);
- //按钮Login的设置
- jPanel2.add(Login);
- jPanel2.setBackground(new Color(75, 234, 166));
- jPanel2.setLayout(new FlowLayout());
- this.getContentPane().add("North",jPanel2);
- //背景和字体的设置
- jTextGetInfo.setBackground(new Color(122,150,223));
- jTextGetInfo.setFont(new java.awt.Font("Dialog", 0, 15));
- jTextGetInfo.setForeground(new Color(176, 42, 0));
- //jPanel3.add(jTextGetInfo);
- this.getContentPane().add("Center",jTextGetInfo);
- this.setResizable(false);
- jPanel1.add(JLabelInfo);
- jPanel1.add(jTextSendInfo);
- //按钮Send的设置
- jPanel1.add(Send);
- jPanel1.setBackground(new Color(75, 234, 166));
- jPanel1.setLayout(new FlowLayout());
- this.getContentPane().add("South",jPanel1);
- areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
- areaScrollPane.setAutoscrolls(true);
- jTextSendInfo.setText("");
- jPanel1.add("North",areaScrollPane);
- jTextGetInfo.setEditable(false);
- //开始监听
- Send.addActionListener(this);
- jTextSendInfo.addKeyListener(this);
- Login.addActionListener(new ActionListener()
- {public void actionPerformed(ActionEvent e){
- if(!jTextname.getText().equals("")){
- clientname=jTextname.getText();
- connect();
- Login.setEnabled(false);
- Send.setEnabled(true);
- jTextname.setText("");
- }
- else JOptionPane.showMessageDialog(null,"警告:用户名不能为空!!","消息对话框",JOptionPane.WARNING_MESSAGE);
- }});
- //this.setContentPane(jPanel1);
- }
- // 登录进入聊天室
- public void connect(){
- try{
- client=new Socket(SERVERADDRESS,SERVERPORT);
- cout=new PrintWriter(client.getOutputStream());
- cin=new BufferedReader(
- new InputStreamReader(client.getInputStream()));
- cout.println("欢迎["+clientname+"]进入聊天室");
- cout.flush();
- Receiver r=new Receiver();
- r.start();
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- protected void processWindowEvent(WindowEvent e){
- if(e.getID()==WindowEvent.WINDOW_CLOSING){
- quit();
- }
- super.processWindowEvent(e);
- }
- //主函数
- public static void main(String[] args){
- ClientPoint cp=new ClientPoint();
- cp.show();
- }
- //与服务器连接
- class Receiver extends Thread{
- public void run(){
- String msg=null;
- JScrollBar sb;
- try{
- msg=cin.readLine();
- while(true){
- jTextGetInfo.append(msg+"n");
- sb=areaScrollPane.getVerticalScrollBar();
- sb.setValue(sb.getMaximum()+50);
- jTextSendInfo.setEnabled(true);
- msg=cin.readLine();
- }
- }catch(Exception e){
- Send.setEnabled(false);
- }
- }
- }
- //离开聊天室
- void quit(){
- try{
- cout.println("["+clientname+"]离开聊天室");
- cout.flush();
- cout.println("exit");
- cout.flush();
- System.out.print("lianjieduankai");
- cin.close();
- cout.close();
- client.close();
- }catch(Exception e){}
- finally{System.exit(0);
- }
- }
- //处理和监听事件
- public void actionPerformed(ActionEvent e){
- if(!jTextSendInfo.getText().equals("")){
- cout.println("["+clientname+"]:"+jTextSendInfo.getText());
- cout.flush();
- jTextSendInfo.setText("");
- jTextSendInfo.setFocusable(true);
- }
- else JOptionPane.showMessageDialog(null,"警告:消息不能为空!!","消息对话框",JOptionPane.WARNING_MESSAGE);
- }
- public void keyPressed(KeyEvent e){}
- public void keyTyped(KeyEvent e){}
- public void keyReleased(KeyEvent e)
- {
- if (e.getKeyCode()==KeyEvent.VK_ENTER)
- {
- if(!jTextSendInfo.getText().equals(""))
- {
- if(cout==null)
- {
- clientname=jTextSendInfo.getText();
- connect();
- Login.setEnabled(false);
- Send.setEnabled(true);
- jTextSendInfo.setText("");
- }
- else
- {
- cout.println("["+clientname+"]:"+jTextSendInfo.getText());
- cout.flush();
- jTextSendInfo.setText("");
- jTextSendInfo.setFocusable(true);
- }
- }
- else
- {
- if (cout==null)
- {
- JOptionPane.showMessageDialog(null,"警告:用户名不能为空!!","消息对话框",JOptionPane.WARNING_MESSAGE);
- }
- else JOptionPane.showMessageDialog(null,"警告:消息不能为空!!","消息对话框",JOptionPane.WARNING_MESSAGE);
- }
- }
- }
- }