2011-12-05 12 views
-4

私のアプリケーションにいくつかの問題があります。私はAWTコンポーネントだけでJButtonを作成しようとしています。主な問題は、例外があることです.QButton.QButton.addActionListener(QButton.java:83)です。 もし私が//this.addActionListener行にコメントすると、すべてがOKです。私のオブジェクトは、私はUI事に慣れていないんだけど、あなたのプログラムが自分自身を呼び出すするメソッドを呼び出そうとするので、あなたがStackOverflowException取得する理由は、あるパネルaddActionListenerメソッドのStackoverflow例外

public class QButton extends Panel implements MouseListener,ActionListener{ 
    public Label text; 
    ImagePanel image; 
    ActionListener listener; 

    public QButton(String text){ 
     Label l = new Label(text); 
     this.add(l); 
     this.text=l; 

     this.setLayout(new GridBagLayout()); 
     this.setBackground(Color.gray); 

     TextButtonActions ac1=new TextButtonActions(this); 
     this.addMouseListener(ac1); 
     this.text.addMouseListener(ac1); 
    } 

    public QButton(ImagePanel img){ 
     this.setLayout(new GridLayout()); 

     this.image=img;   
     this.add(image); 

     PictureButtonActions ac2=new PictureButtonActions(this); 
     this.image.addMouseListener(ac2); 
     } 

    public QButton(String text, ImagePanel img){ 
     this.setBackground(Color.gray); 
     this.setLayout(new GridLayout()); 

     Label l = new Label(text); 
     this.add(l); 
     this.text=l; 

     this.image=img; 
     this.add(image); 

     TAndPButtonActions ac3=new TAndPButtonActions(this); 
     this.image.addMouseListener(ac3); 
     this.text.addMouseListener(ac3); 
    } 

    public void setText(String txt) 
    { 
     this.text.setText(txt); 
    } 

    public String getText() 
    { 
     return(text.getText()); 
    } 

    public void setImage(ImagePanel i) 
    { 
     this.remove(image); 
     this.image=i; 

     this.add(i); 
     //System.out.println("setImage"); 
     this.validate(); 
    } 

    public ImagePanel getImage() 
    { 
     return(image); 
    } 

    void addActionListener(ActionListener listener) 
    { 
     this.listener=listener; 
     this.addActionListener(listener); 
    } 
} 
+0

ah別のスタックオーバーフローの問題stackoverflow !! – Gautam

+3

あなたは何を期待していますか? 'this.addActionListener(listener);'は無条件に自分自身を – artistoex

答えて

3

を拡張しています。

void addActionListener(ActionListener listener) 
    // ^^^^^^^^^^^^^^^^^ 
{ 
    this.listener=listener; 
    this.addActionListener(listener); // <-- will keep calling itself. 
     //^^^^^^^^^^^^^^^^^ 
} 
+0

と呼んでいます。OPが例外のスタックトレースを印刷したり調べたりするのが面倒であれば、**明らかに**なるはずです。 Javaデバッグの最初のレッスン...スタックトレースを見てください! –

+0

どうすれば自分のコンポーネントのaddActionListenerメソッドを作成できますか?私はMouseListenerを配置したい画像に同じ問題があります。 –

+0

@Magda Maria Magdalena>あなたは答えを探し回っているかもしれません。誰もいない場合は、適切なタイトルで新しい質問を開始してください。あなたが何をしたいのか、何を問題にしているのか、何を試した/期待したのかを詳細に説明するので、UIについて知っている他の人々が、あなたがしたいことを理解しようと多くの時間を費やすことなくあなたを助けることができます。 [FAQ](http://stackoverflow.com/faq)を読むことができます。ようこそ。 –

3

おそらくsuper.addActionListener(listener);と言っていましたか?

関連する問題