2014-01-15 1 views
1

일부 단추가있는 패널을 설계했습니다. 버튼에는 ActionListener가 첨부됩니다. 이 버튼을 클릭하면이 ActionListener는이 단일 클릭에 대해 4 개의 이벤트를 감지합니다. 반면에 그것은 하나만 탐지해야합니다. 정확히 그 이유가 누구인지 압니까?조치 수신기가 단일 이벤트에 대해 여러 이벤트를 감지했습니다.

public class Buttons extends JPanel 
{ 
private JButton undo=new JButton("Undo"); 
private JButton replay=new JButton("Replay"); 

public void paint(Graphics g) 
{ 
    super.paint(g); 
    super.setSize(new Dimension(560,30)); 
    super.add(replay); 
    super.add(undo); 
    undo.setBorder(new LineBorder(Color.WHITE,3)); 
    replay.setBorder(new LineBorder(Color.WHITE,3)); 

    undo.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent arg0) { 
      // TODO Auto-generated method stub 
      Controler.pieces.undo(); 
      Controler.reDraw(); 

     } 
    }); 
    replay.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent arg0) { 
      // TODO Auto-generated method stub 
      System.out.println("Dastiii"); 
     } 
    }); 

} 
} 

이러한 이벤트는 귀하가 paint 방법에서 당신에게 ActionListener의 등록을 여기

public void undo() 
{ 
    System.out.print(Controler.allMoves.size()); 
    if(Controler.allMoves.size()<=1) 
    { 
     init_board(); 
     return; 
    } 
    Piece temp[][]=Controler.allMoves.get(Controler.allMoves.size()-2); 
    for(int i=0;i<8;i++) 
    { 
     for(int j=0;j<8;j++) 
     { 
      board[i][j].set_name(temp[i][j].get_name()); 
      board[i][j].set_oneWay(temp[i][j].get_oneWay()); 
     } 
    } 
    Controler.allMoves.remove(Controler.allMoves.size()-2); 
} 
+1

질문에 코드를 추가하십시오. –

+0

아마도 'buttonDown','buttonPressed', 'buttonUp'과 같은 것을 감지 할 것입니다. – csmckelvey

+0

@csmckelvey 버튼이있는 actionlistener를 추가했습니다. ** buttonDown ** 또는 다른 일부는 마우스 수신기에 연결되어 있습니다. 내가 맞습니까? –

답변

3

을 사용하고 있습니다!

의도가 있다는 사실에 대해 걱정하지 말자 않은 권장 paint

변경하거나 구성 요소 또는 그것의 어떤 상태를 수정 절대로 무시할 수있는 paint 방법 내 자식 구성 요소의 이러한 여러 번 호출됩니다 귀하의 응용 프로그램을 실행하는 동안.

  • Performing Custom Painting
  • : 메인 윈도우가 표시되어 단지 때 paint 방법은 2-4 번 호출 할 수 있도록 예를 들어,
    public void paint(Graphics g) 
    { 
        super.paint(g); 
        /** All this should be done within the constructor 
        // If you are using a layout manager, this is pointless, if your not 
        // then that's another problem 
        super.setSize(new Dimension(560,30)); 
        super.add(replay); 
        super.add(undo); 
        undo.setBorder(new LineBorder(Color.WHITE,3)); 
        replay.setBorder(new LineBorder(Color.WHITE,3)); 
    
        undo.addActionListener(new ActionListener() { 
    
         @Override 
         public void actionPerformed(ActionEvent arg0) { 
          // TODO Auto-generated method stub 
          Controler.pieces.undo(); 
          Controler.reDraw(); 
    
         } 
        }); 
        replay.addActionListener(new ActionListener() { 
    
         @Override 
         public void actionPerformed(ActionEvent arg0) { 
          // TODO Auto-generated method stub 
          System.out.println("Dastiii"); 
         } 
        }); 
        **/ 
    
    } 
    

    은 봐 ... 이상한 아니다 어떻게 어떤 그림에 대한 자세한 내용은 Painting in AWT and Swing

는 스윙에

+0

이런 쓰레기는 어떻게 보였습니까? – csmckelvey

+0

@MadProgrammer Ohhhh ... 알았어 .. 알았어 !! !! :) –

+0

@csmckelvey 처음에 제대로 전달 된 것처럼 보였습니다 ... 내 잠재 의식이 저를 보호하려고 시도했다고 생각합니다 ... – MadProgrammer

관련 문제