⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 runnabledemo.java

📁 里面所含源码是本人平时做程序的一些实例
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class RunnableDemo extends JFrame implements ActionListener,Runnable{
	JPanel pnlMain;
	JLabel lblMove;
	JButton btnControl;
	CurrentTime ct;
	Thread thdDisplay;
	Date dateDisplay;
	GregorianCalendar gcCalendar;
	String strTime,strDate;
	public RunnableDemo(){
		super(" 接口Runnable类线程演示");
		pnlMain=new JPanel(new GridLayout(2,1));
		setContentPane(pnlMain);
		lblMove=new JLabel("");
		lblMove.setForeground(Color.RED);
		btnControl=new JButton("挂起");
		btnControl.addActionListener(this);
		pnlMain.add(lblMove);
		pnlMain.add(btnControl);
		thdDisplay=new Thread(this);
		
		thdDisplay.start();
		setSize(250,150);
		setVisible(true);
		setResizable(false);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	public void actionPerformed(ActionEvent ae){
		if(ae.getActionCommand()=="挂起"){
			btnControl.setText("重启");
			thdDisplay.suspend();
		}
		if(ae.getActionCommand()=="重启"){
			btnControl.setText("挂起");
			thdDisplay.resume();	
		}
	}
	public static void main(String args[]){
		new RunnableDemo();
	}

	public void run(){
		while(true){
			displayTime();
			try{
				thdDisplay.sleep(1000);
			}
			catch(InterruptedException e){JOptionPane.showMessageDialog(null,"线程中断!");}
		}
	}
	public void displayTime(){
		dateDisplay=new Date();
		gcCalendar=new GregorianCalendar();
		gcCalendar.setTime(dateDisplay);
		strTime="当前时间:"+gcCalendar.get(Calendar.HOUR)+":"+gcCalendar.get(Calendar.MINUTE)+":"+gcCalendar.get(Calendar.SECOND);
		strDate="今天日期:"+gcCalendar.get(Calendar.YEAR)+":"+(gcCalendar.get(Calendar.MONTH)+1)+":"+gcCalendar.get(Calendar.DATE);
		lblMove.setText(strDate+"\n"+strTime);
		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -