简体   繁体   English

计时器实用程序不会为JLabel添加新行

[英]Timer util wont add a new line for JLabel

Why is that When I user the Timer.util the JLabel wont allign with my String that I inserted in my JTextPane? 为什么当我使用Timer.util时,JLabel不会对插入JTextPane中的String起作用? But when I use the Timer it Allign. 但是,当我使用Timer Allign时。 I need the Timer.util for my database so it wont lag. 我需要为我的数据库使用Timer.util,所以它不会滞后。

Here is the image when I use the Timer swing 这是我使用计时器摆动时的图像

在此处输入图片说明

Timer t2 = new Timer(250,new ActionListener(){

        public void actionPerformed(ActionEvent arg0) {

            fetchMessageData2();
        }
    });
    t2.start();


public static void fetchMessage(JTextPane jtep,StyledDocument sd,int count  )
{

    try{

    String query = "SELECT members.username, message FROM chat JOIN members ON    chat.user_id = members.id WHERE message_id > "+count+"";
    rs = st.executeQuery(query);
    while(rs.next())
    {

        try {
            final JLabel jp = new JLabel(rs.getString("username")+ "\n");
            jp.setAlignmentY(0.75f);
            jp.setFont(new Font("arial",Font.BOLD,16));
            jtep.insertComponent(jp);
            sd.insertString(sd.getLength(), ": "+rs.getString("message")+ "\n", MainPanel.sas);
        } catch (BadLocationException e1) {

            e1.printStackTrace();
        }


        MainPanel.count++;}

    }catch(Exception ex){System.out.print(ex);}

}

Here is the image result using the Timer.util 这是使用Timer.util的图像结果

在此处输入图片说明

Timer t  = new Timer();
    t.schedule(new runnableMethods(), 0,500);

import java.util.TimerTask;


public class runnableMethods extends TimerTask{

@Override
public void run() {

    SubPanel1.checkOfflineOnline();
}
}

Don't use java.util.Timer's on the Swing event dispatch thread, the EDT, as it is not thread safe for use with Swing. 不要在Swing事件分配线程EDT上使用java.util.Timer,因为它与Swing一起使用不是线程安全的。 Either use the javax.swing.Timer or if you must use the java.util.Timer, use it on a background thread, and make sure that you make Swing calls only on the EDT. 请使用javax.swing.Timer,或者如果必须使用java.util.Timer,请在后台线程上使用它,并确保仅在EDT上进行Swing调用。 A SwingWorker would work well for this. 一个SwingWorker可以很好地解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM