| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1514 人关注过本帖
标题:聊天程序客户端中,用户设置UserConf和连接用户ConnectConf函数该怎么写
只看楼主 加入收藏
夙愿000000
Rank: 1
来 自:甘肃张掖
等 级:新手上路
帖 子:10
专家分:0
注 册:2016-4-10
结帖率:50%
收藏
 问题点数:0 回复次数:0 
聊天程序客户端中,用户设置UserConf和连接用户ConnectConf函数该怎么写
package ChatServer;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import *;
import *;
public class ChatClient extends JFrame implements ActionListener{
    String ip="192.168.191.1";
    int port=8000;
    String userName="张三";
    int type=0;
    JComboBox combobox;
    JTextArea messageShow;
    JScrollPane messageScrollPane;
    JLabel express,sendToLabel,messageLabel;
    JTextField clientMessage;
    JCheckBox checkbox;
    JComboBox actionlist;
    JButton clientMessageButton;
    JTextField showStatus;
    Socket socket;
    ObjectOutputStream output;
    ObjectInputStream input;
    ClientReceive recvThread;
    JMenuBar jMenuBar=new JMenuBar();  //菜单项
    JMenu operateMenu=new JMenu("操作(O)");    //菜单组
    //菜单项
    JMenuItem loginItem=new JMenuItem("用户登录(I)");
    JMenuItem logoffItem=new JMenuItem("用户注销(L)");
    JMenuItem exitItem=new JMenuItem("退出(X)");
    JMenu conMenu=new JMenu("设置(C)");
    JMenuItem userItem=new JMenuItem("用户设置(U)");
    JMenuItem connectItem=new JMenuItem("连接设置(C)");
    JMenu helpMenu=new JMenu("帮助(H)");
    JMenuItem helpItem=new JMenuItem("帮助(H)");
   
