【问题标题】:actionlistener returning a nullexception on jbuttonactionlistener 在 jbutton 上返回 null 异常
【发布时间】:2010-08-18 18:58:35
【问题描述】:

我在我的主 jframe 菜单上为上面列出的按钮设置了一个动作侦听器,它们工作正常,可以根据需要调出其他 jframe。问题是当一个人单击 jframes 上的按钮时,在该子菜单 jframe 上单击 jbutton 后,我得到一个空异常。

示例代码:

public class main extends JFrame implements ActionListener
{
   public main
   {
       private JButton thisButton = new JButton( "this" );
       private JButton thatButton = new JButton( "that" );
       thisButton.addActionListener( this );
       thatButton.addActionListener( this );
       thisButton.setActionCommand( "THISBUTTON" );
       thatButton.setActionCommand( "THATBUTTON" );           

       setLayOut( new FlowLayout() );

       add(thisButton);

       public void actionPerformed( ActionEvent event )
       {
          String source = event.getActionCommand();
          if( source.equals( "THISBUTTON" )
          {
              JFrame thisFrame = new JFrame();
              thisFrame.add( thatButton );

              if( source.equals( "THATBUTTON" )
              {
                  System.out.println( "pushed thatbutton" );
              }
          }
       }
    }
}

现在我几乎可以肯定我需要为内部 jbutton 设置另一个动作侦听器,但我不知道该怎么做。

【问题讨论】:

  • 您的示例代码似乎无法编译,您有有效的示例吗?
  • 好吧,我想我明白了。我制作了另一个类,它用自己的动作监听器制作框架。这样就没有冲突,也没有发生空异常;

标签: jframe jbutton actionlistener actioncontroller argumentnullexception


【解决方案1】:

要为内部 JButton 设置另一个动作侦听器,只需为每个按钮编写此代码

thisButton.addActionListener(this);

【讨论】:

    猜你喜欢
    • 2020-11-01
    • 2011-05-23
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多