好像你的source 编译都有问题,如何运行???

解决方案 »

  1.   

    应该是类包没找到吧,看一下java path 以及class path是否正确
      

  2.   

    把Panel textPanel=new Panel();一句移到外面如下:
    public class PanelDemo extends Frame implements ActionListener
    {
       public static final int WIDTH=300;
       public static final int HEIGHT=200;
       Panel textPanel=new Panel();
       
       
       public PanelDemo()
       {
          setTitle("Panel Demonstration");
          setSize(WIDTH,HEIGHT);
          setBackground(Color.blue);
          addWindowListener(new WindowDestroyer());
          
          Panel buttonPanel=new Panel();
          buttonPanel.setBackground(Color.white);
          
          buttonPanel.setLayout(new FlowLayout());
          
          Button goButton=new Button("Green");
          goButton.addActionListener(this);
          buttonPanel.add(goButton);
          
          Button stopButton=new Button("Red");
          stopButton.addActionListener(this);
          buttonPanel.add(stopButton);
          
          textPanel.setBackground(Color.black);
          TextField theText=new TextField(30);
          theText.setBackground(Color.yellow);
          theText.setText("watch me");
          textPanel.add(theText);
                
          setLayout(new BorderLayout());
          add(buttonPanel,"South");
          add(textPanel,"North");
        }
        
        public static void main(String[] args)
       {
          PanelDemo guiWithPanel=new PanelDemo();
          guiWithPanel.setVisible(true);
       }
                 
        public void actionPerformed(ActionEvent e)
        {
          if(e.getActionCommand().equals("Red"))
          {
             theText.setBackground(Color.red);
             theText.setText("stop");
          }
          else if(e.getActionCommand().equals("Green"))
          {
             theText.setBackground(Color.green);
             theText.setText("go");
          }
          else
             theText.setText("interface error!");
             repaint();
          }
          
          private TextField theText;
          
          private Panel textPanel;
    }
      

  3.   

    我是想在按按钮“GO”时,字幕上显示GO,字幕栏颜色变成绿色,按按钮“STOP”时,字幕显示STOP,字幕栏颜色变成红色。
    感谢帮忙,楼上的前辈,还是一样的错误,编译没有问题,执行时才会出现问题,我是用的命令行来运行和编译的。
    我用PANEL类编了另外一个计算器的程序,可以正确运行,但是还是会出现如上情况。请大虾指点,不胜感激。
      

  4.   

    我想可能是因为这个原因:
    在调用ACTIONLISTENER的时候不能用来处理调用对象以外的数据,比如:
     theText.setBackground(Color.red);就不对,因为它处理了theText对象,而调用ACTIONLISTENER的是stopButton和goButton对象。
    不知道理解得对不对,如果是这样的话,那ACTIONLISTENER的作用就很小了。
      

  5.   

    1:注意变量访问权限,2:addWindowListener(new WindowDestroyer());语句用的时候没有声明接口吧
    import java.awt.*;
    import java.awt.event.*;public class PanelDemo extends Frame implements ActionListener
    {
       public static final int WIDTH=300;
       public static final int HEIGHT=200;
       TextField theText;
       Button goButton,stopButton;
       Panel buttonPanel,textPanel;
       
       public PanelDemo()
       {
          setTitle("Panel Demonstration");
          setSize(WIDTH,HEIGHT);
          setBackground(Color.blue);
          //addWindowListener(new WindowDestroyer());
          
          buttonPanel=new Panel();
          buttonPanel.setBackground(Color.white);
          
          buttonPanel.setLayout(new FlowLayout());
          
          goButton=new Button("Green");
          goButton.addActionListener(this);
          buttonPanel.add(goButton);
          
          stopButton=new Button("Red");
          stopButton.addActionListener(this);
          buttonPanel.add(stopButton);
          
          textPanel=new Panel();
          textPanel.setBackground(Color.black);
          theText=new TextField(30);
          theText.setBackground(Color.yellow);
          theText.setText("watch me");
          textPanel.add(theText);
                
          setLayout(new BorderLayout());
          add(buttonPanel,"South");
          add(textPanel,"North");
        }
        
        public static void main(String[] args)
       {
          PanelDemo guiWithPanel=new PanelDemo();
          guiWithPanel.setVisible(true);
       }
                 
        public void actionPerformed(ActionEvent e)
        {
          if(e.getSource()==goButton)
          {
             theText.setBackground(Color.red);
             theText.setText("stop");
          }
          else if(e.getSource()==stopButton)
          {
             theText.setBackground(Color.green);
             theText.setText("go");
          }
          else
             theText.setText("interface error!");
             repaint();
          }
          
          
    }
      

  6.   

    我发现楼上的程序和我的区别就是把theText,goButton,stopButton,buttonPanel,textPanel的声明全部放到CONTRUCTOR外面了,所以ACTIONPERFORMED这个方法可以修改这些实例变量。但是楼上的GETSOURCE()是什么意思?是得到调用LISTENER的对象吗?