import  java.awt.*;
import  java.awt.event.*;
public class Interact  implements ActionListener {
    Frame   f=new    Frame("按钮交互事件");
    f.setLayout(new  FlowLayout());//此处出现提示错误。
    Button    b=new  Button("打开");
  
    public  void  actionPerformed(ActionEvent e)
    {   if(e.getSource()==b)
       {   if(b.getLabel()=="打开")
         {   b.setLabel("关闭");
              f.pack();
         }
       else
       {b.setLabel("打开");  f.pack();}
    
    }
我想作一个按钮交互的图像,看看上面的程序怎么改呀。
    }
}

解决方案 »

  1.   

    setLayout 这些是布局管理器来调用的 不能直接用类对象调用`
      

  2.   

    f.setLayout(new  FlowLayout());这是命令不能放在声明成员的位置
      

  3.   

    package ocean.accp.demo;import java.awt.*;
    import java.awt.event.*;public class Interact implements ActionListener { public static void main(String[] args) {
    Frame frame=new Frame("按钮交互事件");
    frame.setLayout(new FlowLayout());
    frame.setSize(200, 100);
    frame.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
    Button cmd=new Button("打开");
    cmd.addActionListener(new Interact());
    frame.add(cmd);

    frame.setVisible(true);
    }
    public void actionPerformed(ActionEvent e) {
    if(e.getActionCommand().equals("打开")){
    ((Button)e.getSource()).setLabel("关闭");
    }else{
    ((Button)e.getSource()).setLabel("打开");
    }
    }
    }