jinlin
4 天以前 66f0597bf6a1e79540c6bc51dedf561c22f3bdb5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.example.client.utils;
 
import javax.swing.*;
import java.awt.*;
 
public class CustomPanel extends JPanel {
    private static final int DIAMETER = 20; // 圆的直径
    private static final int SPACING = 10;  // 圆和文字之间的间距
 
    private static class CircleInfo {
        String colorCode;
        String text;
 
        CircleInfo(String colorCode, String text) {
            this.colorCode = colorCode;
            this.text = text;
        }
    }
 
    public CustomPanel() {
        // 添加多个圆形和文字信息
        CircleInfo[] circleInfos = new CircleInfo[]{
                new CircleInfo("#3498DB", "进行中"),
                new CircleInfo("#F1C40F", "临期(7天)"),
                new CircleInfo("#E74C3C", "逾期"),
                new CircleInfo("#2ECC71", "正常完成"),
                new CircleInfo("#006400", "超期完成")
        };
        setCircleInfos(circleInfos);
    }
 
    private CircleInfo[] circleInfos;
 
    public void setCircleInfos(CircleInfo[] circleInfos) {
        this.circleInfos = circleInfos;
        revalidate();
        repaint();
    }
 
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
 
        int x = 140; // 起始绘制位置
        int y = (getHeight() - DIAMETER) / 2; // 垂直居中
 
        for (CircleInfo circleInfo : circleInfos) {
            // 解析颜色代码并绘制圆形
            Color color = Color.decode(circleInfo.colorCode);
            g.setColor(color);
            g.fillOval(x, y, DIAMETER, DIAMETER);
 
            // 设置文字颜色和字体,并绘制文字
            g.setColor(Color.BLACK);
            g.setFont(new Font("宋体", Font.BOLD, 20));
            x += DIAMETER + SPACING; // 更新绘制位置
            g.drawString(circleInfo.text, x, y + DIAMETER / 2 + 5);
 
            x += g.getFontMetrics().stringWidth(circleInfo.text) + 20; // 更新绘制位置
        }
    }
}