学校搞课程设计,做了一个Clock,
可是不知道应该如何实现对系统时间的修改……
周末就要交程序了……
请各位高手帮个忙啊……
谢谢了…………
修改时间已经实现了,
修改过的程序段如下:
class ChangeTime{
ChangeTime(int hou,int min){
String timeString=new String("cmd /c time "+hou+":"+min+":0");
try{java.lang.Runtime.getRuntime().exec(timeString);
}catch (java.io.IOException e) {e.printStackTrace();}
}
}
但是在修改过时间后有时会出现表盘上显示时间不刷新的问题,
我用的是javax.swing.Timer timer=new javax.swing.Timer(1000,new TimeChanged());
实现1秒刷新一次
和Calendar timeNow = Calendar.getInstance();
获取当前时间
附:源程序(我才学JAVA可能编程风格有点乱请谅解)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
public class ClockPart extends JFrame{
//控件和变量的定义
int sec,min,hou;
String strSec,strMin,strHou;
String ap;
Calendar timeNow = Calendar.getInstance();
javax.swing.Timer timer=new javax.swing.Timer(1000,new TimeChanged());
JTextField timeShow=new JTextField(7);
JPanel numberClock=new JPanel();
JButton changeTime=new JButton("修改");
JLabel label = new JLabel("系统时间:",JLabel.RIGHT);
Font font=new Font("宋体",Font.BOLD, 15);
DrawClock drawClock=new DrawClock();
//构造方法
ClockPart(){
//组件初始设置
this.setTitle("时钟啊时钟");
this.setSize(350,350);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//组件初始设置
label.setFont(new Font("仿宋_GB2312",Font.BOLD,20));
changeTime.setFont(font);
changeTime.addActionListener(new ChangeTimeAction());
timeShow.setEditable(false);
//数字式时钟面板的组件添加
numberClock.add(label);
numberClock.add(timeShow);
numberClock.add(changeTime);
//添加控件到面版
this.getContentPane().add("Center",drawClock);
this.getContentPane().add("South",numberClock);
//设置初始显示时间
GetTime t=new GetTime(timeShow);
this.setVisible(true);
//时间刷新的实现
timer.start();
}
//指针式时钟布局
class DrawClock extends JPanel{
int xCenter=350/2;int yCenter=120;
int r=100;
int length=100;
int xSec,ySec,xSec1,ySec1;
int xMin1,yMin1,xMin2,yMin2,xMin3,yMin3,xMin4,yMin4;
int xHou1,yHou1,xHou2,yHou2,xHou3,yHou3,xHou4,yHou4;
int i;
Polygon pClock = new Polygon();
Polygon pMin = new Polygon();
Polygon pHou = new Polygon();
public void paint(Graphics g){
//指针位置的确定
xSec=(int)(xCenter+length*0.68*Math.sin((180-sec*6)*Math.PI/180));
ySec=(int)(yCenter+length*0.68*Math.cos((180-sec*6)*Math.PI/180));
xSec1=(int)(xCenter-length*0.15*Math.sin((180-sec*6)*Math.PI/180));
ySec1=(int)(yCenter-length*0.15*Math.cos((180-sec*6)*Math.PI/180));
xMin1=(int)(xCenter+length*0.6*Math.sin((180-min*6)*Math.PI/180));
yMin1=(int)(yCenter+length*0.6*Math.cos((180-min*6)*Math.PI/180));
xMin2=(int)(xCenter-length*0.04*Math.sin((180-min*6)*Math.PI/180));
yMin2=(int)(yCenter-length*0.04*Math.cos((180-min*6)*Math.PI/180));
xMin3=(int)(xCenter-length*0.04*Math.sin((90-min*6)*Math.PI/180));
yMin3=(int)(yCenter-length*0.04*Math.cos((90-min*6)*Math.PI/180));
xMin4=(int)(xCenter+length*0.04*Math.sin((90-min*6)*Math.PI/180));
yMin4=(int)(yCenter+length*0.04*Math.cos((90-min*6)*Math.PI/180));
xHou1=(int)(xCenter+length*0.4*Math.sin((180-hou*30-min/2)*Math.PI/180));
yHou1=(int)(yCenter+length*0.4*Math.cos((180-hou*30-min/2)*Math.PI/180));
xHou2=(int)(xCenter-length*0.05*Math.sin((180-hou*30-min/2)*Math.PI/180));
yHou2=(int)(yCenter-length*0.05*Math.cos((180-hou*30-min/2)*Math.PI/180));
xHou3=(int)(xCenter-length*0.05*Math.sin((90-hou*30-min/2)*Math.PI/180));
yHou3=(int)(yCenter-length*0.05*Math.cos((90-hou*30-min/2)*Math.PI/180));
xHou4=(int)(xCenter+length*0.05*Math.sin((90-hou*30-min/2)*Math.PI/180));
yHou4=(int)(yCenter+length*0.05*Math.cos((90-hou*30-min/2)*Math.PI/180));
//表盘的显示
//用for循环实现有一定厚度的边框(暂时没有更好的方法)
for (i=1;i<13;i++){
pClock.reset();
g.setColor(new Color(100,255-i*20,255));
pClock.addPoint(xCenter,yCenter+r+i);
pClock.addPoint(xCenter+r+i,yCenter);
pClock.addPoint(xCenter,yCenter-r-i);
pClock.addPoint(xCenter-r-i,yCenter);
g.drawPolygon(pClock);
}
//字符颜色随时间变换
g.setColor(new Color(255,0,10));
g.setFont(font);
g.drawString("6",xCenter-3,yCenter+r-7);
g.drawString("3",xCenter+r-20,yCenter+4);
g.drawString("12",xCenter-9,yCenter-r+20);
g.drawString("9",xCenter-r+15,yCenter+4);
g.setColor(new Color(255,240-sec*4,0));
g.drawString("铭牌",xCenter-15,yCenter-r+60);
//指针的显示
g.setColor(Color.BLACK);
g.drawLine(xCenter,yCenter,xSec,ySec);
g.drawLine(xCenter,yCenter,xSec1,ySec1);
g.setColor(new Color(255,0,100));
pMin.reset();
pMin.addPoint(xMin1,yMin1);
pMin.addPoint(xMin3,yMin3);
pMin.addPoint(xMin2,yMin2);
pMin.addPoint(xMin4,yMin4);
g.fillPolygon(pMin);
g.setColor(new Color(100,255,0));
pHou.reset();
pHou.addPoint(xHou1,yHou1);
pHou.addPoint(xHou3,yHou3);
pHou.addPoint(xHou2,yHou2);
pHou.addPoint(xHou4,yHou4);
g.fillPolygon(pHou);
}
}
//监视器:获取当前时间
class TimeChanged implements ActionListener{
public void actionPerformed(ActionEvent e){
GetTime t=new GetTime(timeShow);}
}
//监视器:时间修改按钮
class ChangeTimeAction implements ActionListener{
int i,newTime,newHou,newMin;
char judgementChar;
public void actionPerformed(ActionEvent e){
//新的时间的输入
String getTime=JOptionPane.showInputDialog(
drawClock,"请输入时间","TTMM");
repaint();
judge:if(getTime!=null){
//判断输入的是不是4位字符
if(getTime.length()!=4){JOptionPane.showMessageDialog(
drawClock,"格式错误\n请按格式输入4位数字","Error",JOptionPane.ERROR_MESSAGE);
repaint();
break judge;
}
//判断输入的是不是数字
for(i=0;i<(getTime.length());i++){
judgementChar=getTime.charAt(i);
//判断当前字符,如果不是数字则跳出该事件
if(!Character.isDigit(judgementChar)){
JOptionPane.showMessageDialog(
drawClock,"格式错误\n请按格式输入:TTMM","Error",JOptionPane.ERROR_MESSAGE);
repaint();break judge;
}
}
//赋值新的小时和分钟变量newHou和newMin
int newTime = Integer.parseInt(getTime);
newHou=(int)(newTime/100);
newMin=newTime-newHou*100;
if( newHou>=24||newHou<0){JOptionPane.showMessageDialog(
drawClock,"格式错误\n小时应该是小于23的正数","Error",JOptionPane.ERROR_MESSAGE);
repaint();break judge;
}
if( newMin>=60||newHou<0){JOptionPane.showMessageDialog(
drawClock,"格式错误\n分钟应该是小于60的正数","Error",JOptionPane.ERROR_MESSAGE);
repaint();break judge;
}
//修改时间的实现
new ChangeTime(newHou,newMin);
}
}
}
//类:获取当前时间
class GetTime{
//将当前时间显示在JTextField上
GetTime(JTextField tf){
timeNow = Calendar.getInstance();
sec=timeNow.get(GregorianCalendar.SECOND);
min=timeNow.get(GregorianCalendar.MINUTE);
hou=timeNow.get(GregorianCalendar.HOUR);
if(timeNow.get(GregorianCalendar.AM_PM) == 1){ap = "PM";}
else{ap = "AM";}
//当时间参数小于10的时候在前面添加字符0
if(sec < 10){
strSec="0"+sec;}
else {
strSec=""+sec;}
if(min < 10){
strMin="0"+min;}
else {
strMin=""+min;}
if(hou < 10){
strHou="0"+hou;}
else {
strHou=""+hou;}
tf.setText(ap+strHou+" :"+strMin+" :"+strSec);
repaint();
}
}
//类:修改时间
class ChangeTime{
ChangeTime(int hou,int min){
String timeString=new String("cmd /c time "+hou+":"+min+":0");
try {
java.lang.Runtime.getRuntime().exec(timeString);
}catch (java.io.IOException e) {e.printStackTrace();}
timer.start();
}
}
public static void main(String[] args){
ClockPart cp=new ClockPart();
}
}