Windows下的j2se时钟示例
demo里原本有个applet,不过难看又没什么新意,加上了一些功能,其中利用到了JNI,dll文件的源码由于太短就不晒出来了…
/ @(#)mp77_clock.java mp77的clock模拟器j2se1.4.2版 mp77 版权所有 西北大学 韩翼 @author mp77 @version 1.00 2008/10/18 / import java.awt.; import java.awt.event.; import java.util.;
import javax.swing.; import javax.swing.event.; public class clock extends JFrame implements Runnable{ //绘图坐标集合 private int initX,initY,centerX,centerY,sX,sY,mX,mY,hX,hY; private Date trialTime; private Calendar calendar; private volatile Thread thread; //控制面版JPanel private static JPanel clockPanel; private JButton button; private JSpinner spinner; private Container container; private JSpinner.DateEditor editor;
private SpinnerDateModel model; //指针时间变量 private int seconds,minutes,hours; public clock(){ super(“mp77的clock模拟器j2se版”);
container = getContentPane(); container.setLayout(new BorderLayout());
///////////////////控制面版JPanel.start/////////////////// clockPanel = new JPanel();
JLabel label= new JLabel(“当前系统时间”);
//JSpinner显示格式 calendar = new GregorianCalendar(); calendar.getInstance(TimeZone.getTimeZone(“GMT+8”)); Date initDate = calendar.getTime(); model = new SpinnerDateModel(initDate,null,null,Calendar.YEAR); spinner= new JSpinner(model); spinner.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent changeEvent) { //挂起thread thread.suspend(); //实时更改指针坐标 seconds=model.getDate().getSeconds(); minutes=model.getDate().getMinutes(); hours=model.getDate().getHours(); repaint(); //唤醒确认button button.setEnabled(true);
} }); editor = new JSpinner.DateEditor(spinner, “ HH:mm:ss ”); spinner.setEditor(editor); //时间修改确认 button = new JButton(“ok”); button.setEnabled(false); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { clock.setTime(model.getDate().getHours(),model.getDate().getMinutes(),model.getDate().getSeconds()); //唤醒thread thread.resume(); //屏蔽确认button button.setEnabled(false); } } ); //设置JSpinner为不可编辑状态 JFormattedTextField tf =((JSpinner.DefaultEditor)spinner.getEditor()).getTextField(); tf.setEditable(false); tf.setBackground(Color.WHITE); clockPanel.add(label); clockPanel.add(spinner); clockPanel.add(button); container.add(clockPanel,BorderLayout.SOUTH); button.setBackground(Color.WHITE); getContentPane().setBackground(Color.WHITE); ///////////////////控制面版JPanel.end///////////////////
//指针初始化 trialTime = new Date(); calendar = new GregorianCalendar(); seconds=calendar.get(Calendar.SECOND); minutes=calendar.get(Calendar.MINUTE); hours=calendar.get(Calendar.HOUR); //窗体属性 setSize(300,350); setVisible(true); setResizable(false);
} //绘图 public void paint(Graphics g) {
clockPanel.setBackground(Color.WHITE); initX=45; initY=55; //表盘圆心坐标 centerX=initX+100; centerY=initY+100; //jdk1.6前在vista中存在时区bug TimeZone zone = new SimpleTimeZone(28800000,“Asia/Shanghai”); calendar.setTimeZone(zone); //指针绘图算法 sX=centerX+(int)(90Math.cos(Math.PIseconds/30-Math.PI/2)); sY=centerY+(int)(90Math.sin(Math.PIseconds/30-Math.PI/2)); mX=centerX+(int)(80Math.cos(Math.PIminutes/30-Math.PI/2+(Math.PIseconds/30)/60)); mY=centerY+(int)(80Math.sin(Math.PIminutes/30-Math.PI/2+(Math.PIseconds/30)/60)); hX=centerX+(int)(50Math.cos(Math.PIhours/6-Math.PI/2+(Math.PIminutes/30)/12)); hY=centerY+(int)(50Math.sin(Math.PIhours/6-Math.PI/2+(Math.PIminutes/30)/12));
super.paint(g); g.setColor(Color.blue); //绘制表盘和刻度 g.drawArc(initX, initY, 200, 200, 0, 360); g.drawLine(centerX,centerY,sX,sY); g.drawLine(centerX,centerY,mX,mY); g.drawLine(centerX,centerY,hX,hY); g.drawString(“12”,138,70); g.drawString(“3”,235,160); g.drawString(“6”,143,250); g.drawString(“9”,48,160); g.drawString(“1”,188,85); g.drawString(“2”,220,118); g.drawString(“4”,220,205); g.drawString(“5”,188,235); g.drawString(“7”,98,235); g.drawString(“8”,65,205); g.drawString(“10”,62,118); g.drawString(“11”,95,85); //秒针针头端红色标记 g.setColor(Color.red); g.fillArc(sX-5,sY-5,10,10,0,360); } public void run() {
Thread me = Thread.currentThread(); while (thread == me) { try { Thread.currentThread().sleep(500); } catch (InterruptedException e) { } //重新设置指针坐标 trialTime = new Date(); calendar = new GregorianCalendar(); seconds=calendar.get(Calendar.SECOND); minutes=calendar.get(Calendar.MINUTE); hours=calendar.get(Calendar.HOUR); //重绘全部控件 repaint(); //重新设置JSpinner内容 model = new SpinnerDateModel(); spinner.setModel(model); editor = new JSpinner.DateEditor(spinner, “ HH:mm:ss ”); spinner.setEditor(editor); JFormattedTextField tf =((JSpinner.DefaultEditor)spinner.getEditor()).getTextField(); tf.setEditable(false); tf.setBackground(Color.WHITE); } } public void start() { thread = new Thread(this); thread.start(); } //JNI方法函数 public static native void setTime(int hours,int minutes,int seconds); //载入setTimer.dll static { System.loadLibrary(“setTimer”); }
//入口 public static void main(String args[]) { clock application = new clock(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); application.start(); } }