好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java 画时钟遇到的问题及解决方案

1.不能一直得到当前的时间,导致刷新时间不变。

刚开始我自以为把int s = calendar.get(calendar.second)放到一个线程线程里再刷新就可以看到秒的变化了,

可惜结果并不是我想到那样,画面并没有任何变化。

然后我一顿乱操作,最后还是得去问老师。/(ㄒoㄒ)/~~ /(ㄒoㄒ)/~~

让我知道了还有时间监听器这个东西,看来我知道还是太少啦 (;′⌒`)

加入下列代码就行啦

2.piant方法里

g.drawstring("s",x,y)刷新的时候后一个数字覆盖前一个数字,假如秒从0开始,下一秒的1会直接画在0上,原来的0不会消失,drawline()也是如此。如下图

为什么会有这个问题呢?

因为你之前画上去的没有擦掉,所以全混在一起

然后咋解决呢?

想到用g.clearrect()去解决,发现位置有点难调。/(ㄒoㄒ)/~~

调了半天然后发现直接覆盖了连内容都不显示了 /(ㄒoㄒ)/~~ 不知道是不是我的用法有错 ≧ ﹏ ≦

到这里我只能去找度娘求助。

找到了一个好办法 上图:

也就是图上两行代码。

调用super.paintcomponent(g) 问题就被解决了

super.paintcomponent(g) 会调用组件的原始界面重新绘制,这样就相当于把上次的擦除啦。

最后把我源码附上。请各位指点!

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

import javax.swing.*;

import java.awt.*;

import java.awt.event.actionevent;

import java.awt.event.actionlistener;

import java.net.http.websocket;

import java.net.http.websocket.listener;

import java.util.calendar;

import java.util.date;

 

public class clockframe extends jframe {

     clockframe() {

         setresizable( false );

         setdefaultcloseoperation(jframe.exit_on_close);

         setsize( 400 , 400 );

         setlocationrelativeto( null );

     }

 

     public static void main(string[] args) {

         clockframe frame = new clockframe();

         clockpanel panel = new clockpanel();

         frame.add(panel);

         panel.setcalendar();

         frame.setvisible( true );

     }

}

 

 

class clockpanel extends jpanel {

     //        画h:m:s;

     calendar calendar = calendar.getinstance();

     int s = calendar.get(calendar.second);

     int m = calendar.get(calendar.minute);

     int h = calendar.get(calendar.hour);

 

     public void setcalendar() {

         timer timer = new timer( 1000 , new listener());

         timer.start();

     }

     class listener implements actionlistener {

         @override

         public void actionperformed(actionevent e) {

             s++;

             if (s == 60 ) {

                 m++;

                 s = 0 ;

             }

             if (m == 60 ) {

                 h++;

                 m = 0 ;

             }

             repaint();

         }

     }

 

     //run 动态

     int sx = 190 ;

     int sy = 70 ;

 

     clockpanel() {

         setbackground(color.gray);

     }

 

     public void paint(graphics g) {

         super .paintcomponent(g);

//        圆

         g.drawoval( 83 , 57 , 220 , 220 );

 

         //画点

         g.filloval( 190 , 170 , 5 , 5 ); //center

         for ( int i = 12 ; i > 0 ; i--) {

             g.filloval(( int ) ( 190 + 100 * math.sin(math.pi / 6 * i)), ( int ) ( 170 + 100 * math.cos(math.pi / 6 * i)), 5 , 5 );

             string time = i + "" ;

             g.setfont( new font( "微软雅黑" , font.bold, 15 ));

             g.drawstring(time, ( int ) ( 190 + math.sin(math.pi / 6 * i) * 100 ), ( int ) ( 170 - math.cos(math.pi / 6 * i) * 100 ));

         }

         g.setfont( new font( "微软雅黑" , font.bold, 20 ));

         string s = s > 10 ? s + "" : "0" + s;

         string m = m > 10 ? m + "" : "0" + m;

         string h = h > 10 ? h + "" : "0" + h;

         g.drawstring( "现在是中国时间" + h + ":" + m + ":" + s, 80 , 40 );

 

         final basicstroke stokeline = new basicstroke( 3 .0f);

         graphics2d g2d = (graphics2d) g;

         g2d.setstroke(stokeline);

         g.setcolor(color.black);

         g.drawline( 190 , 170 , ( int ) ( 190 + (math.cos((s * math.pi / 30 ) - math.pi / 2 ) * 100 )), ( int ) ( 170 + (math.sin((s * math.pi / 30 - math.pi / 2 )) * 100 )));

         g.setcolor(color.blue);

         g.drawline( 190 , 170 , ( int ) ( 190 + (math.cos((m * math.pi / 30 - math.pi / 2 )) * 100 )), ( int ) ( 170 + (math.sin((m * math.pi / 30 - math.pi / 2 )) * 100 )));

         g.setcolor(color.green);

         g.drawline( 190 , 170 , ( int ) ( 190 + (math.cos((h * math.pi / 12 )) * 100 )), ( int ) ( 170 + ((math.sin(h * math.pi / 12 )) * 100 )));

     }

}

到此这篇关于java 画时钟遇到的问题及解决方案的文章就介绍到这了,更多相关java 画时钟内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/qq_61672548/article/details/121124487

查看更多关于Java 画时钟遇到的问题及解决方案的详细内容...

  阅读:19次