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,解决办法就是想办法把初始化的那个线程挂起。

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

报告相同问题?

悬赏问题

  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?