原程序是两个按钮的事件处理,即按BUTTON   A标签出现文本XXX,按BUTTON   B标签出现文本YYY 
现在我改写成三个按钮 
但是编译过了,就是不能执行,请各位指点一下     分数不多,只能放20了,在这谢谢了 
代码如下 import   java.awt.*; 
import   java.awt.event.*; 
import   javax.swing.*; 
public   class   MyFirstWindow   extends   JFrame   implements   ActionListener{//ActionListener   监听器 
      private   static   final   String   MYFIRST= "My   First   Window "; 
      private   static   final   String   CONGRUATULATIONS= "Congratulations "; 
      private   static   final   String   THANKS= "Thanks   for   your   using "; 
      private   String   message=MYFIRST; 
      private   JPanel   counterPanel; 
      private   JButton   counterButton1; 
        private   JButton   counterButton2; 
  private   JButton   counterButton3; 
      private   JLabel   counterLabel; 
      public   MyFirstWindow(){ 
      super( "My   First   Window "); 
      counterLabel=new   JLabel(message,SwingConstants.SOUTH); 
      counterButton1=new   JButton( "My   First   Window "); 
      counterButton2=new   JButton( "Congratulations "); 
      counterButton3=new   JButton( "Thanks   For   your   using "); 
      counterButton1.addActionListener(this); 
      counterButton2.addActionListener(this); 
      counterButton3.addActionListener(this); 
        
        
      counterPanel.add(counterLabel,BorderLayout.NORTH); 
      counterPanel.add(counterButton1,BorderLayout.WEST); 
      counterPanel.add(counterButton2,BorderLayout.CENTER); 
      counterPanel.add(counterButton3,BorderLayout.EAST); 
      getContentPane().add(counterPanel);// 
      addWindowListener(new   WindowAdapter(){ 
public   void   windowClosing(WindowEvent   e){ 
System.exit(0); 

      }); 
      pack(); 
setVisible(true); 
      } 
      public   void   actionPerformed(ActionEvent   e){ 
if(message.equals(MYFIRST)){ 
message=MYFIRST; 
} if(message.equals(CONGRUATULATIONS)){ message=CONGRUATULATIONS; 

if(message.equals(THANKS)){ 
message=THANKS; 

counterLabel.setText(message); 
      } 
public   static   void   main(final   String[]   args) 
{final   MyFirstWindow   app=new   MyFirstWindow(); 


解决方案 »

  1.   

    contentPanel没有初始化
    import       java.awt.*;   
    import       java.awt.event.*;   
    import       javax.swing.*;   
    public       class       Test       extends       JFrame       implements       ActionListener{//ActionListener       监听器   
    private       static       final       String       MYFIRST=   "My       First       Window   ";   
    private       static       final       String       CONGRUATULATIONS=   "Congratulations   ";   
    private       static       final       String       THANKS=   "Thanks       for       your       using   ";   
    private       String       message=MYFIRST;   
    private       JPanel       counterPanel;   
    private       JButton       counterButton1;   
    private       JButton       counterButton2;   
    private       JButton       counterButton3;   
    private       JLabel       counterLabel;   
    public       Test(){   
    super(   "My       First       Window   ");   
    counterLabel=new       JLabel(message,SwingConstants.CENTER);   
    counterButton1=new       JButton(   "My       First       Window   ");   
    counterButton2=new       JButton(   "Congratulations   ");   
    counterButton3=new       JButton(   "Thanks       For       your       using   ");   
    counterButton1.addActionListener(this);   
    counterButton2.addActionListener(this);   
    counterButton3.addActionListener(this);   

    counterPanel=new JPanel();//增加此句
    counterPanel.add(counterLabel,BorderLayout.NORTH);   
    counterPanel.add(counterButton1,BorderLayout.WEST);   
    counterPanel.add(counterButton2,BorderLayout.CENTER);   
    counterPanel.add(counterButton3,BorderLayout.EAST);   
    getContentPane().add(counterPanel);//   
    addWindowListener(new       WindowAdapter(){   
    public       void       windowClosing(WindowEvent       e){   
    System.exit(0);   
    }   
    });   
    pack();   
    setVisible(true);   
    }   
    public       void       actionPerformed(ActionEvent       e){   
    message=((JButton)e.getSource()).getText();
    if(message.equals(MYFIRST)){   
    message=MYFIRST;   
    }   

    if(message.equals(CONGRUATULATIONS)){   

    message=CONGRUATULATIONS;   
    }   
    if(message.equals(THANKS)){   
    message=THANKS;   
    }   
    counterLabel.setText(message);   
    }   
    public       static       void       main(final       String[]       args)   
    {       Test       app=new       Test();   
    }