import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class GraphicsFunda extends JPanel implements ActionListener
{
Graphics myg;
JButton jb;
GraphicsFunda()
{
jb = new JButton("Draw");
add(jb);
setBackground(Color.YELLOW);
jb.addActionListener(this);
}
public void paintComponent(Graphics g)
{
myg=g;
g.drawOval(100,300,50,50);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==jb)
myg.fillRect(10,10,200,200);
}
public static void main(String... sd)
{
GraphicsFunda gf = new GraphicsFunda();
JFrame jf = new JFrame();
jf.add(gf,BorderLayout.NORTH);
jf.setBackground(Color.blue);
jf.setSize(400,400);
jf.setVisible(true);
}
请帮我解决其中的问题。如果我们从paintComponent中获取Graphics类的引用,那么我们可以在绘制其他形状时使用它,但它在这里不起作用:(