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);
}
}