java-利用Java多线程机制实现带滚动字幕的时钟

在这里插入图片描述
交作业的大佬记得把后三个命令删了这个太独特查重
完成后就像上面一样,命令暂时就只有四个 输入空格用空格隔开 分别是:
change ***把滚动字幕显示成***

up滚动字幕向上移动

down有了up肯定得有down
color red字母颜色换成红色 一共可以换 red black white就写了这三个

获取时间的代码

//时间戳获取时间
				Date date=new Date(System.currentTimeMillis());
				SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");//设置日期格式
				String dates = df.format(date);// new Date()为获取当前系统时间,也可使用当前时间戳

布局方面代码

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setForeground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		//时间lab
		JLabel timeLab = new JLabel("显示时间:");
		timeLab.setBounds(63, 10, 87, 15);
		contentPane.add(timeLab);
		
		//时间textfiled
		timeField = new JTextField();
		timeField.setBounds(160, 7, 153, 21);
		timeField.setEditable(false);
		contentPane.add(timeField);
		timeField.setColumns(10);
		
		//命令lab
		staticLab = new JLabel("命令:");
		staticLab.setBounds(63, 58, 54, 15);
		contentPane.add(staticLab);
		
		//命令textfile
		cmdFieled = new JTextField();
		cmdFieled.setBounds(160, 55, 153, 21);
		contentPane.add(cmdFieled);
		cmdFieled.setColumns(10);

		label = new JLabel("滚动字幕");
		label.setForeground(Color.BLACK);
		label.setBounds(148, 141, 112, 15);
		contentPane.add(label);

只有一个监听 命令文本框监听
这里采用匿名内部类

cmdFieled.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				String cmd=cmdFieled.getText();
				//分割字符串确认命令
				String[] a=cmd.split(" ");
				if(a[0].equals("change")) {
					label.setText(a[1]);
				}else if(a[0].equals("up")){
					int y=label.getY();
					int x=label.getX();
					y-=10;
					label.setBounds(x, y, 54, 15);
				}else if(a[0].equals("down")) {
					int y=label.getY();
					int x=label.getX();
					y+=10;
					label.setBounds(x, y, 54, 15);
				}else if(a[0].equals("color")){
					if(a[1].equals("white"))
						label.setForeground(Color.WHITE);
					else if(a[1].equals("black"))
						label.setForeground(Color.BLACK);
					else if(a[1].equals("red"))
						label.setForeground(Color.RED);
				}
				cmdFieled.setText("");
			}
		});

然后就是线程类

class Stick implements Runnable{
	Thread times,labmove;
	Colock frame=new Colock();
	//构造方法
	public Stick(Colock frame) {
		times=new Thread(this);
		labmove=new Thread(this);
		this.frame=frame;
	}
	//启动线程
	public void startThread() {
		times.start();
		labmove.start();
	}
	//线程主体 
	public void run() {
		//时间线程
		if(Thread.currentThread()==times) {
			while(true) {
				//时间戳获取时间
				Date date=new Date(System.currentTimeMillis());
				SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");//设置日期格式
				String dates = df.format(date);// new Date()为获取当前系统时间,也可使用当前时间戳
				frame.timeField.setText("现在的时间是:"+dates);
			}
		}
		//滚动字幕线程
		if(Thread.currentThread()==labmove) {
			while(true) {//获取滚动字幕位置,然后x坐标+10,到边缘就置0
				int y=frame.label.getY();
				int x=frame.label.getX();
				if(x>=423)
					x=0;
				else
					x+=10;
				frame.label.setBounds(x,y, 112, 15);
				try {
					labmove.sleep(100);//每一次移动100毫秒
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}

最后是main方法

public static void main(String[] args) {
		//创建窗口
		Colock fram=new Colock();
		fram.setVisible(true);
		//启动线程传入窗口
		Stick Thread=new Stick(fram);
		Thread.startThread();
	}
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值