Java 画时钟遇到的问题及处理方案
发布时间:2021-11-07 16:17:52 所属栏目:教程 来源:互联网
导读:1.不能一直得到当前的时间,导致刷新时间不变。 刚开始我自以为把int s = calendar.get(Calendar.SECOND)放到一个线程线程里再刷新就可以看到秒的变化了, 可惜结果并不是我想到那样,画面并没有任何变化。 然后我一顿乱操作,最后还是得去问老师。/(ㄒo
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))); } } (编辑:丽水站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |