In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
这期内容当中小编将会给大家带来有关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) 会调用组件的原始界面重新绘制,这样就相当于把上次的擦除啦。
最后把我源码附上。请各位指点!
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 画时钟遇到的问题及解决方法了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.