使用JFrame完成动态模拟时钟的攻略可以分为以下几个步骤:
1. 导入Swing包
使用JFrame需要导入Swing包,可以在文件头添加以下代码:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Dimension;
import java.awt.BasicStroke;
import java.util.Calendar;
import java.util.Date;
2. 创建时钟面板
使用JPanel创建一个时钟面板,可以设置时钟面板的背景色、大小等属性,代码如下所示:
public class ClockPanel extends JPanel {
private int width, height;
private Date date;
public ClockPanel(int width, int height) {
this.width = width;
this.height = height;
setPreferredSize(new Dimension(width, height));
setBackground(Color.WHITE);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawClock(g);
}
private void drawClock(Graphics g) {
// 画钟面
g.setColor(Color.BLACK);
g.drawOval(10, 10, width - 20, height - 20);
g.fillOval(10, 10, width - 20, height - 20);
// 画刻度
for (int i = 0; i < 12; i++) {
double angle = Math.PI / 6 * i;
int x1 = (int)(width / 2 + (width / 2 - 40) * Math.cos(angle));
int y1 = (int)(height / 2 + (height / 2 - 40) * Math.sin(angle));
int x2 = (int)(width / 2 + (width / 2 - 20) * Math.cos(angle));
int y2 = (int)(height / 2 + (height / 2 - 20) * Math.sin(angle));
g.drawLine(x1, y1, x2, y2);
}
// 画数字
FontMetrics fm = g.getFontMetrics();
int fontHeight = fm.getHeight();
for (int i = 1; i <= 12; i++) {
double angle = Math.PI / 6 * i;
String numberStr = String.valueOf(i);
int x = (int)(width / 2 + (width / 2 - 60) * Math.cos(angle)) - fm.stringWidth(numberStr) / 2;
int y = (int)(height / 2 + (height / 2 - 60) * Math.sin(angle)) + fontHeight / 3;
g.drawString(numberStr, x, y);
}
// 画指针
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
int millis = calendar.get(Calendar.MILLISECOND);
int hourAngle = (hour % 12) * 30 + minute / 2;
int minuteAngle = minute * 6;
int secondAngle = second * 6 + millis / 600;
g.setColor(Color.RED);
g.setStroke(new BasicStroke(3));
drawPointer(g, hourAngle, 60, 5);
drawPointer(g, minuteAngle, 80, 3);
drawPointer(g, secondAngle, 100, 1);
}
private void drawPointer(Graphics g, int angle, int length, int width) {
double radians = Math.toRadians(angle - 90);
int x = (int)(width / 2 * Math.cos(radians));
int y = (int)(width / 2 * Math.sin(radians));
g.drawLine(width / 2, height / 2, width / 2 + x, height / 2 + y);
radians = Math.toRadians(angle + 90);
x = (int)(length * Math.cos(radians));
y = (int)(length * Math.sin(radians));
g.drawLine(width / 2 + x, height / 2 + y, width / 2 + x + width * x / Math.abs(x + y), height / 2 + y - width * y / Math.abs(x + y));
g.drawLine(width / 2 + x, height / 2 + y, width / 2 + x - width * x / Math.abs(x + y), height / 2 + y + width * y / Math.abs(x + y));
}
public void setDate(Date date) {
this.date = date;
repaint();
}
}
3. 创建时钟窗口
使用JFrame创建一个时钟窗口,并将时钟面板添加到窗口中,可以设置窗口的标题、大小等属性,代码如下所示:
public class ClockWindow extends JFrame {
public ClockWindow() {
setTitle("Clock");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ClockPanel clockPanel = new ClockPanel(300, 300);
add(clockPanel);
pack();
setLocationRelativeTo(null);
setVisible(true);
Timer timer = new Timer(10, e -> {
clockPanel.setDate(new Date());
});
timer.start();
}
public static void main(String[] args) {
new ClockWindow();
}
}
这段代码创建了一个时钟窗口,窗口的标题为“Clock”,并将ClockPanel添加到窗口中。使用Timer定时更新时钟的显示。在每次更新时钟的时候,调用ClockPanel的setDate方法将当前时间传入,并调用repaint方法重新绘制时钟。
这是一个使用JFrame完成动态模拟时钟的例子,可以参考并修改代码,实现不同样式的时钟效果。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:如何使用JFrame完成动态模拟时钟 - Python技术站