用Java绘制方形电子表
时间: 2024-05-16 10:18:17 浏览: 10
你可以使用Java提供的Graphics类和JFrame类来绘制方形电子表。
以下是一个简单的示例代码:
```java
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SquareClock extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
private int hours, minutes, seconds;
public SquareClock() {
Thread t = new Thread(this);
t.start();
}
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
updateTime();
repaint();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
int squareSize = Math.min(width, height) - 20;
int x = (width - squareSize) / 2;
int y = (height - squareSize) / 2;
// 绘制方形边框
g.setColor(Color.BLACK);
g.drawRect(x, y, squareSize, squareSize);
// 绘制小时数
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString(String.format("%02d", hours), x + 10, y + 30);
// 绘制冒号
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString(":", x + squareSize / 2 - 10, y + 30);
// 绘制分钟数
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString(String.format("%02d", minutes), x + squareSize / 2 + 10, y + 30);
// 绘制冒号
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString(":", x + squareSize / 2 + 40, y + 30);
// 绘制秒数
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString(String.format("%02d", seconds), x + squareSize / 2 + 50, y + 30);
}
private void updateTime() {
long millis = System.currentTimeMillis();
seconds = (int) (millis / 1000 % 60);
minutes = (int) (millis / (60 * 1000) % 60);
hours = (int) (millis / (60 * 60 * 1000) % 24);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.add(new SquareClock());
frame.setVisible(true);
}
}
```
这个示例代码创建一个继承自JPanel的SquareClock类。在构造函数中启动一个新线程来更新时间,并在每次更新后调用repaint()方法重新绘制电子表。
在paintComponent()方法中,首先计算出方形的大小和位置,然后使用Graphics类的drawRect()方法绘制方形边框。接下来,使用setFont()和drawString()方法绘制小时数、分钟数和秒数。
最后,在main()方法中创建一个JFrame并将SquareClock添加到其中。运行程序后将会看到一个简单的方形电子表。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)