Java GUI 一个按钮画直线,一个按钮画椭圆,怎么不行啊??

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

class Board extends JFrame implements ActionListener
{
int x1,x2,y1,y2;
Container cp = getContentPane();

JPanel pa = new JPanel();
JButton but1 = new JButton("直线");
JButton but2 = new JButton("空心椭圆");

public Board()
{
    super("画板");
    setBounds(400, 20, 500, 500);
    cp.setLayout(new FlowLayout()); 
    pa.add(but1);
    pa.add(but2);
    cp.add(pa);

    but1.addActionListener(this);
//  but1.setActionCommand("直线");
    but2.addActionListener(this);
//  but2.setActionCommand("空心椭圆");

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
    if (e.getSource() == but1)
    {
        System.out.println("fj");

        cp.addMouseListener(new MouseAdapter()
        {
            public void mousePressed(MouseEvent e)
            {
                x1=e.getX();
                y1=e.getY();
            }
            public void mouseReleased(MouseEvent e)
            {
                x2=e.getX();
                y2=e.getY();
                Graphics g = getGraphics();
                g.drawLine(x1, y1, x2, y2);
                //g.drawRect(x1, y1, Math.abs(x2-x1),Math.abs(y2-y1));
            }

        });

    } else
    {
        System.out.println("jfjf");
        cp.addMouseListener(new MouseAdapter()
        {
            public void mousePressed(MouseEvent e)
            {
                x1=e.getX();
                y1=e.getY();
            }
            public void mouseReleased(MouseEvent e)
            {
                x2=e.getX();
                y2=e.getY();
                Graphics g = getGraphics();
                //g.drawLine(x1, y1, x2, y2);
                g.drawRect(x1, y1, Math.abs(x2-x1),Math.abs(y2-y1));
            }

        });
    //  g.drawRect(x1, y1, Math.abs(x2-x1),Math.abs(y2-y1));
    }
}

}

public class Text
{
public static void main(String[] args)
{
new Board();
}
}

图片说明

cp.addMouseListener(new MouseAdapter()

用frame.addMouseListener(new MouseAdapter()或this.addMouseListener(new MouseAdapter()

https://github.com/ljheee/MyPaint1.0
这是我做的,所有图形都实现了,你看一下