public void guitest4()
{错了,构造函数不能有返回值!

解决方案 »

  1.   

    import java.awt.*;
    public class guitest4 extends Frame
    {
        public guitest4()
        {
            setTitle("this is a test");
            setLayout(new FlowLayout());
            add(new Button("Yellow"));
            add(new Button("red"));
        }
        
        public boolean handleEvent(Event evt)
        {
            if(evt.id==Event.WINDOW_DESTROY)System.exit(0);
            return super.handleEvent(evt);
            
        }
        public static void main(String[] args)
        {
            guitest4 f=new UnicodeTest();
            f.resize(600,500);
            f.show();
        }
        
        public boolean action(Event evt,Object arg)
        {
            
            if(arg.equals("Yellow"))setBackground(Color.yellow);
            else if(arg.equals("red"))setBackground(Color.red);
            repaint();
            return true;
        }
        
    }//这个是正确的代码
      

  2.   

    import java.awt.*;
    import java.awt.event.*;import javax.swing.*;public class guitest4 extends JFrame
    {
    JButton btnYellow = null;
    JButton btnRed = null;
    JPanel pneMain = null;

    public guitest4()
    {
    init ();
    }

    private void init ()
    {
    setTitle("this is a test");
    getPane ();
    }

    private JPanel getPane ()
    {
    if (pneMain == null)
    {
    pneMain = (JPanel)getContentPane ();
    getContentPane().setLayout(new FlowLayout());
    pneMain.add (getBtnYellow ());
    pneMain.add (getBtnRed ());
    }
    return (pneMain);
    }

    private JButton getBtnYellow ()
    {
    if (btnYellow == null)
    {
    btnYellow = new JButton ("Yellow");
    btnYellow.addActionListener (new ActionListener ()
    {
    public void actionPerformed (ActionEvent arg0)
    {
    setBack (Color.YELLOW);
    }
    });
    }
    return (btnYellow);
    }

    private JButton getBtnRed ()
    {
    if (btnRed == null)
    {
    btnRed = new JButton ("Red");
    btnRed.addActionListener (new ActionListener ()
    {
    public void actionPerformed (ActionEvent arg0)
    {
    setBack (Color.RED);
    }
    });
    }
    return (btnRed);
    }

    private void setBack (Color aColor)
    {
    pneMain.setBackground(aColor);
    }

    public static void main (String [] args)
    {
    guitest4 f= new guitest4();
    f.setSize (600,500);
    f.show();
    }
    }
      

  3.   

    上面的代码是用SWING组件写的,这是改正你写的代码:import java.awt.*;
    public class guitest4 extends Frame
    {
    public guitest4()
    {
    setTitle("this is a test");
    setLayout(new FlowLayout());
    add(new Button("Yellow"));
    add(new Button("red"));
    }

    public boolean handleEvent(Event evt)
    {
    if(evt.id==Event.WINDOW_DESTROY)System.exit(0);
    return super.handleEvent(evt);

    }
    public static void main(String[] args)
    {
    guitest4 f=new guitest4();
    f.resize(600,500);
    f.show();
    }

    public boolean action(Event evt,Object arg)
    {

    if(arg.equals("Yellow"))setBackground(Color.yellow);
    else if(arg.equals("red"))setBackground(Color.red);
    repaint();
    return true;
    }


    }
      

  4.   

    以下代码经过jdk1.4测试
    import java.awt.*;
    public class guitest4 extends Frame
    {
        public guitest4()
        {
            setTitle("this is a test");
            setLayout(new FlowLayout());
            add(new Button("Yellow"));
            add(new Button("red"));
        }
        
        public boolean handleEvent(Event evt)
        {
            if(evt.id==Event.WINDOW_DESTROY)System.exit(0);
            return super.handleEvent(evt);
            
        }
        public static void main(String[] args)
        {
            guitest4 f=new guitest4();
            f.resize(600,500);
            f.show();
        }
        
        public boolean action(Event evt,Object arg)
        {
            
            if(arg.equals("Yellow"))setBackground(Color.yellow);
            else if(arg.equals("red"))setBackground(Color.red);
            repaint();
            return true;
        }
        
    }