    JToolBar toolBar=new JToolBar();   //工具栏
    JButton loginButton;
    JButton logoffButton;
    JButton userButton;
    JButton connectButton;
    JButton exitButton;
    Dimension faceSize=new Dimension(400,600);
    JPanel downPanel;
    GridBagLayout gridBag;
    GridBagConstraints gridBagCon;
    public ChatClient()
    {
        init();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        this.setSize(faceSize);
        show();
        Dimension screenSize =Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation((int)(screenSize.width-faceSize.getWidth())/2, (int)(screenSize.width-faceSize.getWidth())/2);
        this.setResizable(false);
        this.setTitle("聊天室客户端");
        
        
    }
    public void init()
    {
        Container contentPane=getContentPane();
        contentPane.setLayout(new BorderLayout());
        operateMenu.add(loginItem);
        operateMenu.add(logoffItem);
        operateMenu.add(exitItem);
        jMenuBar.add(operateMenu);
        conMenu.add(userItem);
        conMenu.add(connectItem);
        jMenuBar.add(conMenu);
        helpMenu.add(helpItem);
        jMenuBar.add(helpMenu);
        setJMenuBar(jMenuBar);
        
        loginButton=new JButton("登录");
        logoffButton=new JButton("注销");
        userButton=new JButton("用户设置");
        connectButton=new JButton("连接设置");
        exitButton=new JButton("退出");
        logoffButton.setToolTipText("连接到指定的服务器");
        logoffButton.setToolTipText("与服务器断开断开连接");
        userButton.setToolTipText("设置用户信息");
        connectButton.setToolTipText("设置所要连接的服务器信息");
        toolBar.add(userButton);
        toolBar.add(connectButton);
        toolBar.addSeparator();
        toolBar.add(exitButton);
        contentPane.add(toolBar, BorderLayout.NORTH);
        checkbox=new JCheckBox("悄悄话");
        checkbox.setSelected(false);
        actionlist=new JComboBox();
        actionlist.addItem("微笑地");
        actionlist.addItem("高兴地");
        actionlist.addItem("轻轻地");
        actionlist.addItem("生气的");
        actionlist.addItem("小心的");
        actionlist.addItem("静静地");
        actionlist.setSelectedIndex(0);
        
        loginButton.setEnabled(true);
        logoffButton.setEnabled(false);
        
        loginItem.addActionListener(this);
        logoffItem.addActionListener(this);
        exitButton.addActionListener(this);
        userItem.addActionListener(this);
        connectItem.addActionListener(this);
        exitItem.addActionListener(this);
        userItem.addActionListener(this);
        connectItem.addActionListener(this);
        helpItem.addActionListener(this);
        
        loginButton.addActionListener(this);
        logoffButton.addActionListener(this);
        userButton.addActionListener(this);
        connectButton.addActionListener(this);
        exitButton.addActionListener(this);
        combobox=new JComboBox();
        combobox.insertItemAt("所有人", 0);
        combobox.setSelectedItem(0);
        messageShow=new JTextArea();
        messageShow.setEditable(false);
        
        messageScrollPane=new JScrollPane(messageShow,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        messageScrollPane.setPreferredSize(new Dimension(400,400));
        messageScrollPane.revalidate();
        clientMessage=new JTextField(23);
        clientMessage.setEnabled(false);
        clientMessageButton=new JButton();
        clientMessageButton.setText("发送");
        clientMessage.addActionListener(this);
        clientMessageButton.addActionListener(this);
        sendToLabel=new JLabel("发送至");
        express=new JLabel("  表情  ");
        messageLabel=new JLabel("发送信息");
        downPanel=new JPanel();
        gridBag=new GridBagLayout();
        downPanel.setLayout(gridBag);
        gridBagCon=new GridBagConstraints();
        gridBagCon.gridx=0;
        gridBagCon.gridy=0;
        gridBagCon.gridwidth=5;
        gridBagCon.gridheight=2;
        gridBagCon.ipadx=5;
        gridBagCon.ipady=5;
        JLabel none=new JLabel("   ");
        gridBag.setConstraints(none, gridBagCon);
        downPanel.add(none);
        gridBagCon=new GridBagConstraints();
        gridBagCon.gridx=0;
        gridBagCon.gridy=2;
        gridBagCon.insets=new Insets(1,0,0,0);
        gridBag.setConstraints(sendToLabel, gridBagCon);
        downPanel.add(sendToLabel);
        gridBagCon=new GridBagConstraints();
        gridBagCon.gridx=1;
        gridBagCon.gridy=2;
        gridBagCon.anchor =GridBagConstraints.LINE_START;
        gridBag.setConstraints(combobox, gridBagCon);
        downPanel.add(combobox);
        gridBagCon=new GridBagConstraints();
        gridBagCon.gridx=2;
        gridBagCon.gridy=2;
        gridBagCon.anchor =GridBagConstraints.LINE_END;
        gridBag.setConstraints(express, gridBagCon);
        downPanel.add(express);
        gridBagCon=new GridBagConstraints();
        gridBagCon.gridx=3;
        gridBagCon.gridy=2;
        gridBagCon.anchor =GridBagConstraints.LINE_START;
        gridBag.setConstraints(actionlist, gridBagCon);
        downPanel.add(actionlist);
        gridBagCon=new GridBagConstraints();
        gridBagCon.gridx=4;
        gridBagCon.gridy=2;
        gridBagCon.insets =new Insets(1,0,0,0);
        gridBag.setConstraints(checkbox, gridBagCon);
        downPanel.add(checkbox);
        gridBagCon=new GridBagConstraints();
        gridBagCon.gridx=0;
        gridBagCon.gridy=3;
        gridBag.setConstraints(messageLabel, gridBagCon);
        downPanel.add(messageLabel);
        gridBagCon=new GridBagConstraints();
        gridBagCon.gridx=1;
        gridBagCon.gridy=3;
        gridBagCon.gridwidth=3;
        gridBagCon.gridheight=1;
        gridBag.setConstraints(clientMessage, gridBagCon);
        downPanel.add(clientMessage);
        gridBagCon=new GridBagConstraints();
        gridBagCon.gridx=4;
        gridBagCon.gridy=3;
        gridBag.setConstraints(clientMessageButton, gridBagCon);
        downPanel.add(clientMessageButton);
        showStatus=new JTextField(35);
        showStatus.setEditable(false);
        gridBagCon=new GridBagConstraints();
        gridBagCon.gridx=0;
        gridBagCon.gridy=5;
        gridBagCon.gridwidth=5;
        gridBag.setConstraints(showStatus, gridBagCon);
        downPanel.add(showStatus);
        contentPane.add(messageScrollPane, BorderLayout.CENTER);
        contentPane.add(downPanel, BorderLayout.SOUTH);
        this.addWindowListener(
                new WindowAdapter()
                {
                    public void windowClosing(WindowEvent e)
                    {
                        if(type==1)
                        {
                            DisConnect();
                        }
                        System.exit(0);
                    }
                }
                );
        }
    public void actionPerformed(ActionEvent e)
    {
        Object obj=e.getSource();
        if(obj==userItem||obj==userButton)
        {
            UserConf userConf=new UserConf(this,userName);
            userConf.show();
            userName=userConf.userInputName;
        }
        else if(obj==connectItem||obj==connectButton)
        {
            ConnectConf conConf=new ConnectConf(this,ip,port);
            conConf.show();
            ip=conConf.userInputIP;
            port=conConf.userInputPort;
        }
        else if(obj==loginItem||obj==loginButton)
        {
            Connect();
        }
        else if(obj==logoffItem||obj==logoffButton)
        {
            DisConnect();
            showStatus.setText("");
        }
        else if(obj==clientMessage||obj==clientMessageButton)
        {
            SendMessage();
            clientMessage.setText("");
        }
        else if(obj==exitButton||obj==exitItem)
        {
            int j=JOptionPane.showConfirmDialog(this,"真的要退出吗?","退出", JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE);
            if(j==JOptionPane.YES_OPTION)
            {
                if(type==1)
                    DisConnect();
                System.exit(0);
            }
        }
        else if(obj==helpItem)
        {
            Help helpDialog=new Help();
            helpDialog.show();
        }
    }
    public void Connect()
    {
        try
        {
            socket=new Socket(ip,port);
        }
        catch(Exception e)
        {
            JOptionPane.showConfirmDialog(this,"不能连接到指定的服务器.\n请确认连接设置是否正确.","提示", JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE);
            return;
        }
        try
        {
            output=new ObjectOutputStream(socket.getOutputStream());
            output.flush();
            input=new ObjectInputStream(socket.getInputStream());
            output.writeObject(userName);
            output.flush();
            recvThread=new ClientReceive(socket,output,input,combobox,messageShow,showStatus);
            recvThread.start();
            loginButton.setEnabled(false);
            loginItem.setEnabled(false);
            userButton.setEnabled(false);
            userItem.setEnabled(false);
            connectButton.setEnabled(false);
            connectItem.setEnabled(false);
            logoffItem.setEnabled(true);
            logoffButton.setEnabled(true);
            clientMessage.setEnabled(true);
            messageShow.append("连接服务器"+ip+":"+port+"成功..\n");
            type=1;
        }
        catch(Exception e)
        {
            System.out.println(e);
            return;
        }
    }
    public void DisConnect()
    {
        loginButton.setEnabled(true);
        loginItem.setEnabled(true);
        userButton.setEnabled(true);
        userItem.setEnabled(true);
        connectButton.setEnabled(true);
        connectItem.setEnabled(true);
        logoffItem.setEnabled(false);
        logoffButton.setEnabled(false);
        clientMessage.setEnabled(false);
        if(socket.isClosed())
        {
            return;
        }
        try
        {
            output.writeObject("用户下线");
            output.flush();
            input.close();
            output.close();
            socket.close();
            messageShow.append("已经与服务器断开连接...\n");
            type=0;
        }
        catch(Exception e)
        {
            
        }
    }
    public void SendMessage()
    {
        String toSomebody=combobox.getSelectedItem().toString();
        String status="";
        if(checkbox.isSelected())
        {
            status="悄悄话";
        }
        String action=actionlist.getSelectedItem().toString();
        String message=clientMessage.getText();
        if(socket.isClosed())
        {
            return;
        }
        try
        {
            output.writeObject("聊天信息");
            output.flush();
            output.writeObject(toSomebody);
            output.flush();
            output.writeObject(status);
            output.flush();
            output.writeObject(action);
            output.flush();
            output.writeObject(message);
            output.flush();
        }
        catch(Exception e)
        {
            
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method
        ChatClient app=new ChatClient();
    }

}




//ServerReceive  
package ChatServer;
import javax.swing.*;
import *;
import *;
public class ServerReceive extends Thread{
    JTextArea textarea;
    JTextField textfield;
    JComboBox combobox;
    Node client;
    UserLinkList userLinkList;
    public boolean isStop;
    public ServerReceive(JTextArea textarea,JTextField textfield,JComboBox combobox,Node client,UserLinkList userLinkList)
    {
        this.textarea=textarea;
        this.textfield=textfield;
        this.client=client;
        this.userLinkList=userLinkList;
        
        isStop=false;
    }
    public void run()
    {
        sendUserList();
        while(!isStop&&!client.socket.isClosed())
        {
            try
            {
                String type=(String)client.input.readObject();
                if(type.equals("聊天信息"))
                {
                    String toSomebody=(String)client.input.readObject();
                    String status=(String)client.input.readObject();
                    String action=(String)client.input.readObject();
                    String message=(String)client.input.readObject();
                    String msg=client.username+" "+action+"对 "+toSomebody+"说:"+message+"\n";
                    if(status.equalsIgnoreCase("悄悄话"))
                    {
                        msg="[悄悄话]"+msg;
                    }
                    textarea.append(msg);
                    if(toSomebody.equalsIgnoreCase("所有人"))
                    {
                        sendToAll(msg);
                    }
                    else
                    {
                        try
                        {
                            client.output.writeObject("聊天信息");
                            client.output.flush();
                            client.output.writeObject(msg);
                            client.output.flush();
                        }
                        catch(Exception e)
                        {
                           
                        }
                        Node node=userLinkList.findUser(toSomebody);
                        if(node!=null)
                        {
                            node.output.writeObject("聊天信息");
                            node.output.flush();
                            node.output.writeObject(msg);
                            node.output.flush();
                        }
                    }
                }
                else if(type.equalsIgnoreCase("用户下线"))
                {
                    Node node=userLinkList.findUser(client.username);
                    userLinkList.delUser(node);
                    String msg="用户 "+client.username+"下线\n";
                    int count=userLinkList.getCount();
                    combobox.removeAllItems();
                    combobox.addItem("所有人");
                    int i=0;
                    while(i<count)
                    {
                        node=userLinkList.findUser(i);
                        if(node==null)
                        {
                            i++;
                            continue;
                        }
                        combobox.addItem(node.username);
                        i++;
                    }
                    combobox.setSelectedIndex(0);
                    textarea.append(msg);
                    textfield.setText("在线用户 "+userLinkList.getCount()+"人\n");
                    sendToAll(msg);
                    sendUserList();
                    break;
                }
            }
            catch(Exception e)
            {
               
            }
        }
    }
    public void sendUserList()
    {
        String userlist=" ";
        int count=userLinkList.getCount();
        int i=0;
        while(i<count)
        {
            Node node=userLinkList.findUser(i);
            if(node==null)
            {
                i++;
                continue;
            }
            try
            {
                node.output.writeObject("用户列表");
                node.output.flush();
                node.output.writeObject(userlist);
                node.output.flush();
            }
            catch(Exception e )
            {
               
            }
            i++;
        }
    }
    public void sendToAll(String msg)
    {
        int count=userLinkList.getCount();
        int i=0;
        while(i<count)
        {
            Node node=userLinkList.findUser(i);
            if(node==null)
            {
                i++;
                continue;
            }
            try
            {
                node.output.writeObject("聊天信息");
                node.output.flush();
                node.output.writeObject(msg);
                node.output.flush();
            }
            catch(Exception e)
            {
               
            }
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

}
搜索更多相关主题的帖子: package public import 客户端 
2016-12-27 22:09
快速回复:聊天程序客户端中,用户设置UserConf和连接用户ConnectConf函数该怎么 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024408 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved