求一段JAVA程序的注释 (万分紧急!!!)
packageDemo;importjava.awt.*;importjavax.swing.*;importjava.util.*;importjava.awt.eve...
package Demo;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.text.*;
public class Clock extends JPanel implements Runnable{
private JTextField jt;
private JPanel jp;
public Clock(){
Thread c=new Thread(this);
c.start();
jp=new JPanel();
jt=new JTextField();
jt.setEditable(false);
jt.setHorizontalAlignment(jt.CENTER);
this.setLayout(new BorderLayout());
add(jt,BorderLayout.SOUTH);
add(jp,BorderLayout.CENTER);
}
public void run(){
while(true){
this.repaint();
try{
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}
}
}
public void paint(Graphics g){
super.paint(g);
Calendar cal=Calendar.getInstance();
DateFormat format=DateFormat.getDateTimeInstance(DateFormat.LONG,
DateFormat.LONG,this.getLocale());
jt.setText(format.format(cal.getTime()));
int min=Math.min(this.getSize().width,this.getSize().height);
int radix=(int)(min*0.8);
int kedu=(int)(radix*0.48);
int sl=(int)(radix*0.4);
int ml=(int)(sl*0.8);
int hl=(int)(ml*0.8);
int xo=this.getSize().width/2;
int yo=this.getSize().height/2;
for(int i=0;i<60;i++){
if(i%5==0){
g.drawLine((int)(kedu*0.92*(Math.sin(i*Math.PI/30))+xo),
(int)(yo-(kedu*0.92*(Math.cos(i*Math.PI/30)))),
(int)(xo+(radix/2)*(Math.sin(i*Math.PI/30))),
(int)(yo-(radix/2*(Math.cos(i*Math.PI/30)))));
if(i==0)
g.drawString(""+12,(int)(kedu*0.92*(Math.sin(i*Math.PI/30))+xo),
(int)(yo-(kedu*0.92*(Math.cos(i*Math.PI/30)))));
else
g.drawString(""+i/5,(int)(kedu*0.92*(Math.sin(i*Math.PI/30))+xo),
(int)(yo-(kedu*0.92*(Math.cos(i*Math.PI/30)))));
}
g.drawLine((int)(kedu*(Math.sin(i*Math.PI/30))+xo),
(int)(yo-(kedu*(Math.cos(i*Math.PI/30)))),
(int)(xo+(radix/2)*(Math.sin(i*Math.PI/30))),
(int)(yo-(radix/2*(Math.cos(i*Math.PI/30)))));
}
g.drawOval((this.getSize().width-radix)/2,(this.getSize().height-radix)/2,radix,radix);
Graphics2D gg=(Graphics2D)g;
gg.setColor(Color.MAGENTA);
gg.setStroke(new BasicStroke(4.5f));
gg.drawLine(xo,yo,xo+(int)(hl*Math.sin((cal.get(Calendar.MINUTE)/60.0+cal.get(Calendar.HOUR))*
Math.PI/6)),yo-(int)((hl*Math.cos((cal.get(Calendar.MINUTE)/60.0+cal.get(Calendar.HOUR))*
Math.PI/6))));
gg.setColor(Color.BLUE);
gg.setStroke(new BasicStroke(2.7f));
gg.drawLine(xo,yo,xo+(int)(ml*Math.sin(cal.get(Calendar.MINUTE)*Math.PI/30)),
yo-(int)(ml*Math.cos(cal.get(Calendar.MINUTE)*Math.PI/30)));
gg.setStroke(new BasicStroke(1.0f));
gg.setColor(Color.RED);
gg.drawLine(xo,yo,xo+(int)(sl*Math.sin(cal.get(Calendar.SECOND)*Math.PI/30)),
yo-(int)(sl*Math.cos(cal.get(Calendar.SECOND)*Math.PI/30)));
}
由于字数的限制,本人并没能把整段的代码上传!~!所以只要注释所示代码就可以了!~!万分紧急!~!!!~!望大家顶力支持!~!谢谢!~! 展开
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.text.*;
public class Clock extends JPanel implements Runnable{
private JTextField jt;
private JPanel jp;
public Clock(){
Thread c=new Thread(this);
c.start();
jp=new JPanel();
jt=new JTextField();
jt.setEditable(false);
jt.setHorizontalAlignment(jt.CENTER);
this.setLayout(new BorderLayout());
add(jt,BorderLayout.SOUTH);
add(jp,BorderLayout.CENTER);
}
public void run(){
while(true){
this.repaint();
try{
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}
}
}
public void paint(Graphics g){
super.paint(g);
Calendar cal=Calendar.getInstance();
DateFormat format=DateFormat.getDateTimeInstance(DateFormat.LONG,
DateFormat.LONG,this.getLocale());
jt.setText(format.format(cal.getTime()));
int min=Math.min(this.getSize().width,this.getSize().height);
int radix=(int)(min*0.8);
int kedu=(int)(radix*0.48);
int sl=(int)(radix*0.4);
int ml=(int)(sl*0.8);
int hl=(int)(ml*0.8);
int xo=this.getSize().width/2;
int yo=this.getSize().height/2;
for(int i=0;i<60;i++){
if(i%5==0){
g.drawLine((int)(kedu*0.92*(Math.sin(i*Math.PI/30))+xo),
(int)(yo-(kedu*0.92*(Math.cos(i*Math.PI/30)))),
(int)(xo+(radix/2)*(Math.sin(i*Math.PI/30))),
(int)(yo-(radix/2*(Math.cos(i*Math.PI/30)))));
if(i==0)
g.drawString(""+12,(int)(kedu*0.92*(Math.sin(i*Math.PI/30))+xo),
(int)(yo-(kedu*0.92*(Math.cos(i*Math.PI/30)))));
else
g.drawString(""+i/5,(int)(kedu*0.92*(Math.sin(i*Math.PI/30))+xo),
(int)(yo-(kedu*0.92*(Math.cos(i*Math.PI/30)))));
}
g.drawLine((int)(kedu*(Math.sin(i*Math.PI/30))+xo),
(int)(yo-(kedu*(Math.cos(i*Math.PI/30)))),
(int)(xo+(radix/2)*(Math.sin(i*Math.PI/30))),
(int)(yo-(radix/2*(Math.cos(i*Math.PI/30)))));
}
g.drawOval((this.getSize().width-radix)/2,(this.getSize().height-radix)/2,radix,radix);
Graphics2D gg=(Graphics2D)g;
gg.setColor(Color.MAGENTA);
gg.setStroke(new BasicStroke(4.5f));
gg.drawLine(xo,yo,xo+(int)(hl*Math.sin((cal.get(Calendar.MINUTE)/60.0+cal.get(Calendar.HOUR))*
Math.PI/6)),yo-(int)((hl*Math.cos((cal.get(Calendar.MINUTE)/60.0+cal.get(Calendar.HOUR))*
Math.PI/6))));
gg.setColor(Color.BLUE);
gg.setStroke(new BasicStroke(2.7f));
gg.drawLine(xo,yo,xo+(int)(ml*Math.sin(cal.get(Calendar.MINUTE)*Math.PI/30)),
yo-(int)(ml*Math.cos(cal.get(Calendar.MINUTE)*Math.PI/30)));
gg.setStroke(new BasicStroke(1.0f));
gg.setColor(Color.RED);
gg.drawLine(xo,yo,xo+(int)(sl*Math.sin(cal.get(Calendar.SECOND)*Math.PI/30)),
yo-(int)(sl*Math.cos(cal.get(Calendar.SECOND)*Math.PI/30)));
}
由于字数的限制,本人并没能把整段的代码上传!~!所以只要注释所示代码就可以了!~!万分紧急!~!!!~!望大家顶力支持!~!谢谢!~! 展开
2个回答
展开全部
//类名叫Clock ,继承JPanel ,实现Runnable接口(为了多线程)
public class Clock extends JPanel implements Runnable{
private JTextField jt; //文本框
private JPanel jp; //面板
public Clock(){ //构造函数
Thread c=new Thread(this);
c.start(); //这2行启动一个Clock实例的线程,这样线程会不停的执行Clock类的run()方法
jp=new JPanel();
jt=new JTextField();
jt.setEditable(false); //文本框不可输入
jt.setHorizontalAlignment(jt.CENTER); //文本框水平居中
this.setLayout(new BorderLayout());// 布局设为BorderLayout
add(jt,BorderLayout.SOUTH);
add(jp,BorderLayout.CENTER); //这2行把控件加到面板上
}
public void run(){
while(true){
this.repaint(); //不停地执行repaint方法
try{
Thread.sleep(1000); //睡眠1秒钟,给其他线程以运行的机会(这个例子其实就是每秒钟重新绘制钟)
}
catch(Exception e){
e.printStackTrace(); //线程出错的话要catch
}
}
}
public void paint(Graphics g){
super.paint(g); //调用父类的paint方法
Calendar cal=Calendar.getInstance(); //获得现在的日期时间
DateFormat format=DateFormat.getDateTimeInstance(DateFormat.LONG,
DateFormat.LONG,this.getLocale()); //构造一个日期格式
jt.setText(format.format(cal.getTime())); //把格式化后的日期时间显示到文本框中
int min=Math.min(this.getSize().width,this.getSize().height);//取宽度,高度2者较小值
int radix=(int)(min*0.8); //基数
int kedu=(int)(radix*0.48); //刻度
int sl=(int)(radix*0.4); //秒针
int ml=(int)(sl*0.8); //分针
int hl=(int)(ml*0.8); //时针
int xo=this.getSize().width/2;
int yo=this.getSize().height/2; //这2行算出中心点
//以下用了数学公式sin cos计算并绘画钟的样子
for(int i=0;i<60;i++){
if(i%5==0){
g.drawLine((int)(kedu*0.92*(Math.sin(i*Math.PI/30))+xo),
(int)(yo-(kedu*0.92*(Math.cos(i*Math.PI/30)))),
(int)(xo+(radix/2)*(Math.sin(i*Math.PI/30))),
(int)(yo-(radix/2*(Math.cos(i*Math.PI/30))))); //drawLine方法意思是画线
if(i==0)
g.drawString(""+12,(int)(kedu*0.92*(Math.sin(i*Math.PI/30))+xo), //drawString画字符串
(int)(yo-(kedu*0.92*(Math.cos(i*Math.PI/30)))));
else
g.drawString(""+i/5,(int)(kedu*0.92*(Math.sin(i*Math.PI/30))+xo),
(int)(yo-(kedu*0.92*(Math.cos(i*Math.PI/30)))));
}
g.drawLine((int)(kedu*(Math.sin(i*Math.PI/30))+xo),
(int)(yo-(kedu*(Math.cos(i*Math.PI/30)))),
(int)(xo+(radix/2)*(Math.sin(i*Math.PI/30))),
(int)(yo-(radix/2*(Math.cos(i*Math.PI/30)))));
}
g.drawOval((this.getSize().width-radix)/2,(this.getSize().height-radix)/2,radix,radix); //drawOval画一个圆,即模仿钟的外圈样子
Graphics2D gg=(Graphics2D)g;
gg.setColor(Color.MAGENTA); //画笔设为洋红色
gg.setStroke(new BasicStroke(4.5f)); //画笔线条粗细
gg.drawLine(xo,yo,xo+(int)(hl*Math.sin((cal.get(Calendar.MINUTE)/60.0+cal.get(Calendar.HOUR))* Math.PI/6)),yo-(int)((hl*Math.cos((cal.get(Calendar.MINUTE)/60.0+cal.get(Calendar.HOUR))*
Math.PI/6)))); //时针
gg.setColor(Color.BLUE); //蓝色
gg.setStroke(new BasicStroke(2.7f));
gg.drawLine(xo,yo,xo+(int)(ml*Math.sin(cal.get(Calendar.MINUTE)*Math.PI/30)),
yo-(int)(ml*Math.cos(cal.get(Calendar.MINUTE)*Math.PI/30))); //分针
gg.setStroke(new BasicStroke(1.0f));
gg.setColor(Color.RED); //红色
gg.drawLine(xo,yo,xo+(int)(sl*Math.sin(cal.get(Calendar.SECOND)*Math.PI/30)),
yo-(int)(sl*Math.cos(cal.get(Calendar.SECOND)*Math.PI/30))); //秒针
}
public class Clock extends JPanel implements Runnable{
private JTextField jt; //文本框
private JPanel jp; //面板
public Clock(){ //构造函数
Thread c=new Thread(this);
c.start(); //这2行启动一个Clock实例的线程,这样线程会不停的执行Clock类的run()方法
jp=new JPanel();
jt=new JTextField();
jt.setEditable(false); //文本框不可输入
jt.setHorizontalAlignment(jt.CENTER); //文本框水平居中
this.setLayout(new BorderLayout());// 布局设为BorderLayout
add(jt,BorderLayout.SOUTH);
add(jp,BorderLayout.CENTER); //这2行把控件加到面板上
}
public void run(){
while(true){
this.repaint(); //不停地执行repaint方法
try{
Thread.sleep(1000); //睡眠1秒钟,给其他线程以运行的机会(这个例子其实就是每秒钟重新绘制钟)
}
catch(Exception e){
e.printStackTrace(); //线程出错的话要catch
}
}
}
public void paint(Graphics g){
super.paint(g); //调用父类的paint方法
Calendar cal=Calendar.getInstance(); //获得现在的日期时间
DateFormat format=DateFormat.getDateTimeInstance(DateFormat.LONG,
DateFormat.LONG,this.getLocale()); //构造一个日期格式
jt.setText(format.format(cal.getTime())); //把格式化后的日期时间显示到文本框中
int min=Math.min(this.getSize().width,this.getSize().height);//取宽度,高度2者较小值
int radix=(int)(min*0.8); //基数
int kedu=(int)(radix*0.48); //刻度
int sl=(int)(radix*0.4); //秒针
int ml=(int)(sl*0.8); //分针
int hl=(int)(ml*0.8); //时针
int xo=this.getSize().width/2;
int yo=this.getSize().height/2; //这2行算出中心点
//以下用了数学公式sin cos计算并绘画钟的样子
for(int i=0;i<60;i++){
if(i%5==0){
g.drawLine((int)(kedu*0.92*(Math.sin(i*Math.PI/30))+xo),
(int)(yo-(kedu*0.92*(Math.cos(i*Math.PI/30)))),
(int)(xo+(radix/2)*(Math.sin(i*Math.PI/30))),
(int)(yo-(radix/2*(Math.cos(i*Math.PI/30))))); //drawLine方法意思是画线
if(i==0)
g.drawString(""+12,(int)(kedu*0.92*(Math.sin(i*Math.PI/30))+xo), //drawString画字符串
(int)(yo-(kedu*0.92*(Math.cos(i*Math.PI/30)))));
else
g.drawString(""+i/5,(int)(kedu*0.92*(Math.sin(i*Math.PI/30))+xo),
(int)(yo-(kedu*0.92*(Math.cos(i*Math.PI/30)))));
}
g.drawLine((int)(kedu*(Math.sin(i*Math.PI/30))+xo),
(int)(yo-(kedu*(Math.cos(i*Math.PI/30)))),
(int)(xo+(radix/2)*(Math.sin(i*Math.PI/30))),
(int)(yo-(radix/2*(Math.cos(i*Math.PI/30)))));
}
g.drawOval((this.getSize().width-radix)/2,(this.getSize().height-radix)/2,radix,radix); //drawOval画一个圆,即模仿钟的外圈样子
Graphics2D gg=(Graphics2D)g;
gg.setColor(Color.MAGENTA); //画笔设为洋红色
gg.setStroke(new BasicStroke(4.5f)); //画笔线条粗细
gg.drawLine(xo,yo,xo+(int)(hl*Math.sin((cal.get(Calendar.MINUTE)/60.0+cal.get(Calendar.HOUR))* Math.PI/6)),yo-(int)((hl*Math.cos((cal.get(Calendar.MINUTE)/60.0+cal.get(Calendar.HOUR))*
Math.PI/6)))); //时针
gg.setColor(Color.BLUE); //蓝色
gg.setStroke(new BasicStroke(2.7f));
gg.drawLine(xo,yo,xo+(int)(ml*Math.sin(cal.get(Calendar.MINUTE)*Math.PI/30)),
yo-(int)(ml*Math.cos(cal.get(Calendar.MINUTE)*Math.PI/30))); //分针
gg.setStroke(new BasicStroke(1.0f));
gg.setColor(Color.RED); //红色
gg.drawLine(xo,yo,xo+(int)(sl*Math.sin(cal.get(Calendar.SECOND)*Math.PI/30)),
yo-(int)(sl*Math.cos(cal.get(Calendar.SECOND)*Math.PI/30))); //秒针
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询