使用java.awt.Graphics2D的setStroke方法就可以解决这个小问题
一个Graphics对象可以强制转化为Graphics2D类型(向下转型)
Graphics2D g2 = (Graphics2D)g; //g是Graphics对象
g2.setStroke(new BasicStroke(3.0f))
public class MyPanel extends JPanel{
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g; //g是Graphics对象
g2.setStroke(new BasicStroke(3.0f));
g2.setColor(Color.GREEN);
g2.drawLine(0, 0, 500, 600);
}
}