import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;//定义的面板类
public class paintPanel extends JPanel  {
  private FlowLayout flowLayout1 = new FlowLayout(); public paintPanel() {
   super();
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }  private void jbInit() throws Exception {
    this.setBackground(Color.white);
    this.setBorder(BorderFactory.createLineBorder(Color.black));
    this.setLayout(flowLayout1);
  }
  public void paint(Graphics g){
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.red);
    g2d.setStroke(new BasicStroke(5f));
    g2d.drawLine(10,20,100,80);
  }
}
//调用了这个面板的类
import java.awt.*;
import javax.swing.*;public class Frame1 extends JFrame {  JPanel pp = new paintPanel();
  public Frame1() {
    this.setContentPane(pp);
  }
  public static void main(String[] args) {
    Frame1 frame1 = new Frame1();
    frame1.setLocation(200,200);
    frame1.setSize(200,200);
    frame1.setVisible(true);
  }
}