2017-10-07 1 views
0
public class GlassActionListener{ 

    JButton first; 
    JButton second; 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     if(first == null) 
     { 
      first = (JButton) e.getSource(); 
     } 
     else 
     { 
      second = (JButton) e.getSource(); 

      // do something 

      // clean up 
      first = null; 
      second = null; 
     } 
    } 
} 

public class GUIControl { 

    public static void main(String[] args) { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       showGUI(); 
      } 
     }); 
    } 

    public static void showGUI() { 

     JFrame frame = new JFrame("MyFrame"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLayout(new BorderLayout()); 

     JButton glass1 = new JButton("glass1"); 
     JButton glass2 = new JButton("glass2"); 
     JButton glass3 = new JButton("glass3"); 

     frame.getContentPane().add(glass1); 
     frame.getContentPane().add(glass2); 
     frame.getContentPane().add(glass3); 
     frame.pack(); 
     frame.setVisible(true); 

     glass1.addActionListener(??); 
     glass2.addActionListener(??); 
     glass3.addActionListener(??); 

     // I want to store the first two clicks in these variables 
     from = "glassX"; 
     to = "glassY"; 

     puzzle.move(from, to); 
    } 

사용자는 안경 사이에 소다를 쏟습니다. 사용자가 세 가지 중 하나를 클릭하면 첫 번째 JButton이 "처음"유리를 결정하고 두 번째 JButton을 클릭하면 사용 가능한 세 가지 중 하나를 클릭하여 "대상"대상 유리를 결정합니다.사용자가 두 번 클릭하는 순서를 결정하십시오.

사용자가 클릭 한 특정 두 JButton을 어떻게 확인할 수 있습니까? 사용자가 세 개의 JButton 중 두 개를 클릭 할 때마다 두 JButton과 관련된 문자열을 "from"및 "to"변수에 저장하려고합니다.

이렇게 할 방법이 있습니까?

답변

1

을 만들어 ActionListener으로 만들고 JButton#addActionListener을 사용하여 3 개의 버튼 모두에 추가하십시오. 이벤트에는 사용할 수있는 소스 필드가 있습니다.

ActionListener listener = new ActionListener() { 
    JButton first; 
    JButton second; 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     if(first == null) 
     { 
      first = (JButton) e.getSource(); 
     } 
     else 
     { 
      second = (JButton) e.getSource(); 

      // do something 

      // clean up 
      first = null; 
      second = null; 
     } 
    } 
}; 


glass1.addActionListener(listener); 
glass2.addActionListener(listener); 
glass3.addActionListener(listener); 

청취자 내부의 로직을 처리하는 것은 약간 더렵습니다.하지만 그 생각은 사실입니다.

+0

당신이 나에게 구현을 보여 주시겠습니까? –

+0

위와 같이 수행하고 청취자의 내용을 새 클래스 – Igor

+0

으로 옮기십시오. "\\ do something part"에서 무엇을해야합니까? –

0
firstbutton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
    //do something here first button is clicked 
    } 
}); 

버튼과 같은 버튼에 리스너를 추가하고 클래스 변수를 사용하여 버튼을 몇 번 클릭했는지 추적 할 수 있습니다.

예 : 난 그냥 JButtonGlassActionListener라는 하나 개의 클래스를 생성하고 각각의 JButton이 클래스의 인스턴스를 추가하려면

public class GUIControl { 
public static void main(String[] args) { 
    javax.swing.SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      showGUI(); 
     } 
    }); 
} 



public static void showGUI() { 
    JFrame frame = new JFrame("MyFrame"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setLayout(new BorderLayout()); 

    JButton glass1 = new JButton("glass1"); 
    JButton glass2 = new JButton("glass2"); 
    JButton glass3 = new JButton("glass3"); 

    String from = ""; 
    String to = ""; 
    int click_count = 0; 


glass1.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
    if(click_count ==0) 
    { 
     from = "glassX"; 
     click_count = 1; 
    }else if(click_count ==1) 
    { 
     from = "glassY"; 
     click_count = 0; 
    } 
    } 
}); 

glass2.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
    if(click_count ==0) 
    { 
     from = "glassX"; 
     click_count = 1; 
    }else if(click_count ==1) 
    { 
     from = "glassY"; 
     click_count = 0; 
    } 
    } 
}); 

glass3.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
    if(click_count ==0) 
    { 
     from = "glassX"; 
     click_count = 1; 
    }else if(click_count ==1) 
    { 
     from = "glassY"; 
     click_count = 0; 
    } 
    } 
}); 

    frame.getContentPane().add(glass1); 
    frame.getContentPane().add(glass2); 
    frame.getContentPane().add(glass3); 
    frame.pack(); 
    frame.setVisible(true); 

    // I want to store the first two clicks in these variables 
} 
관련 문제