Java写了两个奇怪的线程


import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ThreadCeShi extends JFrame{
    static Threadt t;
    static Threadt2 t2;
    JPanel timePanel=new JPanel();
    JTextField timeText=new JTextField(30);
    JLabel timeLabel=new JLabel();
    JLabel timeLabel2=new JLabel();
    ThreadCeShi() {
        setTitle("TheadTest");
        setSize(400,400);
        setVisible(true);//窗体显示
        setResizable(false);//设置窗体不可变大小
        setLayout(null);
        this.setBackground(Color.blue);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗体关闭
        validate();
        int screenHeight=(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        int screenWidth=(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();//屏幕大小
        int shellHeight=(int)getBounds().getHeight();
        int shellWidth=(int)getBounds().getWidth();//窗体大小
        if(shellHeight>screenHeight)shellHeight=screenHeight;
        if(shellWidth>screenWidth)shellWidth=screenWidth;
        this.setLocation((screenWidth-shellWidth)/2,(screenHeight-shellHeight)/2);//设置窗体居中
        add(timePanel);
        timePanel.setBounds(100,100,200,130);
        timePanel.setBackground(Color.WHITE);
        timePanel.add(timeLabel);
        timePanel.setLayout(null);
        timeLabel.setText("你好!");
        timeLabel.setFont(new Font("华文行楷",1,15));//设置字体
        timeLabel.setBounds(0,0,200,60);
        timePanel.add(timeLabel2);
        timeLabel2.setText("啊哈!");
        timeLabel2.setFont(new Font("微软雅黑",1,15));
        timeLabel2.setBounds(0,60,200,60);
        t=new Threadt();
        t2=new Threadt2();
        t.run();
        t2.run();

    }
    class Threadt implements Runnable {

        public void run() {
          while(true) {
                try{
                    //System.out.println(new Date()); //打印当前时间
                    Thread.sleep(1);//休眠1毫秒

                }catch(Throwable t){
                    t.printStackTrace();
                }
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                timeLabel.setText(df.format(new Date()));
                t2.run();
            }
        }
    }
    class Threadt2 implements Runnable {
        public void run() {
            while(true) {
                try {
                    Thread.sleep(1);
                }catch(Throwable t2) {
                    t2.printStackTrace();
                }
                SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//设置日期格式
                timeLabel2.setText(df.format(new Date()));
                t.run();
            }
        }
    }
    public static void main(String args[]) {
        new ThreadCeShi();
    }


}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值