2011-12-05 2 views
-4

내 응용 프로그램에 문제가 있습니다. AWT 구성 요소를 사용하여 JButton을 만들려고합니다. 주요 문제는 예외가 있습니다 : QButton.QButton.addActionListener (QButton.java:83). //this.addActionListener 행을 주석 처리하면 모든 것이 정상입니다. 나의 목적은 내가 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

또 하나의 stackoverflow 문제가 stackoverflow! – Gautam

+3

당신은 무엇을 기대합니까? 'this.addActionListener (listener);'무조건 자체적으로 – artistoex

답변

3

을 확장한다.

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

+1을 호출합니다. OP가 예외의 스택 트레이스를 인쇄 /보기를 꺼려한다면 ** 명백한 **임을 지적하는 것이 가치가 있습니다. Java 디버깅의 첫 번째 교훈 ... 스택 트레이스를 살펴보십시오! –

+0

그래서 내 구성 요소에 addActionListener 메서드를 만들 수 있습니까? MouseListener를 넣을 이미지와 동일한 문제가 있습니다. –

+0

@Magda Maria Magdalena> 답변을 검색 할 수 있습니다. 아무도없는 경우 적절한 제목으로 새 질문을 시작하십시오. 무엇을하고 싶은지, 어떤 문제가 있는지, 무엇을 시도했는지/예상했는지, 자세하게 설명하십시오. UI에 대해 잘 아는 다른 사람들은 당신이하고 싶은 것을 파악하려고 많은 시간을 들이지 않고도 당신을 도울 수 있습니다. [FAQ] (http://stackoverflow.com/faq)를 읽을 수 있습니다. 에 오신 것을 환영합니다. –

3

아마 super.addActionListener(listener);을 말하고 싶습니까?

관련 문제