Achieve QQ chat interface with Java Swing

Java curriculum design team -socket chat room

Achieve QQ chat interface with Java Swing

Article content summary
        1.代码过程中遇到的问题以及如何解决
        2.团队项目中负责的功能展示
        3.尚未完成的功能,今后的改进和总结
        4.gitee的提交记录
  1. Gui writing problems encountered in the process

    Summary of JavaSwing layout manager
     1.在控件有布局管理器的情况下,控件是无法通过setSize来控制大小的,只有当setLayout(null)的时候setSize,setLocation,setBounds方法才有实际用处

    2.setPreferredSize take effect when there is a layout manager, the layout manager will get preferredsize space, which can take effect
    3. Use layout managers have a lot of limitations, many are not able to control the position of the right place, especially when nested controls such as when (in the JTextArea inserted image, the JScrollPane insert the JTextArea), etc., the layout manager role will become very tasteless
    4. the control methods may be setBounds x, y directly into the JFrame also can directly control the size, this approach allows a more flexible arrangement of controls

    Understanding of JavaSwing page optimization

    Swing belongs to the older set of tools to generate a page layout does not look good. I tried a lot of methods can only do simple page, far short of beautiful. I've seen better optimization is to rewrite all mouse functions like the benefits of doing so is by JTextArea instead of a Button control, override the mouse-over function, etc. can make the page look more vivid

    The following show you specific optimized code

    public void mouseDragged(MouseEvent e) {//重写窗口拖动函数
            Point p = getLocation();
            setLocation(p.x + e.getX() - origin.x, p.y + e.getY()- origin.y);
        }
    });
    user.addFocusListener(new FocusListener() {//重写鼠标焦点函数
        public void focusLost(FocusEvent e) {//失去焦点       
        }
        public void focusGained(FocusEvent e) {//得到焦点        
    });
    
    public void mousePressed(MouseEvent e){//重写鼠标点击函数
    
    }
    Front and rear ends for alternately experience problems

    The problem Gui graphical interface is a difficult alternately front and rear side code, the front end of the QQ chat interface to do real-time updates of information, when reminded of the button and information. Both realize the function of spending a lot of time and effort, these two issues explained in detail below

    Real-time update of the information displayed

    1. updated in real time into an offline message display 2. The display displays online chat message recording
    method is substantially the same three functions implemented
    ###################### 1. Show offline message #### ############################
    public static void returnOfflineMessage (the ArrayList offlineMessages) {
    for (OfflineMessage o : offlineMessages) {
    if (hashMap.containsKey(o.getSender())) {
    hashMap.get(o.getSender()).add(o);
    } else {
    ArrayList the ArrayList = new new messageArrayList <> ();
    messageArrayList.add (O);
    hashMap.put (o.getSender (), messageArrayList);
    }
    }
    }
    before the on-line, all of the information obtained by the first storage HashMap, key of the sender , value for the ArrayList
    public static void setButton()
    {
    for (int i = 0; i < userItem.size(); i++) {//将用户创建button
    if (!userItem.get(i).getUserName().equals(sender)) {
    JToggleButton jToggleButton = new JToggleButton(userItem.get(i).getUserName());
    jToggleButtons.add(jToggleButton);
    int finalI1 = i;
    jToggleButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    jEditorPane.setText("");
    receivingEnd = userItem.get(finalI1).getUserName();
    for (int i = 0; i < jToggleButtons.size(); i++) {//点击按钮后,把按钮颜色变为无
    if (userItem.get(finalI1).getUserName().equals(jToggleButtons.get(i).getText())) {
    jToggleButtons.get(i).setBackground(null);
    }
    }
    if (hashMap.containsKey(receivingEnd)) {
    for (Message M : hashMap.get(receivingEnd)) {
    if(M.getSender().equals(sender)){
    jEditorPane.append("我" + " :" + M.getMessage() + "(" + M.getTime() + ")" + "\n");
    }else {
    jEditorPane.append(M.getSender() + " :" + M.getMessage() + "(" + M.getTime() + ")" + "\n");
    }
    }
    }
    }
    });
    userListjPanel.add(jToggleButton);
    group.add(jToggleButton);
    jToggleButton.setPreferredSize (new new the Dimension (10, 10));
    }
    }
    }
    acquired UserIteam from the console, and in the UserName UserIteam dynamically create button, the button used herein jToggleButton type and use of ButtonGroup features, which can ensure a button a bit lower and lower no point in both states, and button can be only one point. After clicking Button, so receivingEnd = userItem.get (finalI1) .getUserNam
    here assignment receivingEnd, it determines which users clicked to do the identification display information, the next step to do hashmap traversal, the output of all for the issuer information button

    ############################ 2 Display online news ################# ###########
    public static void returnOnlineMessage (OnlineMessage onlineMessage) {
    IF (onlineMessage.getSender (). the equals (receivingEnd)) {
    // printed to the screen
    jEditorPane.append (onlineMessage.getSender () + " : "onlineMessage.getMessage + () +" ( "+ onlineMessage.getTime () +") "+" \ n-");
    } the else {
    // no point button changes color
    changeButton (onlineMessage);
    }
    IF (hashMap '. containsKey (onlineMessage.getSender ())) {
    HashMap.get (onlineMessage.getSender ()) the Add (onlineMessage);.
    } {the else
    the ArrayList the ArrayList = new new tempList <> ();
    tempList.add (onlineMessage);
    hashMap.put (onlineMessage.getSender (), tempList);
    }
    }
    Every time information, the information is added to the HashMap, and then determines whether the sender button, the information is printed on

     ############################ 3.显示聊天记录##########################
     public static void returnHistoryList(ArrayList<HistoryMessage> list){
             hashMap.clear();
             for (Message m:list) {
                 if(m.getSender().equals(sender)){
                     if(hashMap.containsKey(m.getReceivingEnd())){
                         hashMap.get(m.getReceivingEnd()).add(m);
                     }else{
                         ArrayList<Message> tempList = new ArrayList<>();
                         tempList.add(m);
                         hashMap.put(m.getReceivingEnd(),tempList);
                     }
                 }else{
                     if(hashMap.containsKey(m.getSender())){
                         hashMap.get(m.getSender()).add(m);
                     }else{
                         ArrayList<Message> tempList = new ArrayList<>();
                         tempList.add(m);
                         hashMap.put(m.getSender(),tempList);
                     }
                 }
             }
             jEditorPane.setText("");
             if(receivingEnd!=null){
                 for (Message M : hashMap.get(receivingEnd)) {
                     if(M.getSender().equals(sender)){
                         jEditorPane.append("我" + " :" + M.getMessage() + "(" + M.getTime() + ")" + "\n");
                     }else {
                         jEditorPane.append(M.getSender() + " :" + M.getMessage() + "(" + M.getTime() + ")" + "\n");                 }
                 }
             }
         }

    The empty HashMap, retrieve all messages from the historical background and output

    Realized button when reminded of information

    1.
    public static void changeButton(Message message) {
        /**根据信息改变Button的颜色
         *
         */
        String username = message.getSender();
        for (int i = 0; i < jToggleButtons.size(); i++) {
            if (username.equals(jToggleButtons.get(i).getText())) {
                jToggleButtons.get(i).setBackground(Color.RED);
            }
        }
    }
    如果该用户存有信息,则改变该用户的button的颜色
    2.
    public static void returnOnlineMessage(OnlineMessage onlineMessage) {
             if(onlineMessage.getSender().equals(receivingEnd)){
                 //打印到屏幕
                 jEditorPane.append(onlineMessage.getSender() + " :" + onlineMessage.getMessage() + "(" + onlineMessage.getTime() + ")" + "\n");
             }else{
                 //没点的button改变颜色
                 changeButton(onlineMessage);
             }
             if(hashMap.containsKey(onlineMessage.getSender())){
                 hashMap.get(onlineMessage.getSender()).add(onlineMessage);
             }else{
                 ArrayList<Message> tempList = new ArrayList<>();
                 tempList.add(onlineMessage);
                 hashMap.put(onlineMessage.getSender(),tempList);
             }
         }
    当有信息来时,如果点的这个Button不是发来信息的用户,则把Button变红色

    2. Demo

    3. unfinished features

    1. Add user-group chats (due to problems of time did not have time to make UI)
    2. Register and chat interface and did not have to rewrite function optimization
    3. Add the expressive function

    Need to improve and summary

    1. The first attempt to write code that cooperation, the front and rear ends of the docking was awkward, resulting in very slow progress of the code, the method needs to write and rewrite and changed a waste of time.
    2. GUI code is based on the server's hard to debug function information display, there is no way to run a separate debugging
    3. This time I do a graphical interface is very simple, just the basic functions, which are not attractive interface only landing interface reference rewrite the code of others can have a little look
    4. future process of learning Java, the programming should cooperate to try and do standardized code, write a good class should first think of ways to write to be supplemented, East can not add Seaga lead to poor readability of the code
    5. Code still have to knock, a programming language that has done the actual title set and knock project is very different by this time I feel the most class-based or knock the slow speed of the code a little bit of logic code in order for a long time

    4.gitee commit record

Guess you like

Origin www.cnblogs.com/cxxxxxx/p/12169395.html