📄 clocks.java
字号:
import java.applet.*;
import java.awt.*;
import java.util.Date;
import java.text.DateFormat;
//This applet displays the time and updates it every second.
public class Clocks extends Applet implements Runnable
{
Label time; //A component to display the time.
DateFormat timeFormat; //This object converts time to a string.
Thread timer; //The thread that updates the time.
volatile boolean running; //A flag used to stop the thread.
public void init(){
time=new Label();
time.setFont(new Font("helvetica",Font.BOLD,12) );
time.setAlignment(Label.CENTER);
setLayout(new BorderLayout() );
add(time,BorderLayout.CENTER);
timeFormat=DateFormat.getTimeInstance(DateFormat.MEDIUM);
}
public void start(){
running=true;
if (timer==null)
{ timer=new Thread(this);
timer.start();
}
}
public void run(){
while (running)
{ time.setText(timeFormat.format(new Date() ));
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
}
}
timer=null;
}
public void stop(){
running=false;
}
public String getAppletInfo(){
return "Clock applet";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -