JScrollPane最初没有响应

时间:2014-11-07 20:58:41

标签: java swing

我想用GUI创建一个聊天服务器客户端。任何新消息都将添加为JPanel。最初添加到我的JScrollPanel的消息正在顺利更新。但是,当我实现服务器和客户端以使用GUI时,添加的前几条新消息永远不会更新。消息只会在第三次添加后更新到JScrollPanel。有时,组件的添加过早结束。客户端实现runnable,因此任何新消息都将通过Thread更新到JScrollPanel。 似乎GUI没有完全初始化。

这是客户端代码



public static void main(String[] args) {
        new Thread(new MessageClient("htf0001")).start();
        MessageGUI dialog = new MessageGUI(collaID);
    }
    @Override
    public void run() {
        //loop read from server
        // The default port.
        int portNumber = 50000;
        // The default host.
        String host =  "localhost";//"54.169.62.79";        
        /*
        * Open a socket on a given host and port. Open input and output streams.
        */
        try {
            clientSocket = new Socket(host, portNumber);
            oos = new ObjectOutputStream(clientSocket.getOutputStream());
            oos.flush();
            ois = new ObjectInputStream(clientSocket.getInputStream());
            
            Staff staff = new Staff();
            sendToServer(staff.connectToMessageServer(collaID));
            sendToServer(staff.getMessageLog(collaID));
            
            while(!close){
                ArrayList<String> input = new ArrayList<String>();
                Object o = ois.readObject();
                input = (ArrayList<String>) o;
                if(input.get(0).compareTo("end")!=0){
                    for(int i=0;i<input.size();i=i+5){
                        MessageGUI.addMessage(input.get(i),input.get(i+1), input.get(i+2),
                                          input.get(i+3),input.get(i+4));
                    }
                }
                else close = true;
            }
            oos.close();
            ois.close();
            clientSocket.close();
        }
        catch (UnknownHostException uhe) {
            JOptionPane.showMessageDialog(null,uhe.getMessage());
        } 
        catch (IOException ioe) {
            JOptionPane.showMessageDialog(null,ioe.getMessage());
        } 
        catch (ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(null,ex.getMessage());
        }
    }
&#13;
&#13;
&#13;

这是在组件中添加的GUI代码部分

&#13;
&#13;
public static void addMessage(String date, String firstName, String lastName,
                            String message, String time){
        String newUser = firstName + " " + lastName;
        
        if(recentDate.compareTo(date)!=0){
            JLabel newDate = new JLabel(date);
            newDate.setHorizontalAlignment(SwingConstants.CENTER);
            addComponent(newDate,nextLine,0,3,1);
            recentDate = date;
            nextLine++;
        }
        
        if(recentUser.compareTo(newUser)==0 && recentTime.compareTo(time)==0){
            recentJTextArea.append("\n\n"+message);
        }
        else{
            if(recentUser.compareTo(newUser)==0) newUser = recentUser;
            JTextArea temp = new JTextArea();
            temp.setFocusable(false);
            temp.setBorder(BorderFactory.createTitledBorder(newUser));
            temp.setLineWrap(true);
            temp.setWrapStyleWord(true);
            temp.setEditable(false);
            temp.setText(message);
            recentJTextArea = temp;
            recentUser = newUser;

            JLabel newTime = new JLabel(time);
            newTime.setHorizontalAlignment(SwingConstants.RIGHT);
            recentTime = time;

            addComponent(temp,nextLine,0,2,1);
            nextLine = nextLine + 1;
            addComponent(newTime,nextLine,1,1,1);
            nextLine = nextLine + 1;
            
            
        }
        invokeLater(new Runnable() {
            public void run() {
                ChatLogJScrollPane.getVerticalScrollBar().setValue(ChatLogJScrollPane.getVerticalScrollBar().getMaximum());
            }
        });
    }
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

好像您正在更新(即添加聊天文本)到非GUI线程的线程上的文本区域。相反,你应该打电话给

SwingUtilities.invokeLater(new Runnable() {
  temp.setText(message);
  temp.repaint();
});

答案 1 :(得分:0)

我找到了解决方案。我需要在JScrollPanel中验证JPanel。

JPanel container
JScrollPanel(container)
SwingUtilities.invokeLater(new Runnable(){
    container.validate();
});