【发布时间】: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