IMISSHOT 2017-05-31 10:11 采纳率: 100%
浏览 975
已采纳

java多线程问题,求解答!!!!

本来按下“显示时间”后,TextArea应该不断刷新当前时间并且下面的字正常滚动的,为什么
我的代码按下后只是左滚动一下,右滚动呢,如图, 求解答,下面是我的代码和程序结果的图片, c币真的就剩这么多了

 import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import javax.swing.*;
import java.util.*;

public class Clock{

    public static void main(String[] args){

        new ThreadFrame();
    }
}

class ThreadFrame extends JFrame implements Runnable, ActionListener{

    private JTextArea area;
    private JPanel pl, pl2; 
    private JLabel lb, blank;
    private boolean stoptime = false;
    private JButton bt[] = new JButton[4];
    private Thread count, time, words;
    private SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 E\n a hh时mm分ss秒");
    private int sumtime;

    public ThreadFrame(){

        this.setTitle("java动态时钟");
        this.setBounds(600, 200, 250, 240);
        this.setLayout(new FlowLayout());
        area = new JTextArea(2, 25);
        area.setText("welcome to use");
        area.setFont(new Font("黑体", Font.BOLD, 12));
        pl = new JPanel(new FlowLayout());
        pl.setBackground(Color.BLUE);
        pl.add(area);
        this.add(pl);
        pl2 = new JPanel();
        pl2.setLayout(new GridLayout(2,  2, 10, 10));
        pl2.setBackground(Color.CYAN);
        //pl2.set
        this.add(pl2);
        lb = new JLabel("欢迎使用本时钟");
        this.add(lb);

        String a[] = {"显示时间", "停止显示", "倒计时", "停止计时"};
        for(int i = 0; i < a.length; i++){

            bt[i] = new JButton(a[i]);
            pl2.add(bt[i]);
            bt[i].addActionListener(this);
        }

        time = new Thread(this);
        count = new Thread(this);
        words = new Thread(this);
        this.setVisible(true);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        words.start();
    }

    public void run(){

        if(Thread.currentThread() == words){
            while(true){
                int x = lb.getBounds().x;
                int y = lb.getBounds().y;
                x+=5;
                lb.setLocation(x, y);
                if(x > 320){
                    x = 10;
                    lb.setLocation(x, y);
                }
                try{
                    words.sleep(1500); // 单位是毫秒
                }catch(InterruptedException i1){}
            }

        }
        if(Thread.currentThread() == count){
            String strd = JOptionPane.showInputDialog(this, "格式:hh:mm:ss", "倒计时", 
                    JOptionPane.PLAIN_MESSAGE);
            try{
                    sumtime = (int)((Double.parseDouble(strd.substring(0, 2)))*3600 +(Double.parseDouble
                            (strd.substring(3, 5)))*60+(Double.parseDouble(strd.substring(6, 8))));     
            }catch(NullPointerException ne){

                sumtime = 0;
            };

            while(true){
                try{

                     area.setText(Integer.toString(sumtime/3600)+":"+
                            (Integer.toString((sumtime%3600)/60)+":"+
                             (Integer.toString(sumtime-(sumtime/3600)*3600-((sumtime%3600)/60)*60))));
                     count.sleep(1000);
                }catch(InterruptedException e){}
                sumtime--;
                if(sumtime == -1)
                    return;
            }
        }

        if(Thread.currentThread() == time){
            while(true){
                String str = format.format(new Date());
                area.setText(str);
                try{
                    Thread.sleep(1000);
                }catch(InterruptedException e){}
                if(stoptime == true)
                    return;
            }
        }


    }

    public void actionPerformed(ActionEvent e){

        if(e.getSource() == bt[0]){
            if(!time.isAlive()){
                time = new Thread(this);
                sumtime = 0;
                stoptime = false;
                time.start();
            }
        }else if(e.getSource() == bt[1]){
            stoptime = true;
            area.setText("welcome to use");
        }else if(e.getSource() == bt[2]){
            if(!count.isAlive()){
                stoptime = true;
                count = new Thread(this);
                count.start();
            }   
        }else if(e.getSource() == bt[3]){
            if(count.isAlive()){
                sumtime = 0;
                area.setText("welcome to use");
            }
        }
    }

}



图片说明

  • 写回答

1条回答 默认 最新

  • 编程爱好者熊浪 2017-05-31 10:37
    关注

    你点击时间又没有停止你本身的那个现成,这样你2个线程都在操作欢迎使用本时钟对象,就造成了线程不安全了,就是有一个要加5,一个就初始化了,你可以打印下x和y,解决办法就是想办法把初始化的那个线程挂起。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 需求高精度PT100设计电路和算法
  • ¥15 单片机配网,继电器开关,广播
  • ¥60 Qcustomplot绘制实时动态曲线
  • ¥20 运用matlab画x-y图
  • ¥15 用idea运行项目,运行tomcat报错:断言失败
  • ¥15 Sqlserver查询链接服务器数据问题
  • ¥15 Bibtex4Word 引用中文文献
  • ¥20 用opencv c/c++ 转换成灰度图,然后做一下直方图均衡,输出mp4文件
  • ¥20 matlab中的双层数值积分
  • ¥50 服务器打印水晶报表问题