界面设计
-
登录界面设计
- ip地址输入
- 用户名输入
示例图如下:

-
聊天界面设计
- 登录用户显示
- 聊天内容显示
- 在线用户显示
- 私聊选择
- @选择
- 聊天输入及发送
示例图如下:

界面代码实现
-
登录界面
private JFrame loginView; private JTextField ipEt, nameEt, idEt; private void displayLoginView() { // TODO Auto-generated method stub loginView = new JFrame("登录"); loginView.setLayout(new GridLayout(3, 1)); loginView.setSize(400, 230); JPanel ip = new JPanel(); JLabel label = new JLabel("ip:"); ip.add(label); ipEt = new JTextField(20); ipEt.setText("127.0.0.1"); ip.add(ipEt); loginView.add(ip); JPanel name = new JPanel(); JLabel labelName = new JLabel("姓名:"); name.add(labelName); nameEt = new JTextField(20); nameEt.setText("zyw"); name.add(nameEt); loginView.add(name); JPanel btnView = new JPanel(); JButton login = new JButton("登录"); btnView.add(login); JButton cancel = new JButton("取消"); btnView.add(cancel); loginView.add(btnView); loginView.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setWindowCenter(loginView, 400, 260, true); // 给登录和取消绑定事件 login.addActionListener(this); cancel.addActionListener(this); } -
聊天界面代码
public JTextArea smsContent = new JTextArea(23, 50); private JTextArea smsSend = new JTextArea(4, 40); public JList<String> onLineUsers = new JList<String>(); private JCheckBox isPrivateBn = new JCheckBox("私聊"); private JButton sendBn = new JButton("发送"); private void displayChatView() { JPanel bottomPanel = new JPanel(new BorderLayout()); win.add(bottomPanel, BorderLayout.SOUTH); bottomPanel.add(smsSend); JPanel btns = new JPanel(new FlowLayout(FlowLayout.LEFT)); btns.add(sendBn); btns.add(isPrivateBn); bottomPanel.add(btns, BorderLayout.EAST); smsContent.setBackground(new Color(0xdd, 0xdd, 0xdd)); win.add(new JScrollPane(smsContent), BorderLayout.CENTER); smsContent.setEditable(false); Box rightBox = new Box(BoxLayout.Y_AXIS); onLineUsers.setFixedCellWidth(120); onLineUsers.setVisibleRowCount(13); rightBox.add(new JScrollPane(onLineUsers)); win.add(rightBox, BorderLayout.EAST); win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); win.pack(); setWindowCenter(win, 650, 600, true); sendBn.addActionListener(this); } -
登录事件及聊天内容发送事件绑定
@Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub JButton btn = (JButton) e.getSource(); switch (btn.getText()) { case "登录": String ip = ipEt.getText().toString(); String name = nameEt.getText().toString(); String msg = ""; if (ip == null || !ip.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")) { msg = "请输入合法的服务端地址"; } else if (name == null || !name.matches("\\S{1,}")) { msg = "姓名必须1个字符以上"; } if (!msg.equals("")) { JOptionPane.showMessageDialog(loginView, msg); } else { try { win.setTitle(name); socket = new Socket(ip, Constants.PORT); new ClientReader(this, socket).start(); DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); dos.writeInt(1); dos.writeUTF(name.trim()); dos.flush(); loginView.dispose(); displayChatView(); } catch (IOException ioException) { // TODO: handle exception ioException.printStackTrace(); } } break; case "取消": System.exit(0); break; case "发送": String msgSend = smsSend.getText().toString(); if (!msgSend.trim().equals("")) { String selectName = onLineUsers.getSelectedValue(); int flag = 2; if (selectName != null && !selectName.equals("")) { msgSend = ("@" + selectName + "," + msgSend); if (isPrivateBn.isSelected()) { flag = 3; } } try { DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); dos.writeInt(flag); dos.writeUTF(msgSend); if (flag == 3) { dos.writeUTF(selectName.trim()); } dos.flush(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } smsSend.setText(null); System.out.println("111111"); break; default: break; } }
评论(0)
登录 后参与评论。