java问题救命啊!救命啊!急急急!
我的这个程序没有显示时钟,请帮忙改正,让它显示时钟!救救我吧!谢谢!importjava.awt.*;importjava.awt.event.ActionEvent;i...
我的这个程序没有显示时钟,请帮忙改正,让它显示时钟! 救救我吧!谢谢!
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
import java.awt.Color;
import javax.swing.Timer;
import java.util.GregorianCalendar;
import java.lang.Math;
public class Clock extends JFrame implements ActionListener{
int hour,minute,second;
Clock()
{
setTitle("时钟");
setSize(400,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Timer timer=new Timer(1000,this);
timer.start();
}
public void paintComponent(Graphics g)
{
super.paintComponents(g);
setCurrentTime();
int clockRadius=(int)(Math.min(getWidth(),getHeight())*0.4);
int xCenter=getWidth()/2;
int yCenter=getHeight()/2;
g.drawOval(xCenter-clockRadius, yCenter-clockRadius, 2*clockRadius, 2*clockRadius);
g.drawString("12",xCenter-5,yCenter-clockRadius+12);
g.drawString("9", xCenter-clockRadius+3, yCenter+5);
g.drawString("3",xCenter+clockRadius-10,yCenter+3);
g.drawString("6", xCenter-3, yCenter+clockRadius-3);
int sLength=(int)(clockRadius*8);
int xSecond=(int)(xCenter+sLength*Math.sin(second*(2*Math.PI/60)));
int ySecond=(int)(yCenter-sLength*Math.cos(second*(2*Math.PI/60)));
g.setColor(Color.red);
g.drawLine(xCenter,yCenter,xSecond,ySecond);
int mLength=(int)(clockRadius*0.65);
int xMinute=(int)(xCenter+mLength*Math.sin(minute*(2*Math.PI/60)));
int yMinute=(int)(yCenter-mLength*Math.cos(minute*(2*Math.PI/60)));
g.setColor(Color.blue);
g.drawLine(xCenter,yCenter,xMinute,yMinute);
int hLength=(int)(clockRadius*0.5);
int xHour=(int)(xCenter+hLength*Math.sin((hour%12+minute/60.0)*(2*Math.PI/12)));
int yHour=(int)(yCenter-hLength*Math.cos((hour%12+minute/60.0)*(2*Math.PI/12)));
g.setColor(Color.black);
g.drawLine(xCenter,yCenter,xHour,yHour);
}
public void actionPerformed(ActionEvent e)
{
repaint();
}
public void setCurrentTime()
{
Calendar cal=new GregorianCalendar();
this.hour=cal.get(Calendar.HOUR_OF_DAY);
this.minute=cal.get(Calendar.MINUTE);
this.second=cal.get(Calendar.SECOND);
}
public static void main(String[] args) {
JFrame clock=new JFrame();
}
} 展开
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
import java.awt.Color;
import javax.swing.Timer;
import java.util.GregorianCalendar;
import java.lang.Math;
public class Clock extends JFrame implements ActionListener{
int hour,minute,second;
Clock()
{
setTitle("时钟");
setSize(400,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Timer timer=new Timer(1000,this);
timer.start();
}
public void paintComponent(Graphics g)
{
super.paintComponents(g);
setCurrentTime();
int clockRadius=(int)(Math.min(getWidth(),getHeight())*0.4);
int xCenter=getWidth()/2;
int yCenter=getHeight()/2;
g.drawOval(xCenter-clockRadius, yCenter-clockRadius, 2*clockRadius, 2*clockRadius);
g.drawString("12",xCenter-5,yCenter-clockRadius+12);
g.drawString("9", xCenter-clockRadius+3, yCenter+5);
g.drawString("3",xCenter+clockRadius-10,yCenter+3);
g.drawString("6", xCenter-3, yCenter+clockRadius-3);
int sLength=(int)(clockRadius*8);
int xSecond=(int)(xCenter+sLength*Math.sin(second*(2*Math.PI/60)));
int ySecond=(int)(yCenter-sLength*Math.cos(second*(2*Math.PI/60)));
g.setColor(Color.red);
g.drawLine(xCenter,yCenter,xSecond,ySecond);
int mLength=(int)(clockRadius*0.65);
int xMinute=(int)(xCenter+mLength*Math.sin(minute*(2*Math.PI/60)));
int yMinute=(int)(yCenter-mLength*Math.cos(minute*(2*Math.PI/60)));
g.setColor(Color.blue);
g.drawLine(xCenter,yCenter,xMinute,yMinute);
int hLength=(int)(clockRadius*0.5);
int xHour=(int)(xCenter+hLength*Math.sin((hour%12+minute/60.0)*(2*Math.PI/12)));
int yHour=(int)(yCenter-hLength*Math.cos((hour%12+minute/60.0)*(2*Math.PI/12)));
g.setColor(Color.black);
g.drawLine(xCenter,yCenter,xHour,yHour);
}
public void actionPerformed(ActionEvent e)
{
repaint();
}
public void setCurrentTime()
{
Calendar cal=new GregorianCalendar();
this.hour=cal.get(Calendar.HOUR_OF_DAY);
this.minute=cal.get(Calendar.MINUTE);
this.second=cal.get(Calendar.SECOND);
}
public static void main(String[] args) {
JFrame clock=new JFrame();
}
} 展开
2个回答
展开全部
帮你改过来了
不过改的不好,你在修改一下
但是可以运行
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
import java.awt.Color;
import javax.swing.Timer;
import java.util.GregorianCalendar;
import java.lang.Math;
public class Clock {
JFrame s;
fff f;
Clock()
{ s=new JFrame();
f=new fff();
s.add(f);
s.setTitle("时钟");
s.setSize(400,500);
s.setVisible(true);
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Clock();
}
}
class fff extends JPanel implements ActionListener
{/**
*
*/
private static final long serialVersionUID = 0L;
Timer timer;
public fff()
{
timer=new Timer(1000,(ActionListener) this);
timer.start();
}
int hour,minute,second;
public void paintComponent(Graphics g)
{
super.paintComponents(g);
setCurrentTime();
int clockRadius=(int)(Math.min(getWidth(),getHeight())*0.4);
int xCenter=getWidth()/2;
int yCenter=getHeight()/2;
g.drawOval(xCenter-clockRadius, yCenter-clockRadius, 2*clockRadius, 2*clockRadius);
g.drawString("12",xCenter-5,yCenter-clockRadius+12);
g.drawString("9", xCenter-clockRadius+3, yCenter+5);
g.drawString("3",xCenter+clockRadius-10,yCenter+3);
g.drawString("6", xCenter-3, yCenter+clockRadius-3);
int sLength=(int)(clockRadius*8);
int xSecond=(int)(xCenter+sLength*Math.sin(second*(2*Math.PI/60)));
int ySecond=(int)(yCenter-sLength*Math.cos(second*(2*Math.PI/60)));
g.setColor(Color.red);
g.drawLine(xCenter,yCenter,xSecond,ySecond);
int mLength=(int)(clockRadius*0.65);
int xMinute=(int)(xCenter+mLength*Math.sin(minute*(2*Math.PI/60)));
int yMinute=(int)(yCenter-mLength*Math.cos(minute*(2*Math.PI/60)));
g.setColor(Color.blue);
g.drawLine(xCenter,yCenter,xMinute,yMinute);
int hLength=(int)(clockRadius*0.5);
int xHour=(int)(xCenter+hLength*Math.sin((hour%12+minute/60.0)*(2*Math.PI/12)));
int yHour=(int)(yCenter-hLength*Math.cos((hour%12+minute/60.0)*(2*Math.PI/12)));
g.setColor(Color.black);
g.drawLine(xCenter,yCenter,xHour,yHour);
}
public void setCurrentTime()
{
Calendar cal=new GregorianCalendar();
this.hour=cal.get(Calendar.HOUR_OF_DAY);
this.minute=cal.get(Calendar.MINUTE);
this.second=cal.get(Calendar.SECOND);
}
public void actionPerformed(ActionEvent e)
{
repaint();
}
}
不过改的不好,你在修改一下
但是可以运行
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
import java.awt.Color;
import javax.swing.Timer;
import java.util.GregorianCalendar;
import java.lang.Math;
public class Clock {
JFrame s;
fff f;
Clock()
{ s=new JFrame();
f=new fff();
s.add(f);
s.setTitle("时钟");
s.setSize(400,500);
s.setVisible(true);
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Clock();
}
}
class fff extends JPanel implements ActionListener
{/**
*
*/
private static final long serialVersionUID = 0L;
Timer timer;
public fff()
{
timer=new Timer(1000,(ActionListener) this);
timer.start();
}
int hour,minute,second;
public void paintComponent(Graphics g)
{
super.paintComponents(g);
setCurrentTime();
int clockRadius=(int)(Math.min(getWidth(),getHeight())*0.4);
int xCenter=getWidth()/2;
int yCenter=getHeight()/2;
g.drawOval(xCenter-clockRadius, yCenter-clockRadius, 2*clockRadius, 2*clockRadius);
g.drawString("12",xCenter-5,yCenter-clockRadius+12);
g.drawString("9", xCenter-clockRadius+3, yCenter+5);
g.drawString("3",xCenter+clockRadius-10,yCenter+3);
g.drawString("6", xCenter-3, yCenter+clockRadius-3);
int sLength=(int)(clockRadius*8);
int xSecond=(int)(xCenter+sLength*Math.sin(second*(2*Math.PI/60)));
int ySecond=(int)(yCenter-sLength*Math.cos(second*(2*Math.PI/60)));
g.setColor(Color.red);
g.drawLine(xCenter,yCenter,xSecond,ySecond);
int mLength=(int)(clockRadius*0.65);
int xMinute=(int)(xCenter+mLength*Math.sin(minute*(2*Math.PI/60)));
int yMinute=(int)(yCenter-mLength*Math.cos(minute*(2*Math.PI/60)));
g.setColor(Color.blue);
g.drawLine(xCenter,yCenter,xMinute,yMinute);
int hLength=(int)(clockRadius*0.5);
int xHour=(int)(xCenter+hLength*Math.sin((hour%12+minute/60.0)*(2*Math.PI/12)));
int yHour=(int)(yCenter-hLength*Math.cos((hour%12+minute/60.0)*(2*Math.PI/12)));
g.setColor(Color.black);
g.drawLine(xCenter,yCenter,xHour,yHour);
}
public void setCurrentTime()
{
Calendar cal=new GregorianCalendar();
this.hour=cal.get(Calendar.HOUR_OF_DAY);
this.minute=cal.get(Calendar.MINUTE);
this.second=cal.get(Calendar.SECOND);
}
public void actionPerformed(ActionEvent e)
{
repaint();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询