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); 
相關問題