0

我有一个动作监听器在我的主jframe菜单上为它上面列出的按钮设置,它们工作正常,根据需要调出其他jframes。问题是,当一个人点击jframes上的按钮时,点击该子菜单jframe上的jbutton后,我会得到一个nullexception。actionlistener在jbutton上返回一个nullexception

示例代码:

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但我在失去了如何做到这一点。

+0

你的示例代码似乎并没有编译,你有一个有效的例子吗? – 2010-08-18 19:03:11

+0

它错过了一个结局} – rlindsey 2010-08-18 19:16:55

+0

好吧,我想我明白了。我做了另一个课,用自己的行动听众制作了画框。这样就没有冲突,并且没有发生无扭曲现象。 – rlindsey 2010-08-19 03:58:05

回答

0

要设置内Jbutton将另一个动作监听器只写这段代码为每个按钮

thisButton.addActionListener(this);