public void paintComponent(Graphics g)
{  
   super.paintComponent(g);
   if (menuitemtext=="newitem")
   { 
     g.setColor(Color.red);//加上这句
     g.drawRect(100,100,100,40);
   }    
}

解决方案 »

  1.   

    谢谢你了,等了好久啊!
    不过那不是问题的所在!好象是在框架里不能有这个函数
    我是这样写的
    public class w2
    {  
    public static void main(String[] args)
    {  
    FrameSize frame = new FrameSize();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.show(); 

    }
    }class FrameSize extends JFrame
    {
    public FrameSize() //gouzaofangfa
    {
    setTitle("w2");
    setSize(400,400);


    JPanel panel3 =new JPanel();
    panel3.setSize(400,100);
    panel3.setBackground(Color.white);

    Container aaa=getContentPane();
    aaa.add(panel3,BorderLayout.CENTER);

    JMenuBar menubar=new JMenuBar();
    setJMenuBar(menubar);

    //File
    JMenu filemenu=new JMenu("File");
    filemenu.setMnemonic('F');

    newitem=new JMenuItem("New",'N');
    newitem.addActionListener(new showaction("new"));

    openitem=new JMenuItem("Open",'O');
    openitem.addActionListener(new showaction("open"));

    filemenu.add(newitem);
    filemenu.add(openitem);


    menubar.add(filemenu);
    menubar.add(helpemenu);

    }
    public void paintComponent(Graphics g)
    {  
    super.paintComponent(g);
    if (menuitemtext=="newitem")
    {
    g.setColor(Color.red);
    g.drawRect(100,100,100,40);
    //g.drawImage(image,50,150,null);
    }

       
    }

    private class showaction extends AbstractAction
    {
    public showaction(String name)
    {
    super(name);
    }
    public void actionPerformed(ActionEvent event)
    {
    if(event.getSource()== newitem)
    {
    openitem.setEnabled(false);
    menuitemtext="newitem";

    }

    }
    }
    private JMenuItem newitem;
    private JMenuItem openitem;
    public String menuitemtext="";

    }
      

  2.   

    在super.paintComponent(g)句出错问题是:
    cannot resolve symbol method paintCompent (java.awt.Graphics)
      

  3.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class W2
    {  
    public static void main(String[] args)
    {  
    FrameSize frame = new FrameSize();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.show(); 

    }
    }class FrameSize extends JFrame
    {
    public FrameSize() //gouzaofangfa
    {
    setTitle("w2");
    setSize(400,400);


    JPanel panel3 =new JPanel();
    panel3.setSize(400,100);
    panel3.setBackground(Color.white);

    Container aaa=getContentPane();
    aaa.add(panel3,BorderLayout.CENTER);

    JMenuBar menubar=new JMenuBar();
    setJMenuBar(menubar);

    //File
    JMenu filemenu=new JMenu("File");
    filemenu.setMnemonic('F');

    newitem=new JMenuItem("New",'N');
    newitem.addActionListener(new showaction("new", this));

    openitem=new JMenuItem("Open",'O');
    openitem.addActionListener(new showaction("open", this));

    filemenu.add(newitem);
    filemenu.add(openitem);


    menubar.add(filemenu);
    //menubar.add(helpemenu);

    }
    public void paint(Graphics g)
    {
    super.paint(g);
    if (menuitemtext=="newitem")
    {
    g.setColor(Color.red);
    g.drawRect(100,100,100,40);
    //g.drawImage(image,50,150,null);
    }

       
    }

    private class showaction extends AbstractAction
    {
    private Frame f;
    public showaction(String name, Frame f)
    {
    super(name);
    this.f = f;
    }
    public void actionPerformed(ActionEvent event)
    {
    if(event.getSource()== newitem)
    {
    openitem.setEnabled(false);
    menuitemtext="newitem";
    f.repaint();
    }

    }
    }
    private JMenuItem newitem;
    private JMenuItem openitem;
    public String menuitemtext="";

    }
      

  4.   

    或者:import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class W
    {  
    public static void main(String[] args)
    {  
    FrameSize2 frame = new FrameSize2();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.show(); 

    }
    }class T extends JPanel{
    private String menuitemtext;
    public T(String mt){
    super();
    menuitemtext = mt;
    }
    public void setMenuitemtext(String mt){
    menuitemtext = mt;
    }
    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    if (menuitemtext=="newitem")
    {
    g.setColor(Color.red);
    g.drawRect(100,100,100,40);
    //g.drawImage(image,50,150,null);
    }    
    }
    }class FrameSize2 extends JFrame
    {
    public FrameSize2() //gouzaofangfa
    {
    setTitle("w2");
    setSize(400,400);


    T panel3 =new T("menuitemtext");
    panel3.setSize(400,100);
    panel3.setBackground(Color.white);

    Container aaa=getContentPane();
    aaa.add(panel3,BorderLayout.CENTER);

    JMenuBar menubar=new JMenuBar();
    setJMenuBar(menubar);

    //File
    JMenu filemenu=new JMenu("File");
    filemenu.setMnemonic('F');

    newitem=new JMenuItem("New",'N');
    newitem.addActionListener(new showaction("new", panel3));

    openitem=new JMenuItem("Open",'O');
    openitem.addActionListener(new showaction("open", panel3));

    filemenu.add(newitem);
    filemenu.add(openitem);


    menubar.add(filemenu);
    //menubar.add(helpemenu);

    }

    private class showaction extends AbstractAction
    {
    private T f;
    public showaction(String name, T f)
    {
    super(name);
    this.f = f;
    }
    public void actionPerformed(ActionEvent event)
    {
    if(event.getSource()== newitem)
    {
    openitem.setEnabled(false);
    f.setMenuitemtext("newitem");
    f.repaint();
    }

    }
    }
    private JMenuItem newitem;
    private JMenuItem openitem;
    public String menuitemtext="";
    }JFrame只有paintComponents方法。
      

  5.   

    先了解一下多态性等等的知识,然后再搞这个不迟。
    你凭什么认为你写了
    public void paintComponent(Graphics g)
    {  
       super.paintComponent(g);
       if (menuitemtext=="newitem")
       { 
         g.drawRect(100,100,100,40);
       }    
    }
    他就可以画图呢?这个方法是JPanel的,应该在JPanel的子类里面覆盖。
      

  6.   

    同意funcreal(new PLMM[Integer.MAX_VALUE]) !
      

  7.   

    class T extends JPanel{
    private String menuitemtext;
    public T(String mt){
    super();
    menuitemtext = mt;
    }
    public void setMenuitemtext(String mt){
    menuitemtext = mt;
    }
    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    if (menuitemtext=="newitem")
    {
    g.setColor(Color.red);
    g.drawRect(100,100,100,40);
    //g.drawImage(image,50,150,null);
    }    
    }
    }难道这不是在JPanel子类中覆盖?