java实现聊天室功能(包含全部代码,有界面),Java 聊天室

  java实现聊天室功能(包含全部代码,有界面),Java 聊天室

  本文实例为大家分享了爪哇实现聊天室界面的具体代码,供大家参考,具体内容如下

  

服务器端:

包服务器;导入Java。awt。工具包;导入Java。awt。事件。动作事件;导入Java。awt。事件。动作监听器;导入Java。awt。事件。窗口适配器;导入Java。awt。事件。窗口事件;导入Java。util。ArrayList导入Java。util。列表;导入javax。挥棒。边境工厂;导入javax。挥棒。jframe导入javax。挥棒。jbutton导入javax。挥棒。joptionpane导入javax。挥棒。jscrollpane导入javax。挥棒。jtextarea导入javax。挥棒。jlist导入javax。挥棒。ui管理器;导入javax。挥棒。unsupportedlookandfeelexception;公共类服务器框架扩展窗口实现操作侦听器{ private JButton jbt _ start私有JButton jbt _ stop私有JButton jbt _ exit private JTextArea JTA _ disMess私有JList jlt _ deputurers私有服务器服务器;公共列表字符串online _ username公共列表整数online _ username ids公共服务器框架(服务器Server){ this。服务器=服务器;online _ usernames=new ArrayList string();online _ username ids=new ArrayListInteger();请尝试{用户界面管理器。setlookandfeel(ui管理器。getsystemlookandfeelclassname());} catch(ClassNotFoundException e){ e . printstacktrace();} catch(实例化异常e){ e . printstacktrace();} catch(IllegalAccessException e){ e . printstacktrace();} catch(UnsupportedLookAndFeelException e){ e . printstacktrace();} setTitle( u670D u52a 1 u 5668 );seticon image(工具包。getdefaulttoolkit().getImage( Images/socket。jpg’);setSize(449301);setResizable(false);WinCenter.center(这个);addWindowListener(new window adapter(){ @ Override public void window closing(window event arg 0){ jbt _ exit。doclick();} });getContentPane().设置布局(空);jbt _ start=new JButton( u542F u52a 8 u670D u52a 1 u 5668 );jbt_start.setBounds(32,23,103,34);jbt _ start。addactionlistener(this);getContentPane().add(jbt _ start);jbt _ stop=new JButton( u505C u6b 62 u670D u52a 1 u 5668 );jbt_stop.s

 

  etBounds(145, 23, 103, 34);        jbt_stop.setEnabled(false);        jbt_stop.addActionListener(this);        getContentPane().add(jbt_stop);         jbt_exit = new JButton("u9000u51FAu670Du52A1u5668");        jbt_exit.setBounds(258, 23, 103, 34);        jbt_exit.addActionListener(this);        getContentPane().add(jbt_exit);         JScrollPane scrollPane = new JScrollPane();        scrollPane.setBounds(10, 64, 221, 192);        scrollPane.setWheelScrollingEnabled(true);        scrollPane.setBorder(BorderFactory.createTitledBorder("聊天消息"));        getContentPane().add(scrollPane);         jta_disMess = new JTextArea();        scrollPane.setViewportView(jta_disMess);         JScrollPane scrollPane_1 = new JScrollPane();        scrollPane_1.setBounds(258, 65, 157, 191);        scrollPane_1.setBorder(BorderFactory.createTitledBorder("在线用户"));        getContentPane().add(scrollPane_1);         jlt_disUsers = new JList();        jlt_disUsers.setVisibleRowCount(4);        scrollPane_1.setViewportView(jlt_disUsers);    }     /**     *     */    private static final long serialVersionUID = 1L;     @Override    public void actionPerformed(ActionEvent arg0) {        if (arg0.getSource() == jbt_start) {            jbt_start.setEnabled(false);            jbt_stop.setEnabled(true);            server.startServer();        }        if (arg0.getSource() == jbt_stop) {            int flag = JOptionPane.showConfirmDialog(this, "是否要停止服务器?", "",                    JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);            if (flag == JOptionPane.OK_OPTION) {                server.stopServer();                jbt_start.setEnabled(true);                jbt_stop.setEnabled(false);            }        }        if (arg0.getSource() == jbt_exit) {            if (jbt_stop.isEnabled()) {                jbt_stop.doClick();            }            server.close();        }    }     public void setDisUsers(String userNames) {        if (userNames.equals("@userlist")) {            jlt_disUsers.removeAll();            String[] user_null = new String[]{};            jlt_disUsers.setListData(user_null);        } else {            if (userNames.contains("@userlist")) {                String[] dis = userNames.split("@userlist");                String[] disUsernames = new String[dis.length / 2];                int j = 0;                for (int i = 0; i < dis.length; i++) {                    disUsernames[j++] = dis[i++];                }                jlt_disUsers.removeAll();                jlt_disUsers.setListData(disUsernames);            }            if (userNames.contains("@exit")) {                String[] dis = {};                jlt_disUsers.setListData(dis);            }        }    }     public void setDisMess(String message) {        if (message.contains("@chat")) {            int local = message.indexOf("@chat");            jta_disMess.append(message.substring(0, local) + "n");            jta_disMess.setCaretPosition(jta_disMess.getText().length());        }        if (message.contains("@exit")) {            jta_disMess.setText("");        }    }     public void setStartAndStopUnable() {        JOptionPane.showMessageDialog(this, "不能同时开启两个服务器");        jbt_start.setEnabled(false);        jbt_stop.setEnabled(false);    }}设置窗口居中代码:

  

package Server; import java.awt.Dimension;import java.awt.Toolkit;import java.awt.Window; public class WinCenter {    public static void center(Window win) {        Toolkit tkit = Toolkit.getDefaultToolkit();        Dimension sSize = tkit.getScreenSize();        Dimension wSize = win.getSize();        if (wSize.height > sSize.height) {            wSize.height = sSize.height;        }        if (wSize.width > sSize.width) {            wSize.width = sSize.width;        }        win.setLocation((sSize.width - wSize.width) / 2, (sSize.height - wSize.height) / 2);    }}

客户端:

登录界面

 

  

package Client; import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException; import java.awt.Font;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.net.Inet4Address;import java.net.UnknownHostException; import javax.swing.JTextField;import javax.swing.JButton; public class Client_enterFrame extends JFrame implements ActionListener, KeyListener {    public Client_enterFrame(Client client) {        this.client = client;        try {            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());        } catch (ClassNotFoundException e1) {            e1.printStackTrace();        } catch (InstantiationException e1) {            e1.printStackTrace();        } catch (IllegalAccessException e1) {            e1.printStackTrace();        } catch (UnsupportedLookAndFeelException e1) {            e1.printStackTrace();        }        setIconImage(Toolkit.getDefaultToolkit().getImage("Images/socket.jpg"));        setTitle("u804Au5929u5BA4");        getContentPane().setLayout(null);        setSize(296, 249);        WinCenter.center(this);        setResizable(false);        addWindowListener(new WindowAdapter() {            @Override            public void windowClosing(WindowEvent e) {                jbt_exit.doClick();            }        });         JLabel lblNewLabel = new JLabel("u7528u6237u540D");        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 14));        lblNewLabel.setBounds(23, 30, 81, 34);        getContentPane().add(lblNewLabel);         jtf_username = new JTextField();        jtf_username.addKeyListener(this);        jtf_username.setBounds(114, 30, 143, 34);        getContentPane().add(jtf_username);        jtf_username.setColumns(10);         JLabel lblNewLabel_1 = new JLabel("u670Du52A1u5668u5730u5740");        lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 14));        lblNewLabel_1.setBounds(23, 74, 81, 34);        getContentPane().add(lblNewLabel_1);         jtf_hostIp = new JTextField();        jtf_hostIp.setBounds(114, 74, 143, 34);        jtf_hostIp.addKeyListener(this);        getContentPane().add(jtf_hostIp);        try {            String ip = (String) Inet4Address.getLocalHost().getHostAddress();            jtf_hostIp.setText(ip);        } catch (UnknownHostException e) {            e.printStackTrace();        }        jtf_hostIp.setColumns(10);         JLabel lblNewLabel_2 = new JLabel("u7AEFu53E3u53F7");        lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 14));        lblNewLabel_2.setBounds(23, 118, 81, 34);        getContentPane().add(lblNewLabel_2);         jtf_hostPort = new JTextField();        jtf_hostPort.addKeyListener(this);        jtf_hostPort.setBounds(114, 118, 143, 34);        getContentPane().add(jtf_hostPort);        jtf_hostPort.setText("5000");        jtf_hostPort.setColumns(10);         jbt_enter = new JButton("u8FDBu5165u804Au5929u5BA4");        jbt_enter.addActionListener(this);        jbt_enter.addKeyListener(this);        jbt_enter.setFont(new Font("宋体", Font.PLAIN, 14));        jbt_enter.setBounds(23, 162, 108, 39);        getContentPane().add(jbt_enter);         jbt_exit = new JButton("u9000u51FAu804Au5929u5BA4");        jbt_exit.setFont(new Font("宋体", Font.PLAIN, 14));        jbt_exit.setBounds(144, 162, 113, 39);        jbt_exit.addActionListener(this);        getContentPane().add(jbt_exit);    }     /**     *     */    private static final long serialVersionUID = 1L;    private JTextField jtf_username;    private JTextField jtf_hostIp;    private JTextField jtf_hostPort;    private JButton jbt_enter;    private JButton jbt_exit;    private Client client;      @Override    public void actionPerformed(ActionEvent e) {        if (e.getSource() == jbt_exit) {            setVisible(false);            client.exitLogin();        }        if (e.getSource() == jbt_enter) {            String username = jtf_username.getText();            username.trim();            String hostIp = jtf_hostIp.getText();            hostIp.trim();            String hostPort = jtf_hostPort.getText();            hostPort.trim();            if (!username.equals("")) {                if (!hostIp.equals("")) {                    if (!hostPort.equals("")) {                        String login_mess = client.login(username, hostIp, hostPort);                        if (login_mess.equals("true")) {                            this.setVisible(false);                            client.showChatFrame(username);                        } else {                            JOptionPane.showMessageDialog(this, login_mess);                        }                    } else {                        JOptionPane.showMessageDialog(this, "服务器连接端口号不能为空!");                    }                } else {                    JOptionPane.showMessageDialog(this, "服务器地址不能为空!");                }            } else {                JOptionPane.showMessageDialog(this, "用户名不能为空!");            }        }    }     @Override    public void keyPressed(KeyEvent arg0) {        if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {            jbt_enter.doClick();        }    }     @Override    public void keyReleased(KeyEvent arg0) {    }     @Override    public void keyTyped(KeyEvent arg0) {    }}

聊天主界面

 

  

package Client; import java.awt.Font;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.util.ArrayList;import java.util.List; import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JList;import javax.swing.JOptionPane;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.ListModel;import javax.swing.ListSelectionModel;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener; public class Client_chatFrame extends JFrame implements ActionListener,        KeyListener, ListSelectionListener {    public Client_chatFrame(Client client, String title) {        this.client = client;        try {            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());        } catch (ClassNotFoundException e) {            e.printStackTrace();        } catch (InstantiationException e) { &n      

	  
	  
	  
	  
	  
	  
        

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: