import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.geom.*;
import java.util.ArrayList;import javax.swing.*;
  public class Welcome
  {
     public static void main(String[]args)

           EventQueue.invokeLater(new Runnable()
         {   
           public void run() 
         {
   SimpleTest T= new SimpleTest(); 
   T.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   T.setVisible(true);
         }
         });  
}
   }

class SimpleTest extends JFrame
  {
             public SimpleTest()
             {
      setTitle("DrawFrame");
      setExtendedState(MAXIMIZED_BOTH);
               DrawPanel P = new DrawPanel();
               add(P);
      P.MakeButton("Ellipse") ;
      P.MakeButton("Rectangle");
             }
  }
class DrawPanel extends JPanel
{         
    public void paintComponent(Graphics g)
    {
    
       Graphics2D G = (Graphics2D)g;
       for (Shape a:shape)
       { 
        G.draw(a);
       }
    }
          
     public void MakeButton( final String a) //这里的如果a不是final,则a==“Ellipse”会报错,为什么???
        {
            JButton Temp = new JButton(a);
            add(Temp);
           Temp.addActionListener(new ActionListener()//报错:ActionListener cannot be resolved to a type??
          { 
           public void actionPerformed(ActionEvent event)
             { 
          Rectan = new Rectangle2D.Double (Math.random()*100,Math.random()*100,50,50);
          if (a=="Ellipse")  //这里如果把a替换成为event.getActionCommand()会报错,为什么???
           { 
      Elli.setFrame(Rectan);
               shape.add(Elli);
            }
          else 
           {
               shape.add(Rectan);
           }
           repaint();
           }
           });
        }
     private ArrayList<Shape> shape;
     private Ellipse2D.Double Elli;
     private Rectangle2D.Double Rectan;
}
       

解决方案 »

  1.   

    // 这里的如果a不是final,则a==“Ellipse”会报错,为什么???
    第一个问题,问什么要 final 
    if (a=="Ellipse") //这里如果把a替换成为event.getActionCommand()会报错,为什么???
    你在匿名类里new ActionListener()用到了a,而a在外面。
    可以不这么做,自己写个监听器,把值a传进构造方法中。
    //报错:ActionListener cannot be resolved to a type??
      你import了吗?
    //这里如果把a替换成为event.getActionCommand()会报错,为什么???
      没有啊?
    还有不要用== 用equals 
    private ArrayList<Shape> shape = new ArrayList<Shape>();
      

  2.   


    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.*;
    import java.util.ArrayList;import javax.swing.*;public class Welcome {
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    SimpleTest T = new SimpleTest();
    T.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    T.setVisible(true);
    }
    });
    }
    }class SimpleTest extends JFrame {
    public SimpleTest() {
    setTitle("DrawFrame");
    setExtendedState(MAXIMIZED_BOTH);
    DrawPanel P = new DrawPanel();
    add(P);
    P.MakeButton("Ellipse");
    P.MakeButton("Rectangle");

    }
    }class DrawPanel extends JPanel {
    private ArrayList<Shape> shape = new ArrayList<Shape>();
    private Ellipse2D.Double Elli = null;
    private Rectangle2D.Double Rectan = null;

    public void paintComponent(Graphics g) { Graphics2D G = (Graphics2D) g;
    for (Shape a : shape) {
    G.draw(a);
    }
    } public void MakeButton(final String a) // 这里的如果a不是final,则a==“Ellipse”会报错,为什么???
    {
    JButton Temp = new JButton(a);
    add(Temp);
    Temp.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event) {
    Elli = new Ellipse2D.Double(Math.random() * 100, Math
    .random() * 100, 50, 50);
    Rectan = new Rectangle2D.Double(Math.random() * 100, Math
    .random() * 100, 50, 50);
    if ("Ellipse".equals(a))
    {
    Elli.setFrame(Rectan);
    shape.add(Elli);
    } else {
    shape.add(Rectan);
    }
    repaint();
    }
    });
    }
    }