2011-12-11 18 views
0

である私は、次のようなActionListenerを実装しているクラスにJPanelのを追加しようとしています:のActionListenerのクラスにJPanelの参照を追加しようとしているが、参照は常にnull

JPanel jp2 = new JPanel(); 

     RangBazi red = new actionListenerClass (Color.RED, jp2); 
     RangBazi green = new actionListenerClass (Color.GREEN, jp2); 
     RangBazi blue = new actionListenerClass (Color.BLUE, jp2); 

     JButton bred = new JButton("red"); 
     JButton bgreen = new JButton("green"); 
     JButton bblue = new JButton("blue"); 

     bred.addActionListener(red); 
     bgreen.addActionListener(green); 
     bblue.addActionListener(blue); 
     jp2.add(bred); 
     jp2.add(bgreen); 
     jp2.add(bblue); 

     this.add(jp2 , BorderLayout.NORTH); 
//------------------------actionListenerClass.java 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 

public class actionListenerClass implements ActionListener 
{ 
    private Color mClr; 
    private JComponent mControl; 

    public actionListenerClass(Color clr , JComponent control) 
    { 
     mClr = clr; 
     control = mControl; 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     // TODO Auto-generated method stub 
     mControl.setBackground(mClr); 
    } 


} 

が、プログラムを実行した後ボタンをクリックすると、mControlのnull参照が返されます。

どうすればよいですか?

よろしく

答えて

4
control = mControl; 

あなたのActionListenerコンストラクタで間違っています。あなたが欲しかった:

mControl = control; 
+0

ああ私の神、私の愚かな間違い..... –

関連する問題