?? runnabledemo.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="當(dāng)前時間:"+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);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -