Java模拟时钟与图形日历代码合并:实现日历显示在时钟下方
Java Swing时钟与日历组件整合方案
核心修改说明
- 将原日历代码的
Main类重命名为CalendarPanel,避免类名冲突 - 调整
Clock主窗口的布局逻辑,使用BorderLayout的SOUTH区域放置日历组件,天然实现时钟在上、日历在下的垂直排列效果 - 移除日历类中冗余的独立启动
main方法 - 可选:可自行调整日历组件的背景色、尺寸,与时钟风格统一
完整整合代码
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.RenderingHints; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import javax.swing.JFrame; import javax.swing.JPanel; // 时钟主窗口类 class Clock extends JFrame { private static final long serialVersionUID = 1L; private static final Color BACKGROUND_COLOR = new Color(24, 116, 205); public Clock() { // 添加上方时钟组件 ClockPanel clockPanel = new ClockPanel(); add(clockPanel, BorderLayout.CENTER); // 添加下方日历组件 CalendarPanel calendarPanel = new CalendarPanel(); calendarPanel.setDate(new Date()); add(calendarPanel, BorderLayout.SOUTH); setBackground(BACKGROUND_COLOR); setDefaultCloseOperation(EXIT_ON_CLOSE); setResizable(false); pack(); setVisible(true); } public static void main(String[] args) { new Clock(); } } // 时钟面板类 class ClockPanel extends JPanel implements Runnable { private static final long serialVersionUID = 1L; Thread t = new Thread(this); /** 时钟指针绘制坐标 */ int xHandSec, yHandSec, xHandMin, yHandMin, xHandHour, yHandHour; /** 时钟尺寸 */ private final int HORIZONTAL_SIZE = 500; private final int VERTICAL_SIZE = 500; /** 时钟指针相对长度 */ private final int secondHandLength = HORIZONTAL_SIZE / 2 - 50; private final int minuteHandLength = HORIZONTAL_SIZE / 2 - 70; private final int hourHandLength = HORIZONTAL_SIZE / 2 - 100; /** 刻度点距离时钟中心的距离 */ private final int DISTANCE_DOT_FROM_ORIGIN = HORIZONTAL_SIZE / 2 - 40; private final int DIAMETER_BIG_DOT = 8; private final int DIAMETER_SMALL_DOT = 4; private final static Color GREY_COLOR = new Color(160,160,160); public ClockPanel() { setMinimumSize(new Dimension(HORIZONTAL_SIZE, VERTICAL_SIZE)); setMaximumSize(new Dimension(HORIZONTAL_SIZE, VERTICAL_SIZE)); setPreferredSize(new Dimension(HORIZONTAL_SIZE, VERTICAL_SIZE)); setLayout(null); t.start(); } public void run(){ while(true){ try{ int currentSecond = Calendar.getInstance().get(Calendar.SECOND); int currentMinute = Calendar.getInstance().get(Calendar.MINUTE); int currentHour = Calendar.getInstance().get(Calendar.HOUR); xHandSec = minToLocation(currentSecond, secondHandLength).x; yHandSec = minToLocation(currentSecond, secondHandLength).y; xHandMin = minToLocation(currentMinute, minuteHandLength).x; yHandMin = minToLocation(currentMinute, minuteHandLength).y; xHandHour = minToLocation(currentHour * 5 + getRelativeHour(currentMinute), hourHandLength).x; yHandHour = minToLocation(currentHour * 5 + getRelativeHour(currentMinute), hourHandLength).y; repaint(); Thread.sleep(500); } catch(InterruptedException ie){ ie.printStackTrace(); } } } /** 计算时针随分钟偏移的步长 */ private int getRelativeHour(int min) { return min / 12; } protected void paintComponent(Graphics g){ Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.clearRect(0, 0, getWidth(), getHeight()); // 绘制刻度点 g2.setColor(GREY_COLOR); for (int i = 0; i < 60; i++) { Point dotCoordinates = minToLocation(i, DISTANCE_DOT_FROM_ORIGIN); g2.setColor((i <= Calendar.getInstance().get(Calendar.SECOND)) ? Color.black : GREY_COLOR); if (i % 5 == 0) { // 大刻度点 g2.fillOval(dotCoordinates.x - (DIAMETER_BIG_DOT / 2), dotCoordinates.y - (DIAMETER_BIG_DOT / 2), DIAMETER_BIG_DOT, DIAMETER_BIG_DOT); } else { // 小刻度点 g2.fillOval(dotCoordinates.x - (DIAMETER_SMALL_DOT / 2), dotCoordinates.y - (DIAMETER_SMALL_DOT / 2), DIAMETER_SMALL_DOT, DIAMETER_SMALL_DOT); } } // 绘制时钟指针 g2.setColor(Color.black); g2.drawLine(HORIZONTAL_SIZE / 2, VERTICAL_SIZE / 2, xHandSec, yHandSec); g2.drawLine(HORIZONTAL_SIZE / 2, VERTICAL_SIZE / 2, xHandMin, yHandMin); g2.drawLine(HORIZONTAL_SIZE / 2, VERTICAL_SIZE / 2, xHandHour, yHandHour); // 绘制时钟数字 g.setFont(new Font("TimesRoman", Font.BOLD, 15)); g.drawString("1", 360, 60); g.drawString("2", 440, 140); g.drawString("3", 470, 250); g.drawString("4", 440, 370); g.drawString("5", 360, 450); g.drawString("6", 244, 480); g.drawString("7", 130, 450); g.drawString("8", 50, 370); g.drawString("9", 20, 250); g.drawString("10", 50, 140); g.drawString("11", 130, 60); g.drawString("12", 244, 25); } /** 将时间步长转换为绘制坐标 */ private Point minToLocation(int timeStep, int radius) { double t = 2 * Math.PI * (timeStep-15) / 60; int x = (int)(HORIZONTAL_SIZE / 2 + radius * Math.cos(t)); int y = (int)(VERTICAL_SIZE / 2 + radius * Math.sin(t)); return new Point(x, y); } } // 日历面板类(原Main类重命名) class CalendarPanel extends JPanel { SimpleDateFormat month = new SimpleDateFormat("MMMM"); SimpleDateFormat year = new SimpleDateFormat("yyyy"); SimpleDateFormat day = new SimpleDateFormat("d"); Date date = new Date(); public CalendarPanel() { // 调整宽度和时钟一致,实现对齐效果 setPreferredSize(new Dimension(500, 280)); // 背景色和时钟统一 setBackground(new Color(24, 116, 205)); } public void setDate(Date date) { this.date = date; } public void paintComponent(Graphics g) { ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); super.paintComponent(g); g.setFont(new Font("TimesRoman", Font.PLAIN, 17)); g.setColor(Color.black); g.drawString(month.format(date).toUpperCase(), 34, 36); g.drawString(year.format(date), 235, 36); g.setFont(new Font("TimesRoman", Font.PLAIN, 10)); Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.DATE, 1); cal.add(Calendar.DATE, -cal.get(Calendar.DAY_OF_WEEK) + 1); for (int week = 0; week < 5; week++) { for (int d = 0; d < 7; d++) { g.drawString(day.format(cal.getTime()), d * 30 + 46 + 4, week * 29 + 81 + 20); cal.add(Calendar.DATE, +1); } } } }
效果验证
直接运行Clock类的main方法即可看到时钟显示在窗口上方,日历显示在窗口下方的效果。如果需要日历跨天自动更新,可在时钟的刷新线程中增加calendarPanel.repaint()逻辑。
内容的提问来源于stack exchange,提问作者Dawid
相关产品推荐
相关产品推荐

