2017-10-13 2 views
1
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 


import javax.swing.*; 

public class ButtonsActionListener implements ActionListener { 

    private JButton firstButton; 
    private JButton secondButton; 


    @Override 
    public void actionPerformed(ActionEvent e) { 
     if (firstClick == null) { 
      firstClick = (JButton) e.getSource(); 
     } else { 
      secondClick = (JButton) e.getSource(); 
      // Do something 
      firstClick = null; 
      secondClick = null; 
    } 
} 

} 

이 클래스는 사용자가 클릭 한 처음 두 JButton을 기록합니다. firstButton은 사용자가 클릭 한 첫 번째 버튼을 나타내고 secondButton은 사용자가 클릭 한 두 번째 버튼을 나타냅니다.두 번째 버튼을 클릭 할 때까지 첫 번째 JButton의 색상 변경

사용자가 첫 번째 JButton을 클릭하면 두 번째 JButton이 클릭 될 때까지 색상이 빨간색으로 변경되어야합니다. 두 번째 JButton을 클릭하면 첫 번째 JButton의 색상이 원래의 색상으로 다시 변경됩니다.

현재 구현시이를 수행 할 방법이 있습니까?

답변

1

을, 현재 구현을 보존이

class ButtonsActionListener implements ActionListener { 

    private JButton firstButton; 
    private JButton secondButton; 

    @Override 
    public void actionPerformed(ActionEvent e) { 

    if (firstButton == null) { 
      firstButton = (JButton) e.getSource(); 
      firstButton.setBackground(Color.RED); 
     } else { 
      if (firstButton == (JButton) e.getSource()) { 
       firstButton.setBackground(Color.RED); 
      } else { 
       secondButton = (JButton) e.getSource(); 
       firstButton.setBackground(null);// reset to original color      
      } 
     } 


    } 

} 
+0

같은 것을 시도하기 위해 해야하는 나는 firstButton 설정 및 secondButton 후 null로? –

+0

@JackKong 그건 당신의 요구 사항에 따라, 당신은 색상을 변경 한 후 버튼 참조를 지울 필요합니까? – aKilleR

+0

첫 번째와 두 번째 버튼을 얻은 후에는 두 개의 버튼을 클릭하여 움직이는 무언가를 수행하는 move 함수를 호출해야합니다. –

0

두 번째 단추를 클릭하면 배경색을 기본값으로 설정할 수 있습니다. 처음에 첫 번째 버튼을 클릭하면 두 번째 버튼을 클릭하면 첫 번째 버튼 색상이 기본 색상으로 다시 변경됩니다.

public static void main(String[] args) { 
     final JButton button = new JButton("Click me"); 
     final JButton button2 = new JButton("Add"); 
     button.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       // TODO Auto-generated method stub 
       button.setBackground(Color.RED); 

      } 
     }); 
     button2.addActionListener(new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent e) 
      { 
       button.setBackground(null); 
      } 
     }); 
    } 
0

클릭 된 버튼을 파악하고 그에 따라 대응하기 위해, 당신은 할 수 :

class ButtonsActionListener implements ActionListener { 

    @Override 
    public void actionPerformed(ActionEvent e) { 

      if (((JButton) e.getSource()) == firstButton) { 
       firstButtonClicked(); 
      } else if (((JButton) e.getSource()) == secondButton) { 
       secondButtonClicked(); 
      } 
    } 

    private void firstButtonClicked(){ 
     System.out.println("1st button clicked "); 
     //handle second button color 
    } 
    private void secondButtonClicked(){ 
     System.out.println("2nd button clicked "); 
     //handle second button color 
    } 
} 
관련 문제