2017-10-07 84 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決定了「from」初始玻璃,第二個JButton用戶從三個可用的點擊中確定了「to」目標玻璃。確定用戶所做的兩次點擊的順序

如何確定用戶點擊的特定兩個JButton? 每次用戶點擊三個JButton中的兩個,我想將與這兩個JButton關聯的字符串存儲在「from」和「to」變量中。

有沒有辦法做到這一點?

回答

1

使ActionListener和使用JButton#addActionListener將其添加到所有三個按鈕。該活動有一個可用的來源字段。

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

能否請你告訴我執行,如果我只想創建一個名爲JButtonGlassActionListener一個類,並添加這個類的實例,以每個JButton的? –

+0

如上所述,但只是將聽衆的內容移動到新班級 – Igor

+0

好的,但是我應該怎樣做「\\做些事情」? –

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

您可以將偵聽器添加到像this這樣的按鈕,並使用類變量來跟蹤按鈕被點擊的次數。

如:

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 
}