保持TextArea更新的最佳方法?

时间:2016-02-10 13:27:21

标签: java swing io

我正在尝试编写一个简单的聊天应用程序并将消息记录在文件中。目前我正在使用Swing进行GUI。我希望以前的消息显示在TextArea中,但我只知道如何从文件中读取一次,如下所示:

     public static void main(String[] a) throws Exception {

    //some data initilzation for the sample
    Date d1 = new Date(), d2 = new Date();
    d1.setYear(d1.getYear() - 1); //Just for the sample, replace it with your dates
    d1.setMonth(d1.getMonth() + 5);//Just for the sample, replace it with your dates

    Calendar calendar1 = Calendar.getInstance();
    calendar1.setTime(d1);
    int year1 = calendar1.get(Calendar.YEAR);
    int month1 = calendar1.get(Calendar.MONTH) + 1;

    Calendar calendar2 = Calendar.getInstance();
    calendar2.setTime(d2);
    int year2 = calendar2.get(Calendar.YEAR);
    int month2 = calendar2.get(Calendar.MONTH) + 1;

    int yearDiff = year2 - year1;
    int monthDiff = month2 - month1;

    //Logic, in case d1  Jul 2015 and d2 Feb 2016, the need to reduce one year and put the remaining months on monthDiff value
    if (monthDiff < 0) {
        yearDiff = yearDiff - 1;
        monthDiff = monthDiff + 12;
    }

    System.out.println(" " + d1);
    System.out.println(" " + d2);

    System.out.println("yearDiff " + yearDiff + "     " + "monthsDiff " + monthDiff);

}

保持最新状态的最佳方法是什么?我应该使用定时器还是有更好的方法?

提前谢谢你们!

0 个答案:

没有答案