使用java画一条线。

java 复制代码
package p1008;

import javax.swing.*;
import java.awt.*;

public class LineAndTextExample extends JPanel {

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        // 设置线条粗细
        Graphics2D g2d = (Graphics2D) g;
        g2d.setStroke(new BasicStroke(5));

        // 绘制从左上角到右下角的线
        g2d.drawLine(0, 0, getWidth(), getHeight());

        // 设置文本属性
        g2d.setFont(new Font("Serif", Font.BOLD, 30));
        g2d.drawString("李太白", getWidth() / 2 - 50, getHeight() / 2);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Line and Text Example");
        LineAndTextExample panel = new LineAndTextExample();

        frame.add(panel);
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
相关推荐
ps酷教程5 小时前
Jackson 解决没有无参构造函数的反序列化问题
java
NiceCloud喜云5 小时前
Opus 4.8 的 Effort Control 怎么选:Low 到 Max 五档策略
android·java·大数据·前端·c++·python·spring
ccddsdsdfsdf6 小时前
DBeaver怎么链接mongoDB
数据库·mongodb
AI玫瑰助手6 小时前
Python函数:默认参数的定义与注意事项
开发语言·python·信息可视化
油炸自行车6 小时前
Claude Code 错误:API Error: 400 Failed to deserialize the JSON body into the
开发语言·javascript·json·trae·claude code·api error 400
肩上风骋6 小时前
C++14特性
开发语言·c++·c++14特性
_日拱一卒6 小时前
LeetCode:994腐烂的橘子
java·数据结构·算法·leetcode·深度优先
隔窗听雨眠7 小时前
Nginx网关响应慢排查手记
java·服务器·nginx
丷丩7 小时前
Postgresql基础实践教程(十一)各种Join
数据库·postgresql·join
星夜夏空997 小时前
FreeRTOS学习(4)——内存映射
数据库·学习·mongodb