效果图:
代码:
package game;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class ShowClock extends JFrame {
Container c;
JMenuBar mb;
JMenu JFile,JHelp;
JMenuItem mExit,mCopyright;
JLabel clock;
JPanel pl;
public void init(){
c=this.getContentPane();
c.setLayout(new BorderLayout());
mb=new JMenuBar();
c.add(mb,BorderLayout.NORTH);
JFile=new JMenu("文件(F)");
JHelp=new JMenu("帮助(H)");
mb.add(JFile);mb.add(JHelp);
mExit=new JMenuItem("退出");
mCopyright=new JMenuItem("版权信息");
JFile.add(mExit);JHelp.add(mCopyright);
mCopyright.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "本游戏由暗伤无痕出品", "版权声明", JOptionPane.INFORMATION_MESSAGE);
}
});
mExit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
ShowClock.this.dispose();
}
});
pl=new JPanel();pl.setLayout(null);
c.add(pl);
clock=new JLabel();
clock.setFont(new Font("黑体",Font.BOLD,15));
clock.setForeground(Color.blue);
pl.add(clock);
clock.setBounds(40,20,400,30);
ShowTime st=new ShowTime(clock);
st.start();
this.setSize(500,400);
this.setVisible(true);
}
public ShowClock(String title){
super(title);
init();
}
class ShowTime extends Thread{
JLabel clock;
public ShowTime(JLabel clock){
this.clock=clock;
}
public void run(){
while(true){
Calendar c=Calendar.getInstance();
int year=c.get(Calendar.YEAR);
int month=c.get(Calendar.MONTH);
int day=c.get(Calendar.DATE);
int hour=c.get(Calendar.HOUR_OF_DAY);
int minute=c.get(Calendar.MINUTE);
int second=c.get(Calendar.SECOND);
String s="北京时间 "+year+"年"+month+"月"+day+"日"+hour+"时"+minute+"分"+second+"秒";
clock.setText(s);
}
}
}
public static void main(String[] args) {
ShowClock sc=new ShowClock("时钟");
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。